forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SegmentedButtonItem.tsx
246 lines (229 loc) · 5.62 KB
/
SegmentedButtonItem.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import * as React from 'react';
import {
Animated,
GestureResponderEvent,
StyleProp,
StyleSheet,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import color from 'color';
import type { ThemeProp } from 'src/types';
import { useInternalTheme } from '../../core/theming';
import type { IconSource } from '../Icon';
import Icon from '../Icon';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
import {
getSegmentedButtonBorderRadius,
getSegmentedButtonColors,
getSegmentedButtonDensityPadding,
} from './utils';
export type Props = {
/**
* Whether the segmented button is checked
*/
checked: boolean;
/**
* Icon to display for the `SegmentedButtonItem`.
*/
icon?: IconSource;
/**
* @supported Available in v5.x with theme version 3
* Custom color for unchecked Text and Icon.
*/
uncheckedColor?: string;
/**
* @supported Available in v5.x with theme version 3
* Custom color for checked Text and Icon.
*/
checkedColor?: string;
/**
* Whether the button is disabled.
*/
disabled?: boolean;
/**
* Accessibility label for the `SegmentedButtonItem`. This is read by the screen reader when the user taps the button.
*/
accessibilityLabel?: string;
/**
* Function to execute on press.
*/
onPress?: (event: GestureResponderEvent) => void;
/**
* Value of button.
*/
value: string;
/**
* Label text of the button.
*/
label?: string;
/**
* Button segment.
*/
segment?: 'first' | 'last';
/**
* Show optional check icon to indicate selected state
*/
showSelectedCheck?: boolean;
/**
* Density is applied to the height, to allow usage in denser UIs.
*/
density?: 'regular' | 'small' | 'medium' | 'high';
style?: StyleProp<ViewStyle>;
/**
* testID to be used on tests.
*/
testID?: string;
/**
* @optional
*/
theme?: ThemeProp;
};
const SegmentedButtonItem = ({
checked,
accessibilityLabel,
disabled,
style,
showSelectedCheck,
checkedColor,
uncheckedColor,
icon,
testID,
label,
onPress,
segment,
density = 'regular',
theme: themeOverrides,
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const checkScale = React.useRef(new Animated.Value(0)).current;
React.useEffect(() => {
if (!showSelectedCheck) {
return;
}
if (checked) {
Animated.spring(checkScale, {
toValue: 1,
useNativeDriver: true,
}).start();
} else {
Animated.spring(checkScale, {
toValue: 0,
useNativeDriver: true,
}).start();
}
}, [checked, checkScale, showSelectedCheck]);
const { roundness, isV3 } = theme;
const { borderColor, textColor, borderWidth, backgroundColor } =
getSegmentedButtonColors({
checked,
theme,
disabled,
checkedColor,
uncheckedColor,
});
const borderRadius = (isV3 ? 5 : 1) * roundness;
const segmentBorderRadius = getSegmentedButtonBorderRadius({
theme,
segment,
});
const rippleColor = color(textColor).alpha(0.12).rgb().string();
const showIcon = !icon ? false : label && checked ? !showSelectedCheck : true;
const showCheckedIcon = checked && showSelectedCheck;
const iconSize = isV3 ? 18 : 16;
const iconStyle = {
marginRight: label ? 5 : showCheckedIcon ? 3 : 0,
...(label && {
transform: [
{
scale: checkScale.interpolate({
inputRange: [0, 1],
outputRange: [1, 0],
}),
},
],
}),
};
const buttonStyle: ViewStyle = {
backgroundColor,
borderColor,
borderWidth,
borderRadius,
...segmentBorderRadius,
};
const paddingVertical = getSegmentedButtonDensityPadding({ density });
const rippleStyle: ViewStyle = {
borderRadius,
...segmentBorderRadius,
};
const labelTextStyle: TextStyle = {
...(!isV3
? {
textTransform: 'uppercase',
fontWeight: '500',
}
: theme.fonts.labelLarge),
color: textColor,
};
return (
<View style={[buttonStyle, styles.button, style]}>
<TouchableRipple
borderless
onPress={onPress}
accessibilityLabel={accessibilityLabel}
accessibilityState={{ disabled, checked }}
accessibilityRole="button"
disabled={disabled}
rippleColor={rippleColor}
testID={testID}
style={rippleStyle}
theme={theme}
>
<View style={[styles.content, { paddingVertical }]}>
{showCheckedIcon ? (
<Animated.View
testID={`${testID}-check-icon`}
style={[iconStyle, { transform: [{ scale: checkScale }] }]}
>
<Icon source={'check'} size={iconSize} color={textColor} />
</Animated.View>
) : null}
{showIcon ? (
<Animated.View testID={`${testID}-icon`} style={iconStyle}>
<Icon source={icon} size={iconSize} color={textColor} />
</Animated.View>
) : null}
<Text
variant="labelLarge"
style={[styles.label, labelTextStyle]}
selectable={false}
numberOfLines={1}
>
{label}
</Text>
</View>
</TouchableRipple>
</View>
);
};
const styles = StyleSheet.create({
button: {
flex: 1,
minWidth: 76,
borderStyle: 'solid',
},
label: {
textAlign: 'center',
},
content: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
paddingVertical: 9,
paddingHorizontal: 16,
},
});
export default SegmentedButtonItem;
export { SegmentedButtonItem as SegmentedButton };