-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathToggleButtonRow.tsx
More file actions
112 lines (100 loc) · 2.65 KB
/
ToggleButtonRow.tsx
File metadata and controls
112 lines (100 loc) · 2.65 KB
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
import * as React from 'react';
import { StyleSheet, View, StyleProp, ViewStyle } from 'react-native';
import ToggleButton from './ToggleButton';
import ToggleButtonGroup from './ToggleButtonGroup';
export type Props<Value = string> = {
/**
* Function to execute on selection change.
*/
onValueChange: (value: Value) => void;
/**
* Value of the currently selected toggle button.
*/
value: Value;
/**
* React elements containing toggle buttons.
*/
children: React.ReactNode;
style?: StyleProp<ViewStyle>;
};
/**
* Toggle button row renders a group of toggle buttons in a row.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { ToggleButton } from 'react-native-paper';
*
* const MyComponent = () => {
* const [value, setValue] = React.useState('left');
*
* return (
* <ToggleButton.Row onValueChange={value => setValue(value)} value={value}>
* <ToggleButton icon="format-align-left" value="left" />
* <ToggleButton icon="format-align-right" value="right" />
* </ToggleButton.Row>
* );
* };
*
* export default MyComponent;
*
*```
*/
const ToggleButtonRow = <Value = string,>({
value,
onValueChange,
children,
style,
}: Props<Value>) => {
const count = React.Children.count(children);
return (
<ToggleButtonGroup value={value} onValueChange={onValueChange}>
<View style={[styles.row, style]}>
{React.Children.map(children, (child, i) => {
// @ts-expect-error: TypeScript complains about child.type but it doesn't matter
if (child && child.type === ToggleButton) {
// @ts-expect-error: We're sure that child is a React Element
return React.cloneElement(child, {
style: [
styles.button,
i === 0
? styles.first
: i === count - 1
? styles.last
: styles.middle,
// @ts-expect-error: We're sure that child is a React Element
child.props.style,
],
});
}
return child;
})}
</View>
</ToggleButtonGroup>
);
};
ToggleButtonRow.displayName = 'ToggleButton.Row';
const styles = StyleSheet.create({
row: {
flexDirection: 'row',
},
button: {
borderWidth: StyleSheet.hairlineWidth,
},
first: {
borderTopRightRadius: 0,
borderBottomRightRadius: 0,
},
middle: {
borderRadius: 0,
borderLeftWidth: 0,
},
last: {
borderLeftWidth: 0,
borderTopLeftRadius: 0,
borderBottomLeftRadius: 0,
},
});
export default ToggleButtonRow;
// @component-docs ignore-next-line
export { ToggleButtonRow };