-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.tsx
134 lines (123 loc) · 3.28 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { formatLowercase, formatUppercase } from '@wordpress/icons';
/**
* Internal dependencies
*/
import {
ToggleGroupControl,
ToggleGroupControlOption,
ToggleGroupControlOptionIcon,
} from '../index';
import type {
ToggleGroupControlOptionProps,
ToggleGroupControlOptionIconProps,
ToggleGroupControlProps,
} from '../types';
const meta: ComponentMeta< typeof ToggleGroupControl > = {
component: ToggleGroupControl,
title: 'Components (Experimental)/ToggleGroupControl',
subcomponents: { ToggleGroupControlOption, ToggleGroupControlOptionIcon },
argTypes: {
help: { control: { type: 'text' } },
onChange: { action: 'onChange' },
value: { control: { type: null } },
},
parameters: {
controls: { expanded: true },
docs: { source: { state: 'open' } },
},
};
export default meta;
const Template: ComponentStory< typeof ToggleGroupControl > = ( {
onChange,
...props
} ) => {
const [ value, setValue ] =
useState< ToggleGroupControlProps[ 'value' ] >();
return (
<ToggleGroupControl
{ ...props }
onChange={ ( ...changeArgs ) => {
setValue( ...changeArgs );
onChange?.( ...changeArgs );
} }
value={ value }
/>
);
};
const mapPropsToOptionComponent = ( {
value,
...props
}: ToggleGroupControlOptionProps ) => (
<ToggleGroupControlOption value={ value } key={ value } { ...props } />
);
const mapPropsToOptionIconComponent = ( {
value,
...props
}: ToggleGroupControlOptionIconProps ) => (
<ToggleGroupControlOptionIcon value={ value } key={ value } { ...props } />
);
export const Default: ComponentStory< typeof ToggleGroupControl > =
Template.bind( {} );
Default.args = {
children: [
{ value: 'left', label: 'Left' },
{ value: 'center', label: 'Center' },
{ value: 'right', label: 'Right' },
{ value: 'justify', label: 'Justify' },
].map( mapPropsToOptionComponent ),
label: 'Label',
};
/**
* A tooltip can be shown for each option by enabling the `showTooltip` prop.
* The `aria-label` will be used in the tooltip if provided. Otherwise, the
* `label` will be used.
*/
export const WithTooltip: ComponentStory< typeof ToggleGroupControl > =
Template.bind( {} );
WithTooltip.args = {
...Default.args,
children: [
{
value: 'asc',
label: 'A→Z',
'aria-label': 'Ascending',
showTooltip: true,
},
{
value: 'desc',
label: 'Z→A',
'aria-label': 'Descending',
showTooltip: true,
},
].map( mapPropsToOptionComponent ),
};
/**
* The `ToggleGroupControlOptionIcon` component can be used for icon options. A `label` is required
* on each option for accessibility, which will be shown in a tooltip.
*/
export const WithIcons: ComponentStory< typeof ToggleGroupControl > =
Template.bind( {} );
WithIcons.args = {
...Default.args,
children: [
{ value: 'uppercase', label: 'Uppercase', icon: formatUppercase },
{ value: 'lowercase', label: 'Lowercase', icon: formatLowercase },
].map( mapPropsToOptionIconComponent ),
};
/**
* A borderless style may be preferred in some contexts.
*/
export const Borderless: ComponentStory< typeof ToggleGroupControl > =
Template.bind( {} );
Borderless.args = {
...WithIcons.args,
__experimentalIsBorderless: true,
};