-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.tsx
113 lines (104 loc) · 2.11 KB
/
index.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
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import CheckboxControl from '..';
const meta: ComponentMeta< typeof CheckboxControl > = {
component: CheckboxControl,
title: 'Components/CheckboxControl',
argTypes: {
onChange: {
action: 'onChange',
},
checked: {
control: { type: null },
},
help: { control: { type: 'text' } },
},
parameters: {
controls: {
expanded: true,
exclude: [ 'heading' ],
},
docs: { source: { state: 'open' } },
},
};
export default meta;
const DefaultTemplate: ComponentStory< typeof CheckboxControl > = ( {
onChange,
...args
} ) => {
const [ isChecked, setChecked ] = useState( true );
return (
<CheckboxControl
{ ...args }
checked={ isChecked }
onChange={ ( v ) => {
setChecked( v );
onChange( v );
} }
/>
);
};
export const Default: ComponentStory<
typeof CheckboxControl
> = DefaultTemplate.bind( {} );
Default.args = {
label: 'Is author',
help: 'Is the user an author or not?',
};
export const Indeterminate: ComponentStory< typeof CheckboxControl > = ( {
onChange,
...args
} ) => {
const [ fruits, setFruits ] = useState( { apple: false, orange: false } );
const isAllChecked = Object.values( fruits ).every( Boolean );
const isIndeterminate =
Object.values( fruits ).some( Boolean ) && ! isAllChecked;
return (
<>
<CheckboxControl
{ ...args }
checked={ isAllChecked }
indeterminate={ isIndeterminate }
onChange={ ( v ) => {
setFruits( {
apple: v,
orange: v,
} );
onChange( v );
} }
/>
<CheckboxControl
label="Apple"
checked={ fruits.apple }
onChange={ ( apple ) =>
setFruits( ( prevState ) => ( {
...prevState,
apple,
} ) )
}
/>
<CheckboxControl
label="Orange"
checked={ fruits.orange }
onChange={ ( orange ) =>
setFruits( ( prevState ) => ( {
...prevState,
orange,
} ) )
}
/>
</>
);
};
Indeterminate.args = {
label: 'Select all',
};