Skip to content

Commit 7242ba8

Browse files
author
Pierre Poupin
authored
chore: refactor example components (#150)
* chore(example): remove duplicated component * chore(example): remove dead props * chore(example): add missing memo * chore(example): refactor duplicate Program and ProgramLandscape components into one * chore(example): refactor duplicate ProgramList
1 parent d6fbe20 commit 7242ba8

9 files changed

Lines changed: 83 additions & 311 deletions

File tree

packages/example/src/modules/program/view/LongProgram.tsx

Lines changed: 0 additions & 46 deletions
This file was deleted.

packages/example/src/modules/program/view/Program.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type ProgramProps = {
99
isFocused?: boolean;
1010
programInfo: ProgramInfo;
1111
label?: string;
12+
variant?: 'portrait' | 'landscape';
1213
};
1314

1415
const Label = React.memo(({ label }: { label: string }) => {
@@ -17,33 +18,41 @@ const Label = React.memo(({ label }: { label: string }) => {
1718
Label.displayName = 'Label';
1819

1920
export const Program = React.memo(
20-
React.forwardRef<View, ProgramProps>(({ isFocused = false, programInfo, label }, ref) => {
21-
const imageSource = programInfo.image;
21+
React.forwardRef<View, ProgramProps>(
22+
({ isFocused = false, programInfo, label, variant = 'portrait' }, ref) => {
23+
const imageSource = programInfo.image;
24+
const scaleAnimation = useFocusAnimation(isFocused);
2225

23-
const scaleAnimation = useFocusAnimation(isFocused);
24-
25-
return (
26-
<ProgramContainer
27-
style={scaleAnimation} // Apply the animated scale transform
28-
ref={ref}
29-
isFocused={isFocused}
30-
>
31-
<ProgramImage source={imageSource} accessible />
32-
{label ? (
33-
<Overlay>
34-
<Label label={label} />
35-
</Overlay>
36-
) : null}
37-
</ProgramContainer>
38-
);
39-
}),
26+
return (
27+
<ProgramContainer
28+
style={scaleAnimation} // Apply the animated scale transform
29+
ref={ref}
30+
isFocused={isFocused}
31+
variant={variant}
32+
>
33+
<ProgramImage source={imageSource} accessible />
34+
{label ? (
35+
<Overlay>
36+
<Label label={label} />
37+
</Overlay>
38+
) : null}
39+
</ProgramContainer>
40+
);
41+
},
42+
),
4043
);
4144

4245
Program.displayName = 'Program';
4346

44-
const ProgramContainer = styled(Animated.View)<{ isFocused: boolean }>(({ isFocused, theme }) => ({
45-
height: theme.sizes.program.portrait.height,
46-
width: theme.sizes.program.portrait.width,
47+
const ProgramContainer = styled(Animated.View)<{
48+
isFocused: boolean;
49+
variant: 'portrait' | 'landscape';
50+
}>(({ isFocused, variant, theme }) => ({
51+
height: theme.sizes.program.portrait.height, // Height is the same for both variants
52+
width:
53+
variant === 'landscape'
54+
? theme.sizes.program.landscape.width * 2
55+
: theme.sizes.program.portrait.width,
4756
overflow: 'hidden',
4857
borderRadius: 20,
4958
borderColor: isFocused ? theme.colors.primary.light : 'transparent',

packages/example/src/modules/program/view/ProgramLandscape.tsx

Lines changed: 0 additions & 61 deletions
This file was deleted.

packages/example/src/modules/program/view/ProgramList.tsx

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,61 @@ const NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN = 7;
2121
const WINDOW_SIZE = NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN + 8;
2222
const ROW_PADDING = scaledPixels(70);
2323

24+
type ProgramListProps = {
25+
orientation?: 'vertical' | 'horizontal';
26+
containerStyle?: object;
27+
listRef?: MutableRefObject<SpatialNavigationVirtualizedListRef>;
28+
data?: ProgramInfo[];
29+
variant?: 'normal' | 'variable-size';
30+
};
31+
2432
export const ProgramList = ({
25-
orientation,
33+
orientation = 'horizontal',
2634
containerStyle,
2735
listRef,
2836
data,
29-
}: {
30-
orientation?: 'vertical' | 'horizontal';
31-
containerStyle?: object;
32-
listRef: MutableRefObject<SpatialNavigationVirtualizedListRef>;
33-
data?: ProgramInfo[];
34-
}) => {
37+
variant = 'normal',
38+
}: ProgramListProps) => {
3539
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
40+
const theme = useTheme();
41+
42+
const isItemLarge = useCallback((item: ProgramInfo) => {
43+
return parseInt(item.id, 10) % 2 === 0; // Arbitrary condition to decide size
44+
}, []);
3645

3746
const renderItem = useCallback(
3847
({ item, index }: { item: ProgramInfo; index: number }) => (
3948
<ProgramNode
4049
programInfo={item}
4150
onSelect={() => navigation.push('ProgramDetail', { programInfo: item })}
4251
label={index.toString()}
52+
variant={variant === 'variable-size' && isItemLarge(item) ? 'landscape' : 'portrait'}
4353
/>
4454
),
45-
[navigation],
55+
[navigation, isItemLarge, variant],
4656
);
47-
const theme = useTheme();
4857

49-
const programInfos = useMemo(() => getPrograms(1000), []);
58+
const programInfos = useMemo(
59+
() => data ?? getPrograms(variant === 'variable-size' ? 10 : 1000),
60+
[data, variant],
61+
);
5062

5163
return (
5264
<SpatialNavigationNode>
5365
{({ isActive }) => (
5466
<Container isActive={isActive} style={containerStyle}>
5567
<SpatialNavigationVirtualizedList
5668
orientation={orientation}
57-
data={data ?? programInfos}
69+
data={programInfos}
5870
renderItem={renderItem}
59-
itemSize={theme.sizes.program.portrait.width + 30}
71+
itemSize={
72+
variant === 'variable-size'
73+
? (item: ProgramInfo) =>
74+
isItemLarge(item)
75+
? theme.sizes.program.landscape.width * 2 + 45
76+
: theme.sizes.program.portrait.width + 30
77+
: theme.sizes.program.portrait.width + 30 // Default item size for "normal"
78+
}
6079
numberOfRenderedItems={WINDOW_SIZE}
6180
numberOfItemsVisibleOnScreen={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN}
6281
onEndReachedThresholdItemsNumber={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN}
@@ -75,9 +94,11 @@ export const ProgramList = ({
7594
export const ProgramsRow = ({
7695
containerStyle,
7796
listRef,
97+
variant = 'normal',
7898
}: {
7999
containerStyle?: object;
80-
listRef: MutableRefObject<SpatialNavigationVirtualizedListRef>;
100+
listRef?: MutableRefObject<SpatialNavigationVirtualizedListRef>;
101+
variant?: 'normal' | 'variable-size';
81102
}) => {
82103
const theme = useTheme();
83104
return (
@@ -87,6 +108,7 @@ export const ProgramsRow = ({
87108
height: theme.sizes.program.portrait.height + ROW_PADDING,
88109
}}
89110
listRef={listRef}
111+
variant={variant}
90112
/>
91113
);
92114
};

packages/example/src/modules/program/view/ProgramListVariableSize.tsx

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)