File tree Expand file tree Collapse file tree 3 files changed +35
-4
lines changed Expand file tree Collapse file tree 3 files changed +35
-4
lines changed Original file line number Diff line number Diff line change 1+ export function chunk < T > ( arr : readonly T [ ] , size : number ) : T [ ] [ ] {
2+ if ( ! Number . isInteger ( size ) || size <= 0 ) {
3+ throw new Error ( "Size must be an integer greater than zero." ) ;
4+ }
5+
6+ const chunkLength = Math . ceil ( arr . length / size ) ;
7+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
8+ const result : T [ ] [ ] = Array ( chunkLength ) ;
9+
10+ for ( let index = 0 ; index < chunkLength ; index ++ ) {
11+ const start = index * size ;
12+ const end = start + size ;
13+
14+ result [ index ] = arr . slice ( start , end ) ;
15+ }
16+
17+ return result ;
18+ }
Original file line number Diff line number Diff line change @@ -34,9 +34,10 @@ A series of composable ESLint plugins for libraries and frameworks that use Reac
3434- [ ` eslint-config-sukka ` ] ( https://github.com/SukkaW/eslint-config-sukka ) - Sukka's ESLint config preset.
3535
3636<TweetCards tweets = { [
37- " 1841248980354941038" ,
38- " 1839913920984678890" ,
3937 " 1865166494709026873" ,
38+ " 1839913920984678890" ,
39+ " 1841248980354941038" ,
40+ " 1859137094976696467" ,
4041]} />
4142
4243## FAQ
Original file line number Diff line number Diff line change 1+ import { useMemo } from "react" ;
12import { Tweet } from "react-tweet" ;
23
4+ import { chunk } from "#/lib/chunk" ;
5+
36export function TweetCards ( { tweets } : { tweets : string [ ] } ) {
7+ const chunkedTweets = useMemo ( ( ) => chunk ( tweets , 2 ) , [ tweets ] ) ;
48 return (
5- < div className = "grid grid-cols-1 mt-4 sm:grid-cols-2 sm:gap-x-8 sm:mt-8 justify-center" >
6- { tweets . map ( ( id ) => < Tweet key = { id } id = { id } /> ) }
9+ < div className = "grid grid-cols-1 md:grid-cols-2 sm:gap-x-8 sm:mt-8" >
10+ { chunkedTweets . map ( ( chunk ) => (
11+ < div key = { chunk . join ( "_" ) } className = "grid justify-center" >
12+ { chunk . map ( ( id ) => (
13+ < div key = { id } className = "h-fit max-w-full" >
14+ < Tweet id = { id } />
15+ </ div >
16+ ) ) }
17+ </ div >
18+ ) ) }
719 </ div >
820 ) ;
921}
You can’t perform that action at this time.
0 commit comments