-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.js
202 lines (174 loc) · 5.05 KB
/
index.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* External dependencies
*/
import { noop, omit } from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { forwardRef, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { ENTER } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import * as inputControlActionTypes from '../input-control/reducer/actions';
import { composeStateReducers } from '../input-control/reducer/reducer';
import { Root, ValueInput } from './styles/unit-control-styles';
import UnitSelectControl from './unit-select-control';
import { CSS_UNITS, getParsedValue, getValidParsedUnit } from './utils';
import { useControlledState } from '../utils/hooks';
function UnitControl(
{
__unstableStateReducer: stateReducer = ( state ) => state,
autoComplete = 'off',
className,
disabled = false,
disableUnits = false,
isPressEnterToChange = false,
isResetValueOnUnitChange = false,
isUnitSelectTabbable = true,
label,
onChange = noop,
onUnitChange = noop,
size = 'default',
style,
unit: unitProp,
units = CSS_UNITS,
value: valueProp,
...props
},
ref
) {
const [ value, initialUnit ] = getParsedValue( valueProp, unitProp, units );
const [ unit, setUnit ] = useControlledState( unitProp, {
initial: initialUnit,
} );
// Stores parsed value for hand-off in state reducer
const refParsedValue = useRef( null );
const classes = classnames( 'components-unit-control', className );
const handleOnChange = ( next, changeProps ) => {
if ( next === '' ) {
onChange( '', changeProps );
return;
}
/*
* Customizing the onChange callback.
* This allows as to broadcast a combined value+unit to onChange.
*/
next = getValidParsedUnit( next, units, value, unit ).join( '' );
onChange( next, changeProps );
};
const handleOnUnitChange = ( next, changeProps ) => {
const { data } = changeProps;
let nextValue = `${ value }${ next }`;
if ( isResetValueOnUnitChange && data?.default !== undefined ) {
nextValue = `${ data.default }${ next }`;
}
onChange( nextValue, changeProps );
onUnitChange( next, changeProps );
setUnit( next );
};
const mayUpdateUnit = ( event ) => {
if ( ! isNaN( event.target.value ) ) {
refParsedValue.current = null;
return;
}
const [ parsedValue, parsedUnit ] = getValidParsedUnit(
event.target.value,
units,
value,
unit
);
refParsedValue.current = parsedValue;
if ( isPressEnterToChange && parsedUnit !== unit ) {
const data = units.find(
( option ) => option.value === parsedUnit
);
const changeProps = { event, data };
onChange( `${ parsedValue }${ parsedUnit }`, changeProps );
onUnitChange( parsedUnit, changeProps );
setUnit( parsedUnit );
}
};
const handleOnBlur = mayUpdateUnit;
const handleOnKeyDown = ( event ) => {
const { keyCode } = event;
if ( keyCode === ENTER ) {
mayUpdateUnit( event );
}
};
/**
* "Middleware" function that intercepts updates from InputControl.
* This allows us to tap into actions to transform the (next) state for
* InputControl.
*
* @param {Object} state State from InputControl
* @param {Object} action Action triggering state change
* @return {Object} The updated state to apply to InputControl
*/
const unitControlStateReducer = ( state, action ) => {
/*
* On commits (when pressing ENTER and on blur if
* isPressEnterToChange is true), if a parse has been performed
* then use that result to update the state.
*/
if ( action.type === inputControlActionTypes.COMMIT ) {
if ( refParsedValue.current !== null ) {
state.value = refParsedValue.current;
refParsedValue.current = null;
}
}
return state;
};
const inputSuffix = ! disableUnits ? (
<UnitSelectControl
aria-label={ __( 'Select unit' ) }
disabled={ disabled }
isTabbable={ isUnitSelectTabbable }
options={ units }
onChange={ handleOnUnitChange }
size={ size }
value={ unit }
/>
) : null;
let step = props.step;
/*
* If no step prop has been passed, lookup the active unit and
* try to get step from `units`, or default to a value of `1`
*/
if ( ! step && units ) {
const activeUnit = units.find( ( option ) => option.value === unit );
step = activeUnit?.step ?? 1;
}
return (
<Root className="components-unit-control-wrapper" style={ style }>
<ValueInput
aria-label={ label }
type={ isPressEnterToChange ? 'text' : 'number' }
{ ...omit( props, [ 'children' ] ) }
autoComplete={ autoComplete }
className={ classes }
disabled={ disabled }
disableUnits={ disableUnits }
isPressEnterToChange={ isPressEnterToChange }
label={ label }
onBlur={ handleOnBlur }
onKeyDown={ handleOnKeyDown }
onChange={ handleOnChange }
ref={ ref }
size={ size }
suffix={ inputSuffix }
value={ value }
step={ step }
__unstableStateReducer={ composeStateReducers(
unitControlStateReducer,
stateReducer
) }
/>
</Root>
);
}
const ForwardedUnitControl = forwardRef( UnitControl );
export { parseUnit, useCustomUnits } from './utils';
export default ForwardedUnitControl;