@acusti/css-value-input 0.23.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CSSValueInput.js +154 -225
- package/dist/CSSValueInput.js.flow +31 -31
- package/dist/CSSValueInput.js.map +1 -1
- package/package.json +2 -2
- package/src/CSSValueInput.tsx +21 -18
package/dist/CSSValueInput.js
CHANGED
|
@@ -1,234 +1,163 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DEFAULT_CSS_VALUE_TYPE,
|
|
3
|
-
DEFAULT_UNIT_BY_CSS_VALUE_TYPE,
|
|
4
|
-
getCSSValueAsNumber,
|
|
5
|
-
getCSSValueWithUnit,
|
|
6
|
-
getUnitFromCSSValue,
|
|
7
|
-
roundToPrecision,
|
|
8
|
-
} from '@acusti/css-values';
|
|
1
|
+
import { DEFAULT_CSS_VALUE_TYPE, DEFAULT_UNIT_BY_CSS_VALUE_TYPE, getCSSValueAsNumber, getCSSValueWithUnit, getUnitFromCSSValue, roundToPrecision, } from '@acusti/css-values';
|
|
9
2
|
import InputText from '@acusti/input-text';
|
|
10
3
|
import clsx from 'clsx';
|
|
11
4
|
import * as React from 'react';
|
|
12
5
|
const { useCallback, useImperativeHandle, useRef } = React;
|
|
13
6
|
const ROOT_CLASS_NAME = 'cssvalueinput';
|
|
14
|
-
const CSSValueInput = React.forwardRef(
|
|
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
|
-
} else if (validator) {
|
|
70
|
-
isValid = validator(currentValue);
|
|
71
|
-
}
|
|
72
|
-
if (isValid) {
|
|
73
|
-
handleSubmitValue();
|
|
74
|
-
} else {
|
|
75
|
-
// If current value isn’t valid, revert to last submitted value
|
|
76
|
-
inputRef.current.value = submittedValueRef.current;
|
|
77
|
-
}
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
// Normalize value by applying min/max and integer constraints
|
|
81
|
-
let normalizedValueAsNumber = currentValueAsNumber;
|
|
82
|
-
if (isCurrentValueFinite) {
|
|
83
|
-
if (min != null && currentValueAsNumber < min) {
|
|
84
|
-
normalizedValueAsNumber = min;
|
|
85
|
-
} else if (max != null && currentValueAsNumber > max) {
|
|
86
|
-
normalizedValueAsNumber = max;
|
|
87
|
-
} else if (cssValueType === 'integer') {
|
|
88
|
-
normalizedValueAsNumber = Math.floor(currentValueAsNumber);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
if (normalizedValueAsNumber !== currentValueAsNumber) {
|
|
92
|
-
const currentUnit = getUnitFromCSSValue({
|
|
93
|
-
cssValueType,
|
|
94
|
-
defaultUnit: unit,
|
|
95
|
-
value: currentValue,
|
|
96
|
-
});
|
|
97
|
-
inputRef.current.value = normalizedValueAsNumber + currentUnit;
|
|
98
|
-
} else {
|
|
99
|
-
inputRef.current.value = getCSSValueWithUnit({
|
|
100
|
-
cssValueType,
|
|
101
|
-
defaultUnit: unit,
|
|
102
|
-
value: currentValue,
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
handleSubmitValue();
|
|
106
|
-
},
|
|
107
|
-
[
|
|
108
|
-
allowEmpty,
|
|
7
|
+
const CSSValueInput = React.forwardRef(({ allowEmpty = true, className, cssValueType = DEFAULT_CSS_VALUE_TYPE, disabled, getValueAsNumber = getCSSValueAsNumber, icon, label, max, min, name, onBlur, onChange, onFocus, onKeyDown, onKeyUp, onSubmitValue, placeholder, step = 1, tabIndex, title, unit = DEFAULT_UNIT_BY_CSS_VALUE_TYPE[cssValueType], validator, value, }, ref) => {
|
|
8
|
+
const inputRef = useRef(null);
|
|
9
|
+
useImperativeHandle(ref, () => inputRef.current);
|
|
10
|
+
const submittedValueRef = useRef(value || '');
|
|
11
|
+
const handleSubmitValue = useCallback((event) => {
|
|
12
|
+
const currentValue = event.currentTarget.value;
|
|
13
|
+
// Store last submittedValue (used to reset value on invalid input)
|
|
14
|
+
submittedValueRef.current = currentValue;
|
|
15
|
+
onSubmitValue(currentValue);
|
|
16
|
+
}, [onSubmitValue]);
|
|
17
|
+
const handleBlur = useCallback((event) => {
|
|
18
|
+
const input = event.currentTarget;
|
|
19
|
+
inputRef.current = input;
|
|
20
|
+
if (onBlur)
|
|
21
|
+
onBlur(event);
|
|
22
|
+
const currentValue = input.value.trim();
|
|
23
|
+
// If allowEmpty and value is empty, skip all validation + normalization
|
|
24
|
+
if (allowEmpty && !currentValue) {
|
|
25
|
+
handleSubmitValue(event);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
29
|
+
const isCurrentValueFinite = Number.isFinite(currentValueAsNumber);
|
|
30
|
+
if (!isCurrentValueFinite) {
|
|
31
|
+
let isValid = false;
|
|
32
|
+
if (validator instanceof RegExp) {
|
|
33
|
+
isValid = validator.test(currentValue);
|
|
34
|
+
}
|
|
35
|
+
else if (validator) {
|
|
36
|
+
isValid = validator(currentValue);
|
|
37
|
+
}
|
|
38
|
+
if (isValid) {
|
|
39
|
+
handleSubmitValue(event);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// If current value isn’t valid, revert to last submitted value
|
|
43
|
+
input.value = submittedValueRef.current;
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
// Normalize value by applying min/max and integer constraints
|
|
48
|
+
let normalizedValueAsNumber = currentValueAsNumber;
|
|
49
|
+
if (isCurrentValueFinite) {
|
|
50
|
+
if (min != null && currentValueAsNumber < min) {
|
|
51
|
+
normalizedValueAsNumber = min;
|
|
52
|
+
}
|
|
53
|
+
else if (max != null && currentValueAsNumber > max) {
|
|
54
|
+
normalizedValueAsNumber = max;
|
|
55
|
+
}
|
|
56
|
+
else if (cssValueType === 'integer') {
|
|
57
|
+
normalizedValueAsNumber = Math.floor(currentValueAsNumber);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (normalizedValueAsNumber !== currentValueAsNumber) {
|
|
61
|
+
const currentUnit = getUnitFromCSSValue({
|
|
109
62
|
cssValueType,
|
|
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
|
-
|
|
63
|
+
defaultUnit: unit,
|
|
64
|
+
value: currentValue,
|
|
65
|
+
});
|
|
66
|
+
input.value = normalizedValueAsNumber + currentUnit;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
input.value = getCSSValueWithUnit({
|
|
70
|
+
cssValueType,
|
|
71
|
+
defaultUnit: unit,
|
|
72
|
+
value: currentValue,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
handleSubmitValue(event);
|
|
76
|
+
}, [
|
|
77
|
+
allowEmpty,
|
|
78
|
+
cssValueType,
|
|
79
|
+
getValueAsNumber,
|
|
80
|
+
handleSubmitValue,
|
|
81
|
+
max,
|
|
82
|
+
min,
|
|
83
|
+
onBlur,
|
|
84
|
+
unit,
|
|
85
|
+
validator,
|
|
86
|
+
]);
|
|
87
|
+
const getNextValue = useCallback(({ currentValue, multiplier = 1, signum = 1, }) => {
|
|
88
|
+
const modifier = multiplier * step * signum;
|
|
89
|
+
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
90
|
+
// If currentValue isn’t numeric, don’t try to increment/decrement it
|
|
91
|
+
if (typeof currentValue === 'string' &&
|
|
92
|
+
Number.isNaN(currentValueAsNumber)) {
|
|
93
|
+
return currentValue;
|
|
94
|
+
}
|
|
95
|
+
let nextValue = currentValueAsNumber + modifier;
|
|
96
|
+
if (cssValueType === 'integer') {
|
|
97
|
+
nextValue = Math.floor(nextValue);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
nextValue = roundToPrecision(nextValue, 5);
|
|
101
|
+
}
|
|
102
|
+
if (typeof max === 'number' && Number.isFinite(max)) {
|
|
103
|
+
nextValue = Math.min(max, nextValue);
|
|
104
|
+
}
|
|
105
|
+
if (typeof min === 'number' && Number.isFinite(min)) {
|
|
106
|
+
nextValue = Math.max(min, nextValue);
|
|
107
|
+
}
|
|
108
|
+
const nextUnit = getUnitFromCSSValue({
|
|
109
|
+
cssValueType,
|
|
110
|
+
defaultUnit: unit,
|
|
111
|
+
value: currentValue,
|
|
112
|
+
});
|
|
113
|
+
return `${nextValue}${nextUnit}`;
|
|
114
|
+
}, [cssValueType, getValueAsNumber, max, min, step, unit]);
|
|
115
|
+
const handleKeyDown = useCallback((event) => {
|
|
116
|
+
const input = event.currentTarget;
|
|
117
|
+
inputRef.current = input;
|
|
118
|
+
if (onKeyDown)
|
|
119
|
+
onKeyDown(event);
|
|
120
|
+
const currentValue = input.value || placeholder || `0${unit}`;
|
|
121
|
+
let nextValue = '';
|
|
122
|
+
switch (event.key) {
|
|
123
|
+
case 'Escape':
|
|
124
|
+
case 'Enter':
|
|
125
|
+
if (event.key === 'Escape') {
|
|
126
|
+
input.value = submittedValueRef.current;
|
|
141
127
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
128
|
+
input.blur();
|
|
129
|
+
return;
|
|
130
|
+
case 'ArrowUp':
|
|
131
|
+
case 'ArrowDown':
|
|
132
|
+
nextValue = getNextValue({
|
|
133
|
+
currentValue,
|
|
134
|
+
multiplier: event.shiftKey ? 10 : 1,
|
|
135
|
+
signum: event.key === 'ArrowUp' ? 1 : -1,
|
|
146
136
|
});
|
|
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
|
-
if (nextValue === currentValue) return;
|
|
173
|
-
event.stopPropagation();
|
|
174
|
-
event.preventDefault();
|
|
175
|
-
input.value = nextValue;
|
|
176
|
-
return;
|
|
177
|
-
default:
|
|
178
|
-
// No default key handling
|
|
179
|
-
}
|
|
180
|
-
},
|
|
181
|
-
[getNextValue, onKeyDown, placeholder, unit],
|
|
182
|
-
);
|
|
183
|
-
const handleKeyUp = useCallback(
|
|
184
|
-
(event) => {
|
|
185
|
-
if (onKeyUp) onKeyUp(event);
|
|
186
|
-
// If this is the key up from ↑ or ↓ keys, time to handleSubmitValue
|
|
187
|
-
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
188
|
-
handleSubmitValue();
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
|
-
[handleSubmitValue, onKeyUp],
|
|
192
|
-
);
|
|
193
|
-
return React.createElement(
|
|
194
|
-
'label',
|
|
195
|
-
{ className: clsx(ROOT_CLASS_NAME, className, { disabled }), title: title },
|
|
196
|
-
icon &&
|
|
197
|
-
React.createElement(
|
|
198
|
-
'div',
|
|
199
|
-
{ className: `${ROOT_CLASS_NAME}-icon` },
|
|
200
|
-
icon,
|
|
201
|
-
),
|
|
202
|
-
label &&
|
|
203
|
-
React.createElement(
|
|
204
|
-
'div',
|
|
205
|
-
{ className: `${ROOT_CLASS_NAME}-label` },
|
|
206
|
-
React.createElement(
|
|
207
|
-
'p',
|
|
208
|
-
{ className: `${ROOT_CLASS_NAME}-label-text` },
|
|
209
|
-
label,
|
|
210
|
-
),
|
|
211
|
-
),
|
|
212
|
-
React.createElement(
|
|
213
|
-
'div',
|
|
214
|
-
{ className: `${ROOT_CLASS_NAME}-value` },
|
|
215
|
-
React.createElement(InputText, {
|
|
216
|
-
disabled: disabled,
|
|
217
|
-
initialValue: value,
|
|
218
|
-
name: name,
|
|
219
|
-
onBlur: handleBlur,
|
|
220
|
-
onChange: onChange,
|
|
221
|
-
onFocus: onFocus,
|
|
222
|
-
onKeyDown: handleKeyDown,
|
|
223
|
-
onKeyUp: handleKeyUp,
|
|
224
|
-
placeholder: placeholder,
|
|
225
|
-
ref: inputRef,
|
|
226
|
-
selectTextOnFocus: true,
|
|
227
|
-
tabIndex: tabIndex,
|
|
228
|
-
}),
|
|
229
|
-
),
|
|
230
|
-
);
|
|
231
|
-
},
|
|
232
|
-
);
|
|
137
|
+
if (nextValue === currentValue)
|
|
138
|
+
return;
|
|
139
|
+
event.stopPropagation();
|
|
140
|
+
event.preventDefault();
|
|
141
|
+
input.value = nextValue;
|
|
142
|
+
return;
|
|
143
|
+
default:
|
|
144
|
+
// No default key handling
|
|
145
|
+
}
|
|
146
|
+
}, [getNextValue, onKeyDown, placeholder, unit]);
|
|
147
|
+
const handleKeyUp = useCallback((event) => {
|
|
148
|
+
if (onKeyUp)
|
|
149
|
+
onKeyUp(event);
|
|
150
|
+
// If this is the key up from ↑ or ↓ keys, time to handleSubmitValue
|
|
151
|
+
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
152
|
+
handleSubmitValue(event);
|
|
153
|
+
}
|
|
154
|
+
}, [handleSubmitValue, onKeyUp]);
|
|
155
|
+
return (React.createElement("label", { className: clsx(ROOT_CLASS_NAME, className, { disabled }), title: title },
|
|
156
|
+
icon && React.createElement("div", { className: `${ROOT_CLASS_NAME}-icon` }, icon),
|
|
157
|
+
label && (React.createElement("div", { className: `${ROOT_CLASS_NAME}-label` },
|
|
158
|
+
React.createElement("p", { className: `${ROOT_CLASS_NAME}-label-text` }, label))),
|
|
159
|
+
React.createElement("div", { className: `${ROOT_CLASS_NAME}-value` },
|
|
160
|
+
React.createElement(InputText, { disabled: disabled, initialValue: value, name: name, onBlur: handleBlur, onChange: onChange, onFocus: onFocus, onKeyDown: handleKeyDown, onKeyUp: handleKeyUp, placeholder: placeholder, ref: inputRef, selectTextOnFocus: true, tabIndex: tabIndex }))));
|
|
161
|
+
});
|
|
233
162
|
export default CSSValueInput;
|
|
234
|
-
//# sourceMappingURL=CSSValueInput.js.map
|
|
163
|
+
//# sourceMappingURL=CSSValueInput.js.map
|
|
@@ -5,39 +5,39 @@
|
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import * as React from
|
|
9
|
-
import type { CSSValueType } from
|
|
8
|
+
import * as React from "react";
|
|
9
|
+
import type { CSSValueType } from "@acusti/css-values";
|
|
10
10
|
export type Props = {|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Boolean indicating if the user can submit an empty value (i.e. clear the value); defaults to true
|
|
13
|
+
*/
|
|
14
|
+
allowEmpty?: boolean,
|
|
15
|
+
className?: string,
|
|
16
|
+
cssValueType?: CSSValueType,
|
|
17
|
+
disabled?: boolean,
|
|
18
|
+
getValueAsNumber?: (value: string | number) => number,
|
|
19
|
+
icon?: React.Node,
|
|
20
|
+
label?: string,
|
|
21
|
+
max?: number,
|
|
22
|
+
min?: number,
|
|
23
|
+
name?: string,
|
|
24
|
+
onBlur?: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed,
|
|
25
|
+
onChange?: (event: SyntheticInputEvent<HTMLInputElement>) => mixed,
|
|
26
|
+
onFocus?: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed,
|
|
27
|
+
onKeyDown?: (event: SyntheticKeyboardEvent<HTMLInputElement>) => mixed,
|
|
28
|
+
onKeyUp?: (event: SyntheticKeyboardEvent<HTMLInputElement>) => mixed,
|
|
29
|
+
onSubmitValue: (value: string) => mixed,
|
|
30
|
+
placeholder?: string,
|
|
31
|
+
step?: number,
|
|
32
|
+
tabIndex?: number,
|
|
33
|
+
title?: string,
|
|
34
|
+
unit?: string,
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Regex or validator function to validate non-numeric values
|
|
38
|
+
*/
|
|
39
|
+
validator?: RegExp | ((value: string) => boolean),
|
|
40
|
+
value?: string,
|
|
41
41
|
|};
|
|
42
42
|
declare var CSSValueInput: React.StatelessFunctionalComponent<Props>;
|
|
43
43
|
declare export default typeof CSSValueInput;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CSSValueInput.js","sourceRoot":"","sources":["../src/CSSValueInput.tsx"],"names":[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B,YAAY,EAAE,KAAK,EACnB,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,UAAU,EAClB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,aAAa,EACxB,OAAO,EAAE,WAAW,EACpB,WAAW,EAAE,WAAW,EACxB,GAAG,EAAE,QAAQ,EACb,iBAAiB,QACjB,QAAQ,EAAE,QAAQ,GACpB,CACA,CACF,CACX,CAAC;AACN,CAAC,CACJ,CAAC;AAEF,eAAe,aAAa,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acusti/css-value-input",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "React component that renders a text input that can take and update a CSS value of a particular type with a default unit",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@acusti/css-values": "^0.19.0",
|
|
44
|
-
"@acusti/input-text": "^1.0
|
|
44
|
+
"@acusti/input-text": "^1.2.0",
|
|
45
45
|
"clsx": "^1.2.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
package/src/CSSValueInput.tsx
CHANGED
|
@@ -80,25 +80,27 @@ const CSSValueInput: React.FC<Props> = React.forwardRef<HTMLInputElement, Props>
|
|
|
80
80
|
|
|
81
81
|
const submittedValueRef = useRef<string>(value || '');
|
|
82
82
|
|
|
83
|
-
const handleSubmitValue = useCallback(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
const handleSubmitValue = useCallback(
|
|
84
|
+
(event: React.SyntheticEvent<HTMLInputElement>) => {
|
|
85
|
+
const currentValue = event.currentTarget.value;
|
|
86
|
+
// Store last submittedValue (used to reset value on invalid input)
|
|
87
|
+
submittedValueRef.current = currentValue;
|
|
88
|
+
onSubmitValue(currentValue);
|
|
89
|
+
},
|
|
90
|
+
[onSubmitValue],
|
|
91
|
+
);
|
|
91
92
|
|
|
92
93
|
const handleBlur = useCallback(
|
|
93
94
|
(event: React.FocusEvent<HTMLInputElement>) => {
|
|
95
|
+
const input = event.currentTarget;
|
|
96
|
+
inputRef.current = input;
|
|
94
97
|
if (onBlur) onBlur(event);
|
|
95
|
-
if (!inputRef.current) return;
|
|
96
98
|
|
|
97
|
-
const currentValue =
|
|
99
|
+
const currentValue = input.value.trim();
|
|
98
100
|
|
|
99
101
|
// If allowEmpty and value is empty, skip all validation + normalization
|
|
100
102
|
if (allowEmpty && !currentValue) {
|
|
101
|
-
handleSubmitValue();
|
|
103
|
+
handleSubmitValue(event);
|
|
102
104
|
return;
|
|
103
105
|
}
|
|
104
106
|
|
|
@@ -114,10 +116,10 @@ const CSSValueInput: React.FC<Props> = React.forwardRef<HTMLInputElement, Props>
|
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
if (isValid) {
|
|
117
|
-
handleSubmitValue();
|
|
119
|
+
handleSubmitValue(event);
|
|
118
120
|
} else {
|
|
119
121
|
// If current value isn’t valid, revert to last submitted value
|
|
120
|
-
|
|
122
|
+
input.value = submittedValueRef.current;
|
|
121
123
|
}
|
|
122
124
|
|
|
123
125
|
return;
|
|
@@ -142,16 +144,16 @@ const CSSValueInput: React.FC<Props> = React.forwardRef<HTMLInputElement, Props>
|
|
|
142
144
|
defaultUnit: unit,
|
|
143
145
|
value: currentValue,
|
|
144
146
|
});
|
|
145
|
-
|
|
147
|
+
input.value = normalizedValueAsNumber + currentUnit;
|
|
146
148
|
} else {
|
|
147
|
-
|
|
149
|
+
input.value = getCSSValueWithUnit({
|
|
148
150
|
cssValueType,
|
|
149
151
|
defaultUnit: unit,
|
|
150
152
|
value: currentValue,
|
|
151
153
|
});
|
|
152
154
|
}
|
|
153
155
|
|
|
154
|
-
handleSubmitValue();
|
|
156
|
+
handleSubmitValue(event);
|
|
155
157
|
},
|
|
156
158
|
[
|
|
157
159
|
allowEmpty,
|
|
@@ -213,9 +215,10 @@ const CSSValueInput: React.FC<Props> = React.forwardRef<HTMLInputElement, Props>
|
|
|
213
215
|
|
|
214
216
|
const handleKeyDown = useCallback(
|
|
215
217
|
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
218
|
+
const input = event.currentTarget;
|
|
219
|
+
inputRef.current = input;
|
|
216
220
|
if (onKeyDown) onKeyDown(event);
|
|
217
221
|
|
|
218
|
-
const input = event.currentTarget;
|
|
219
222
|
const currentValue = input.value || placeholder || `0${unit}`;
|
|
220
223
|
let nextValue = '';
|
|
221
224
|
|
|
@@ -254,7 +257,7 @@ const CSSValueInput: React.FC<Props> = React.forwardRef<HTMLInputElement, Props>
|
|
|
254
257
|
if (onKeyUp) onKeyUp(event);
|
|
255
258
|
// If this is the key up from ↑ or ↓ keys, time to handleSubmitValue
|
|
256
259
|
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
257
|
-
handleSubmitValue();
|
|
260
|
+
handleSubmitValue(event);
|
|
258
261
|
}
|
|
259
262
|
},
|
|
260
263
|
[handleSubmitValue, onKeyUp],
|