Skip to content

Commit 42dbbe6

Browse files
authored
feat(ui): list component
1 parent d63df29 commit 42dbbe6

File tree

9 files changed

+1392
-77
lines changed

9 files changed

+1392
-77
lines changed

src/framework/ui/index.ts

Lines changed: 86 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import {
33
Avatar as AvatarComponent,
44
Props as AvatarProps,
55
} from './avatar/avatar.component';
6+
import {
7+
BottomNavigatorTab as BottomNavigatorTabComponent,
8+
Props as BottomNavigatorTabProps,
9+
} from './tabNavigator/bottomNavigatorTab.component';
10+
import {
11+
BottomTabNavigator as BottomTabNavigatorComponent,
12+
Props as BottomTabNavigatorProps,
13+
} from './tabNavigator/bottomTabNavigator.component';
614
import {
715
Button as ButtonComponent,
816
Props as ButtonProps,
@@ -11,14 +19,34 @@ import {
1119
ButtonGroup as ButtonGroupComponent,
1220
Props as ButtonGroupProps,
1321
} from './buttonGroup/buttonGroup.component';
22+
import {
23+
CheckBox as CheckBoxComponent,
24+
Props as CheckBoxProps,
25+
} from './checkbox/checkbox.component';
1426
import {
1527
Input as InputComponent,
1628
Props as InputProps,
1729
} from './input/input.component';
1830
import {
19-
Text as TextComponent,
20-
Props as TextProps,
21-
} from './text/text.component';
31+
Layout as LayoutComponent,
32+
Props as LayoutProps,
33+
} from './layout/layout.component';
34+
import {
35+
List as ListComponent,
36+
Props as ListProps,
37+
} from './list/list.component';
38+
import {
39+
ListItem as ListItemComponent,
40+
Props as ListItemProps,
41+
} from './list/listItem.component';
42+
import {
43+
Modal as ModalComponent,
44+
Props as ModalProps,
45+
} from './modal/modal.component';
46+
import {
47+
Popover as PopoverComponent,
48+
Props as PopoverProps,
49+
} from './popover/popover.component';
2250
import {
2351
Radio as RadioComponent,
2452
Props as RadioProps,
@@ -27,18 +55,6 @@ import {
2755
RadioGroup as RadioGroupComponent,
2856
Props as RadioGroupProps,
2957
} from './radioGroup/radioGroup.component';
30-
import {
31-
Layout as LayoutComponent,
32-
Props as LayoutProps,
33-
} from './layout/layout.component';
34-
import {
35-
Toggle as ToggleComponent,
36-
Props as ToggleProps,
37-
} from './toggle/toggle.component';
38-
import {
39-
CheckBox as CheckBoxComponent,
40-
Props as CheckBoxProps,
41-
} from './checkbox/checkbox.component';
4258
import {
4359
Tab as TabComponent,
4460
Props as TabProps,
@@ -47,26 +63,22 @@ import {
4763
TabBar as TabBarComponent,
4864
Props as TabBarProps,
4965
} from './tab/tabBar.component';
50-
import {
51-
Popover as PopoverComponent,
52-
Props as PopoverProps,
53-
} from './popover/popover.component';
54-
import {
55-
Tooltip as TooltipComponent,
56-
Props as TooltipProps,
57-
} from './tooltip/tooltip.component';
58-
import {
59-
ViewPager,
60-
Props as ViewPagerProps,
61-
} from './viewPager/viewPager.component';
6266
import {
6367
TabView,
6468
Props as TabViewProps,
6569
} from './tab/tabView.component';
6670
import {
67-
BottomTabNavigator as BottomTabNavigatorComponent,
68-
Props as BottomTabNavigatorProps,
69-
} from './tabNavigator/bottomTabNavigator.component';
71+
Text as TextComponent,
72+
Props as TextProps,
73+
} from './text/text.component';
74+
import {
75+
Toggle as ToggleComponent,
76+
Props as ToggleProps,
77+
} from './toggle/toggle.component';
78+
import {
79+
Tooltip as TooltipComponent,
80+
Props as TooltipProps,
81+
} from './tooltip/tooltip.component';
7082
import {
7183
TopNavigationBar as TopNavigationBarComponent,
7284
Props as TopNavigationBarProps,
@@ -76,13 +88,9 @@ import {
7688
Props as TopNavigationBarActionProps,
7789
} from './topNavigationBar/topNavigationBarAction.component';
7890
import {
79-
BottomNavigatorTab as BottomNavigatorTabComponent,
80-
Props as BottomNavigatorTabProps,
81-
} from './tabNavigator/bottomNavigatorTab.component';
82-
import {
83-
Modal as ModalComponent,
84-
Props as ModalProps,
85-
} from './modal/modal.component';
91+
ViewPager,
92+
Props as ViewPagerProps,
93+
} from './viewPager/viewPager.component';
8694
import {
8795
ButtonAlignment,
8896
ButtonAlignments,
@@ -93,68 +101,80 @@ import {
93101
} from './popover/type';
94102

95103
const Avatar = styled<AvatarComponent, AvatarProps>(AvatarComponent);
104+
const BottomNavigatorTab = styled<BottomNavigatorTabComponent, BottomNavigatorTabProps>(BottomNavigatorTabComponent);
105+
const BottomTabNavigator = styled<BottomTabNavigatorComponent, BottomTabNavigatorProps>(BottomTabNavigatorComponent);
96106
const Button = styled<ButtonComponent, ButtonProps>(ButtonComponent);
97107
const ButtonGroup = styled<ButtonGroupComponent, ButtonGroupProps>(ButtonGroupComponent);
108+
const CheckBox = styled<CheckBoxComponent, CheckBoxProps>(CheckBoxComponent);
98109
const Input = styled<InputComponent, InputProps>(InputComponent);
99-
const Text = styled<TextComponent, TextProps>(TextComponent);
110+
const Layout = styled<LayoutComponent, LayoutProps>(LayoutComponent);
111+
const List = styled<ListComponent, ListProps>(ListComponent);
112+
const ListItem = styled<ListItemComponent, ListItemProps>(ListItemComponent);
113+
const Modal = styled<ModalComponent, ModalProps>(ModalComponent);
114+
const Popover = styled<PopoverComponent, PopoverProps>(PopoverComponent);
100115
const Radio = styled<RadioComponent, RadioProps>(RadioComponent);
101116
const RadioGroup = styled<RadioGroupComponent, RadioGroupProps>(RadioGroupComponent);
102-
const Layout = styled<LayoutComponent, LayoutProps>(LayoutComponent);
103-
const Toggle = styled<ToggleComponent, ToggleProps>(ToggleComponent);
104-
const CheckBox = styled<CheckBoxComponent, CheckBoxProps>(CheckBoxComponent);
105117
const Tab = styled<TabComponent, TabProps>(TabComponent);
106118
const TabBar = styled<TabBarComponent, TabBarProps>(TabBarComponent);
107-
const Popover = styled<PopoverComponent, PopoverProps>(PopoverComponent);
119+
const Text = styled<TextComponent, TextProps>(TextComponent);
120+
const Toggle = styled<ToggleComponent, ToggleProps>(ToggleComponent);
108121
const Tooltip = styled<TooltipComponent, TooltipProps>(TooltipComponent);
109-
const BottomTabNavigator = styled<BottomTabNavigatorComponent, BottomTabNavigatorProps>(BottomTabNavigatorComponent);
110-
const BottomNavigatorTab = styled<BottomNavigatorTabComponent, BottomNavigatorTabProps>(BottomNavigatorTabComponent);
111122
const TopNavigationBar = styled<TopNavigationBarComponent, TopNavigationBarProps>(TopNavigationBarComponent);
112123
const TopNavigationBarAction =
113124
styled<TopNavigationBarActionComponent, TopNavigationBarActionProps>(TopNavigationBarActionComponent);
114-
const Modal = styled<ModalComponent, ModalProps>(ModalComponent);
115125

116126
export {
117127
Avatar,
128+
BottomNavigatorTab,
129+
BottomTabNavigator,
118130
Button,
119131
ButtonGroup,
132+
CheckBox,
120133
Input,
121-
Text,
122134
Layout,
123-
LayoutProps,
135+
List,
136+
ListItem,
137+
Modal,
138+
Popover,
124139
Radio,
125-
RadioProps,
126140
RadioGroup,
127-
RadioGroupProps,
128-
Toggle,
129-
ToggleProps,
130-
CheckBox,
131141
Tab,
132142
TabBar,
133-
Popover,
143+
TabView,
144+
Text,
145+
Toggle,
134146
Tooltip,
147+
TopNavigationBar,
148+
TopNavigationBarAction,
135149
ViewPager,
136-
TabView,
150+
};
151+
152+
export {
137153
AvatarProps,
154+
BottomNavigatorTabProps,
155+
BottomTabNavigatorProps,
138156
ButtonProps,
139-
InputProps,
140157
ButtonGroupProps,
141158
CheckBoxProps,
159+
InputProps,
160+
LayoutProps,
161+
ListProps,
162+
ListItemProps,
163+
ModalProps,
164+
PopoverProps,
165+
RadioProps,
166+
RadioGroupProps,
142167
TabProps,
143168
TabBarProps,
144-
PopoverProps,
145-
TooltipProps,
146-
ViewPagerProps,
147169
TabViewProps,
148-
BottomTabNavigator,
149-
BottomTabNavigatorProps,
150-
BottomNavigatorTab,
151-
BottomNavigatorTabProps,
152-
TopNavigationBar,
170+
TooltipProps,
171+
ToggleProps,
153172
TopNavigationBarProps,
154-
TopNavigationBarAction,
155173
TopNavigationBarActionProps,
156-
Modal,
157-
ModalProps,
174+
ViewPagerProps,
175+
};
176+
177+
export {
158178
ButtonAlignment,
159179
ButtonAlignments,
160180
PopoverPlacement,
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from 'react';
2+
import {
3+
FlatList,
4+
FlatListProps,
5+
ListRenderItemInfo,
6+
StyleSheet,
7+
} from 'react-native';
8+
import {
9+
StyledComponentProps,
10+
StyleType,
11+
} from '@kitten/theme';
12+
import { Props as ListItemProps } from './listItem.component';
13+
14+
// this is basically needed to avoid generics in required props
15+
type ItemType = any;
16+
17+
interface ListProps<ItemT> {
18+
renderItem: (info: ListRenderItemInfo<ItemT>, style: StyleType) => React.ReactElement<any>;
19+
}
20+
21+
export type Props = ListProps<ItemType> & StyledComponentProps & FlatListProps<ItemType>;
22+
23+
export class List extends React.Component<Props> {
24+
25+
private listRef: React.RefObject<FlatList<ItemType>> = React.createRef();
26+
27+
public scrollToEnd = (params?: { animated?: boolean }) => {
28+
const { current: list } = this.listRef;
29+
30+
list.scrollToEnd(params);
31+
};
32+
33+
public scrollToIndex = (params: {
34+
animated?: boolean;
35+
index: number;
36+
viewOffset?: number;
37+
viewPosition?: number
38+
}) => {
39+
40+
const { current: list } = this.listRef;
41+
42+
list.scrollToIndex(params);
43+
};
44+
45+
public scrollToOffset(params: { animated?: boolean; offset: number }) {
46+
const { current: list } = this.listRef;
47+
48+
list.scrollToOffset(params);
49+
}
50+
51+
private getComponentStyle = (source: StyleType): StyleType => {
52+
const { item, ...container } = source;
53+
54+
return {
55+
container: container,
56+
};
57+
};
58+
59+
private getItemStyle = (source: StyleType, index: number): StyleType => {
60+
const { item } = source;
61+
62+
return item;
63+
};
64+
65+
private extractItemKey = (item: ItemType, index: number): string => {
66+
return index.toString();
67+
};
68+
69+
private renderItem = (info: ListRenderItemInfo<ItemType>): React.ReactElement<ListItemProps> => {
70+
const { renderItem, themedStyle } = this.props;
71+
const { index } = info;
72+
73+
const itemStyle: StyleType = this.getItemStyle(themedStyle, index);
74+
const itemElement: React.ReactElement<ListItemProps> = renderItem(info, itemStyle);
75+
76+
return React.cloneElement(itemElement, {
77+
style: [itemStyle, itemElement.props.style, styles.item],
78+
index: index,
79+
});
80+
};
81+
82+
public render(): React.ReactElement<FlatListProps<ItemType>> {
83+
const { style, themedStyle, ...derivedProps } = this.props;
84+
const { container } = this.getComponentStyle(themedStyle);
85+
86+
return (
87+
<FlatList
88+
{...derivedProps}
89+
ref={this.listRef}
90+
style={[container, style, styles.container]}
91+
keyExtractor={this.extractItemKey}
92+
renderItem={this.renderItem}
93+
/>
94+
);
95+
}
96+
}
97+
98+
const styles = StyleSheet.create({
99+
container: {},
100+
item: {},
101+
});

0 commit comments

Comments
 (0)