-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathSegmentedButtonItem.tsx
287 lines (273 loc) · 6.82 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import * as React from 'react';
import {
Animated,
ColorValue,
GestureResponderEvent,
PressableAndroidRippleConfig,
StyleProp,
StyleSheet,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import color from 'color';
import type { ThemeProp } from 'src/types';
import {
getSegmentedButtonBorderRadius,
getSegmentedButtonColors,
getSegmentedButtonDensityPadding,
} from './utils';
import { useInternalTheme } from '../../core/theming';
import CrossFadeIcon from '../CrossFadeIcon';
import type { IconSource } from '../Icon';
import Icon from '../Icon';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
export type Props = {
/**
* Whether the segmented button is checked
*/
checked: boolean;
/**
* Icon to display for the `SegmentedButtonItem`.
*/
icon?: IconSource;
/**
* Whether an icon change is animated.
*/
animated?: boolean;
/**
* @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;
/**
* Color of the ripple effect.
*/
rippleColor?: ColorValue;
/**
* Whether the button is disabled.
*/
disabled?: boolean;
/**
* Type of background drawabale to display the feedback (Android).
* https://reactnative.dev/docs/pressable#rippleconfig
*/
background?: PressableAndroidRippleConfig;
/**
* 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;
/**
* Label text number of lines of the button.
*/
numberOfLines?: number;
/**
* 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';
/**
* Specifies the largest possible scale a label font can reach.
*/
labelMaxFontSizeMultiplier?: number;
style?: StyleProp<ViewStyle>;
/**
* Style for the button label.
*/
labelStyle?: StyleProp<TextStyle>;
/**
* testID to be used on tests.
*/
testID?: string;
/**
* @optional
*/
theme?: ThemeProp;
};
const SegmentedButtonItem = ({
checked,
animated = false,
accessibilityLabel,
disabled,
style,
labelStyle,
showSelectedCheck,
checkedColor,
uncheckedColor,
rippleColor: customRippleColor,
background,
icon,
testID,
label,
numberOfLines,
onPress,
segment,
density = 'regular',
theme: themeOverrides,
labelMaxFontSizeMultiplier,
}: 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 =
customRippleColor || 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,
};
const IconComponent = animated ? CrossFadeIcon : Icon;
const NumberOfLines = numberOfLines ? numberOfLines : 1;
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}
background={background}
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}>
<IconComponent
color={textColor}
source={icon ? icon : 'progress-question'}
size={iconSize}
/>
</Animated.View>
) : null}
<Text
variant="labelLarge"
style={[styles.label, labelTextStyle, labelStyle]}
selectable={false}
numberOfLines={NumberOfLines}
maxFontSizeMultiplier={labelMaxFontSizeMultiplier}
testID={`${testID}-label`}
>
{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 };