forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToggleButtonGroup.tsx
75 lines (68 loc) · 1.76 KB
/
ToggleButtonGroup.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
import * as React from 'react';
type Props = {
/**
* Function to execute on selection change.
*/
onValueChange: (value: string) => void;
/**
* Value of the currently selected toggle button.
*/
value: string;
/**
* React elements containing toggle buttons.
*/
children: React.ReactNode;
};
type ToggleButtonContextType = {
value: string;
onValueChange: (item: string) => void;
};
export const ToggleButtonGroupContext = React.createContext<
ToggleButtonContextType
>(null as any);
/**
* Toggle group allows to control a group of toggle buttons.</br>
* It doesn't change the appearance of the toggle buttons. If you want to group them in a row, checkout <a href="/toggle-button-row.html">`ToggleButton.Row`</a>.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { View } from 'react-native';
* import { ToggleButton } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* state = {
* value: 'left',
* };
*
* render() {
* return(
* <ToggleButton.Group
* onValueChange={value => this.setState({ value })}
* value={this.state.value}
* >
* <ToggleButton icon="format-align-left" value="left" />
* <ToggleButton icon="format-align-right" value="right" />
* </ToggleButton.Group>
* )
* }
* }
*```
*/
class ToggleButtonGroup extends React.Component<Props> {
static displayName = 'ToggleButton.Group';
render() {
const { value, onValueChange, children } = this.props;
return (
<ToggleButtonGroupContext.Provider
value={{
value,
onValueChange,
}}
>
{children}
</ToggleButtonGroupContext.Provider>
);
}
}
export default ToggleButtonGroup;