/
SettingsPageList.tsx
79 lines (62 loc) · 1.96 KB
/
SettingsPageList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { IntlContext, IntlContextData, getComponent, resolveThunk } from '@bluebase/core';
import { List, NavigationActions, NavigationOptions, Noop } from '@bluebase/components';
import React from 'react';
import { SettingsPageProps } from '../SettingsPage';
export interface SettingsPageListProps {
name: string;
pages: SettingsPageProps[];
}
export class SettingsPageList extends React.PureComponent<SettingsPageListProps> {
static contextType = IntlContext;
private HeaderComponent?: React.ComponentType<any>;
private FooterComponent?: React.ComponentType<any>;
componentWillMount() {
const { name } = this.props;
if (name) {
this.HeaderComponent = getComponent(`${name}RootPageHeader`, 'Noop');
this.FooterComponent = getComponent(`${name}RootPageFooter`, 'Noop');
}
}
render() {
const { __ }: IntlContextData = this.context;
const HeaderComponent = this.HeaderComponent || Noop;
const FooterComponent = this.FooterComponent || Noop;
return (
<React.Fragment>
<HeaderComponent />
<List>
<NavigationActions>
{({ navigate, state }) =>
(this.props.pages).map(page => {
const options = resolveThunk(page.navigationOptions || {});
const title = getTitle(options);
const left = getIcon(options);
const onPress = () => navigate(page.name, state.params);
return (
<List.Item key={page.name} onPress={onPress} title={__(title)} left={left} />
);
})
}
</NavigationActions>
</List>
<FooterComponent />
</React.Fragment>
);
}
}
function getTitle(options: NavigationOptions) {
return (options as any).drawerLabel || options.title || options.headerTitle;
}
function getIcon(options: NavigationOptions) {
const icon = (options as any).drawerIcon;
if (!icon) {
return;
}
if (typeof icon === 'function') {
return icon();
}
if (icon && typeof icon.type === 'string') {
return <List.Icon {...icon} />;
}
}
export default SettingsPageList;