-
Notifications
You must be signed in to change notification settings - Fork 6
/
ContentSwitcher.tsx
119 lines (109 loc) · 3.22 KB
/
ContentSwitcher.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React from 'react';
import { StyleSheet, ScrollView, View } from 'react-native';
import { Checkbox, ContentSwitcher, SwitcherItem, Button } from '@carbon/react-native';
const styles = StyleSheet.create({
view: {
padding: 16,
flex: 1,
},
container: {
flexGrow: 1,
paddingBottom: 64,
},
itemStyle: {
marginBottom: 16,
},
button: {
marginBottom: 32,
},
});
export default class TestContentSwitcher extends React.Component {
state = {
currentIndexA: undefined as number | undefined,
currentIndexB: undefined as number | undefined,
currentIndexC: undefined as number | undefined,
disableSecond: false,
};
private onChange = (index: number, item: SwitcherItem): void => {
console.log(`Pressed to switch to ${item.text}`);
this.setState({ [item.data as string]: index });
};
private get twoOptions(): SwitcherItem[] {
const { disableSecond } = this.state;
return [
{
text: 'Item 1',
data: 'currentIndexA',
},
{
text: 'Item 2',
data: 'currentIndexA',
disabled: disableSecond,
},
];
}
private get threeOptions(): SwitcherItem[] {
const { disableSecond } = this.state;
return [
{
text: 'Item 1',
data: 'currentIndexB',
},
{
text: 'Item 2',
data: 'currentIndexB',
disabled: disableSecond,
},
{
text: 'Item 3',
data: 'currentIndexB',
},
];
}
private get longTextOptions(): SwitcherItem[] {
const { disableSecond } = this.state;
return [
{
text: 'Item 1',
data: 'currentIndexC',
},
{
text: 'Item 2 with really long text that will go into ellipsis instead of wrapping like the one below',
data: 'currentIndexC',
disabled: disableSecond,
},
{
text: 'Item 3 with really long text that will wrap and be kind of odd on the screen',
data: 'currentIndexC',
},
];
}
render(): React.ReactNode {
const { disableSecond, currentIndexA, currentIndexB, currentIndexC } = this.state;
return (
<ScrollView keyboardShouldPersistTaps="handled" contentInsetAdjustmentBehavior="automatic" contentContainerStyle={styles.container} style={styles.view}>
<Checkbox checked={disableSecond} id="subtext" onPress={(value) => this.setState({ disableSecond: value })} label="Make second one disabled" />
<Button
style={styles.button}
onPress={() => {
this.setState({
currentIndexA: 1,
currentIndexB: 1,
currentIndexC: 1,
});
}}
text="Make second one active for all"
/>
<View style={styles.itemStyle}>
<ContentSwitcher items={this.twoOptions} onChange={this.onChange} selectedIndex={currentIndexA} />
</View>
<View style={styles.itemStyle}>
<ContentSwitcher items={this.threeOptions} onChange={this.onChange} selectedIndex={currentIndexB} />
</View>
<View style={styles.itemStyle}>
<ContentSwitcher items={this.longTextOptions} onChange={this.onChange} selectedIndex={currentIndexC} />
</View>
</ScrollView>
);
}
}