Skip to content

Commit

Permalink
responsive grid - remove nested array
Browse files Browse the repository at this point in the history
  • Loading branch information
giannif committed Jan 27, 2025
1 parent 7d3c013 commit c39237a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/react-components/src/components/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ const Loader = styled.div<{ $isFirstLoad: boolean }>`
opacity: ${(props) => (props.$isFirstLoad ? 0 : 1)};
`

function fillArray(length: number, columnOffsets: number[] = []) {
return Array.apply(null, Array(length)).map((_, index: number) => columnOffsets[index] || 0)
function fillArray<T>(length: number, defaultValue: T, initArray: T[] = []) {
return Array.apply(null, Array(length)).map((_, index: number) => initArray[index] || defaultValue)
}

function getColumns(columns: number, columnOffsets: number[] | undefined, gifs: IGif[], gifWidth: number) {
const sorter: [IGif[]][] = []
const columnHeights: number[] = fillArray(columns, columnOffsets)
const sorter: IGif[][] = fillArray(columns, [])
const columnHeights: number[] = fillArray(columns, 0, columnOffsets)
gifs.forEach((gif) => {
// get the shortest column
const columnTarget = columnHeights.indexOf(Math.min(...columnHeights))
const [existingGifs = []] = sorter[columnTarget] || []
sorter[columnTarget] = [[...existingGifs, gif]]
const existingGifs = sorter[columnTarget] || []
sorter[columnTarget] = [...existingGifs, gif]
columnHeights[columnTarget] += getGifHeight(gif, gifWidth)
})
return sorter
Expand Down Expand Up @@ -201,7 +201,7 @@ class Grid extends PureComponent<Props, State> {
gap: gutter,
}}
>
{sortedIntoColumns.map(([columnGifs = []], columnIndex) => (
{sortedIntoColumns.map((columnGifs = [], columnIndex) => (
<div
key={columnIndex}
style={{
Expand Down

0 comments on commit c39237a

Please sign in to comment.