-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.story.tsx
149 lines (138 loc) · 3.13 KB
/
index.story.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
/**
* External dependencies
*/
import type { Meta, StoryFn } from '@storybook/react';
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { UnitControl } from '../';
import { CSS_UNITS } from '../utils';
const meta: Meta< typeof UnitControl > = {
component: UnitControl,
title: 'Components (Experimental)/Selection & Input/UnitControl',
id: 'components-experimental-unitcontrol',
argTypes: {
__unstableInputWidth: { control: { type: 'text' } },
__unstableStateReducer: { control: false },
onChange: { control: false },
onUnitChange: { control: false },
prefix: { control: { type: 'text' } },
value: { control: false },
},
parameters: {
actions: { argTypesRegex: '^on.*' },
controls: {
expanded: true,
},
docs: { canvas: { sourceState: 'shown' } },
},
};
export default meta;
const DefaultTemplate: StoryFn< typeof UnitControl > = ( {
onChange,
...args
} ) => {
const [ value, setValue ] = useState< string | undefined >( '10px' );
return (
<UnitControl
{ ...args }
value={ value }
onChange={ ( v, extra ) => {
setValue( v );
onChange?.( v, extra );
} }
/>
);
};
export const Default: StoryFn< typeof UnitControl > = DefaultTemplate.bind(
{}
);
Default.args = {
label: 'Label',
__next40pxDefaultSize: true,
};
/**
* If the `isPressEnterToChange` prop is set to `true`, the `onChange` callback
* will not fire while a new value is typed in the input field (you can verify this
* behavior by inspecting the console's output).
*/
export const PressEnterToChange: StoryFn< typeof UnitControl > =
DefaultTemplate.bind( {} );
PressEnterToChange.args = {
...Default.args,
isPressEnterToChange: true,
};
/**
* Most of `NumberControl`'s props can be passed to `UnitControl`, and they will
* affect its numeric input field.
*/
export const TweakingTheNumberInput: StoryFn< typeof UnitControl > =
DefaultTemplate.bind( {} );
TweakingTheNumberInput.args = {
...Default.args,
min: 0,
max: 100,
step: 'any',
label: 'Custom label',
};
/**
* When only one unit is available, the unit selection dropdown becomes static text.
*/
export const WithSingleUnit: StoryFn< typeof UnitControl > =
DefaultTemplate.bind( {} );
WithSingleUnit.args = {
...Default.args,
units: CSS_UNITS.slice( 0, 1 ),
};
/**
* It is possible to pass a custom list of units. Every time the unit changes,
* if the `isResetValueOnUnitChange` is set to `true`, the input's quantity is
* reset to the new unit's default value.
*/
export const WithCustomUnits: StoryFn< typeof UnitControl > = ( {
onChange,
...args
} ) => {
const [ value, setValue ] = useState< string | undefined >( '80km' );
return (
<UnitControl
{ ...args }
value={ value }
onChange={ ( v, extra ) => {
setValue( v );
onChange?.( v, extra );
} }
/>
);
};
WithCustomUnits.args = {
...Default.args,
isResetValueOnUnitChange: true,
min: 0,
units: [
{
value: 'km',
label: 'km',
default: 1,
},
{
value: 'mi',
label: 'mi',
default: 1,
},
{
value: 'm',
label: 'm',
default: 1000,
},
{
value: 'yd',
label: 'yd',
default: 1760,
},
],
};