@acusti/css-value-input 1.0.1 → 1.1.1
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/README.md +1 -1
- package/dist/CSSValueInput.d.ts +1 -3
- package/dist/CSSValueInput.js +151 -228
- package/dist/CSSValueInput.js.flow +44 -44
- package/dist/CSSValueInput.test.js +11 -58
- package/dist/CSSValueInput.test.js.flow +1 -1
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Illustrator.
|
|
|
13
13
|
See the [storybook docs and demo][] to get a feel for what it can do.
|
|
14
14
|
|
|
15
15
|
[storybook docs and demo]:
|
|
16
|
-
https://
|
|
16
|
+
https://uikit.acusti.ca/?path=/docs/uikit-controls-CSSValueInput--docs
|
|
17
17
|
|
|
18
18
|
## Usage
|
|
19
19
|
|
package/dist/CSSValueInput.d.ts
CHANGED
|
@@ -39,7 +39,5 @@ export type Props = {
|
|
|
39
39
|
validator?: ((value: string) => boolean) | RegExp;
|
|
40
40
|
value?: string;
|
|
41
41
|
};
|
|
42
|
-
declare const _default: React.ForwardRefExoticComponent<
|
|
43
|
-
Props & React.RefAttributes<HTMLInputElement>
|
|
44
|
-
>;
|
|
42
|
+
declare const _default: React.ForwardRefExoticComponent<Props & React.RefAttributes<HTMLInputElement>>;
|
|
45
43
|
export default _default;
|
package/dist/CSSValueInput.js
CHANGED
|
@@ -1,44 +1,10 @@
|
|
|
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, useEffect, useImperativeHandle, useRef } = React;
|
|
13
6
|
const ROOT_CLASS_NAME = 'cssvalueinput';
|
|
14
|
-
export default React.forwardRef(function CSSValueInput(
|
|
15
|
-
{
|
|
16
|
-
allowEmpty = true,
|
|
17
|
-
className,
|
|
18
|
-
cssValueType = DEFAULT_CSS_VALUE_TYPE,
|
|
19
|
-
disabled,
|
|
20
|
-
getValueAsNumber = getCSSValueAsNumber,
|
|
21
|
-
icon,
|
|
22
|
-
label,
|
|
23
|
-
max,
|
|
24
|
-
min,
|
|
25
|
-
name,
|
|
26
|
-
onBlur,
|
|
27
|
-
onChange,
|
|
28
|
-
onFocus,
|
|
29
|
-
onKeyDown,
|
|
30
|
-
onKeyUp,
|
|
31
|
-
onSubmitValue,
|
|
32
|
-
placeholder,
|
|
33
|
-
step = 1,
|
|
34
|
-
tabIndex,
|
|
35
|
-
title,
|
|
36
|
-
unit = DEFAULT_UNIT_BY_CSS_VALUE_TYPE[cssValueType],
|
|
37
|
-
validator,
|
|
38
|
-
value,
|
|
39
|
-
},
|
|
40
|
-
ref,
|
|
41
|
-
) {
|
|
7
|
+
export default React.forwardRef(function CSSValueInput({ 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) {
|
|
42
8
|
const inputRef = useRef(null);
|
|
43
9
|
useImperativeHandle(ref, () => inputRef.current);
|
|
44
10
|
// props.value should be a string; if it’s a number, convert it here
|
|
@@ -49,207 +15,164 @@ export default React.forwardRef(function CSSValueInput(
|
|
|
49
15
|
useEffect(() => {
|
|
50
16
|
submittedValueRef.current = value !== null && value !== void 0 ? value : '';
|
|
51
17
|
}, [value]);
|
|
52
|
-
const handleSubmitValue = useCallback(
|
|
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
|
-
let isValid = false;
|
|
85
|
-
if (validator instanceof RegExp) {
|
|
86
|
-
isValid = validator.test(currentValue);
|
|
87
|
-
} else if (validator) {
|
|
88
|
-
isValid = validator(currentValue);
|
|
89
|
-
}
|
|
90
|
-
if (isValid) {
|
|
91
|
-
handleSubmitValue(event);
|
|
92
|
-
} else {
|
|
93
|
-
// If current value isn’t valid, revert to last submitted value
|
|
94
|
-
input.value = submittedValueRef.current;
|
|
95
|
-
}
|
|
96
|
-
return;
|
|
18
|
+
const handleSubmitValue = useCallback((event) => {
|
|
19
|
+
const currentValue = event.currentTarget.value;
|
|
20
|
+
// Store last submittedValue (used to reset value on invalid input)
|
|
21
|
+
submittedValueRef.current = currentValue;
|
|
22
|
+
onSubmitValue(currentValue);
|
|
23
|
+
}, [onSubmitValue]);
|
|
24
|
+
const handleBlur = useCallback((event) => {
|
|
25
|
+
const input = event.currentTarget;
|
|
26
|
+
inputRef.current = input;
|
|
27
|
+
if (onBlur)
|
|
28
|
+
onBlur(event);
|
|
29
|
+
const currentValue = input.value.trim();
|
|
30
|
+
// If allowEmpty and value is empty, skip all validation + normalization
|
|
31
|
+
if (allowEmpty && !currentValue) {
|
|
32
|
+
handleSubmitValue(event);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
36
|
+
const isCurrentValueFinite = Number.isFinite(currentValueAsNumber);
|
|
37
|
+
// Inherit unit from last submitted value unless default is unitless;
|
|
38
|
+
// ensures that submitting a new value with no unit doesn’t add a unit
|
|
39
|
+
const defaultUnit = unit
|
|
40
|
+
? getUnitFromCSSValue({
|
|
41
|
+
cssValueType,
|
|
42
|
+
defaultUnit: unit,
|
|
43
|
+
value: submittedValueRef.current,
|
|
44
|
+
})
|
|
45
|
+
: '';
|
|
46
|
+
if (!isCurrentValueFinite) {
|
|
47
|
+
let isValid = false;
|
|
48
|
+
if (validator instanceof RegExp) {
|
|
49
|
+
isValid = validator.test(currentValue);
|
|
97
50
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (isCurrentValueFinite) {
|
|
101
|
-
if (min != null && currentValueAsNumber < min) {
|
|
102
|
-
normalizedValueAsNumber = min;
|
|
103
|
-
} else if (max != null && currentValueAsNumber > max) {
|
|
104
|
-
normalizedValueAsNumber = max;
|
|
105
|
-
} else if (cssValueType === 'integer') {
|
|
106
|
-
normalizedValueAsNumber = Math.floor(currentValueAsNumber);
|
|
107
|
-
}
|
|
51
|
+
else if (validator) {
|
|
52
|
+
isValid = validator(currentValue);
|
|
108
53
|
}
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
cssValueType,
|
|
112
|
-
defaultUnit,
|
|
113
|
-
value: currentValue,
|
|
114
|
-
});
|
|
115
|
-
input.value = normalizedValueAsNumber + currentUnit;
|
|
116
|
-
} else {
|
|
117
|
-
input.value = getCSSValueWithUnit({
|
|
118
|
-
cssValueType,
|
|
119
|
-
defaultUnit,
|
|
120
|
-
value: currentValue,
|
|
121
|
-
});
|
|
54
|
+
if (isValid) {
|
|
55
|
+
handleSubmitValue(event);
|
|
122
56
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
allowEmpty,
|
|
127
|
-
cssValueType,
|
|
128
|
-
getValueAsNumber,
|
|
129
|
-
handleSubmitValue,
|
|
130
|
-
max,
|
|
131
|
-
min,
|
|
132
|
-
onBlur,
|
|
133
|
-
unit,
|
|
134
|
-
validator,
|
|
135
|
-
],
|
|
136
|
-
);
|
|
137
|
-
const getNextValue = useCallback(
|
|
138
|
-
({ currentValue, multiplier = 1, signum = 1 }) => {
|
|
139
|
-
const modifier = multiplier * step * signum;
|
|
140
|
-
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
141
|
-
// If currentValue isn’t numeric, don’t try to increment/decrement it
|
|
142
|
-
if (typeof currentValue === 'string' && Number.isNaN(currentValueAsNumber)) {
|
|
143
|
-
return currentValue;
|
|
57
|
+
else {
|
|
58
|
+
// If current value isn’t valid, revert to last submitted value
|
|
59
|
+
input.value = submittedValueRef.current;
|
|
144
60
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// Normalize value by applying min/max and integer constraints
|
|
64
|
+
let normalizedValueAsNumber = currentValueAsNumber;
|
|
65
|
+
if (isCurrentValueFinite) {
|
|
66
|
+
if (min != null && currentValueAsNumber < min) {
|
|
67
|
+
normalizedValueAsNumber = min;
|
|
150
68
|
}
|
|
151
|
-
if (
|
|
152
|
-
|
|
69
|
+
else if (max != null && currentValueAsNumber > max) {
|
|
70
|
+
normalizedValueAsNumber = max;
|
|
153
71
|
}
|
|
154
|
-
if (
|
|
155
|
-
|
|
72
|
+
else if (cssValueType === 'integer') {
|
|
73
|
+
normalizedValueAsNumber = Math.floor(currentValueAsNumber);
|
|
156
74
|
}
|
|
157
|
-
|
|
75
|
+
}
|
|
76
|
+
if (normalizedValueAsNumber !== currentValueAsNumber) {
|
|
77
|
+
const currentUnit = getUnitFromCSSValue({
|
|
158
78
|
cssValueType,
|
|
159
|
-
defaultUnit
|
|
79
|
+
defaultUnit,
|
|
160
80
|
value: currentValue,
|
|
161
81
|
});
|
|
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
|
-
|
|
82
|
+
input.value = normalizedValueAsNumber + currentUnit;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
input.value = getCSSValueWithUnit({
|
|
86
|
+
cssValueType,
|
|
87
|
+
defaultUnit,
|
|
88
|
+
value: currentValue,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
handleSubmitValue(event);
|
|
92
|
+
}, [
|
|
93
|
+
allowEmpty,
|
|
94
|
+
cssValueType,
|
|
95
|
+
getValueAsNumber,
|
|
96
|
+
handleSubmitValue,
|
|
97
|
+
max,
|
|
98
|
+
min,
|
|
99
|
+
onBlur,
|
|
100
|
+
unit,
|
|
101
|
+
validator,
|
|
102
|
+
]);
|
|
103
|
+
const getNextValue = useCallback(({ currentValue, multiplier = 1, signum = 1, }) => {
|
|
104
|
+
const modifier = multiplier * step * signum;
|
|
105
|
+
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
106
|
+
// If currentValue isn’t numeric, don’t try to increment/decrement it
|
|
107
|
+
if (typeof currentValue === 'string' && Number.isNaN(currentValueAsNumber)) {
|
|
108
|
+
return currentValue;
|
|
109
|
+
}
|
|
110
|
+
let nextValue = currentValueAsNumber + modifier;
|
|
111
|
+
if (cssValueType === 'integer') {
|
|
112
|
+
nextValue = Math.floor(nextValue);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
nextValue = roundToPrecision(nextValue, 5);
|
|
116
|
+
}
|
|
117
|
+
if (typeof max === 'number' && Number.isFinite(max)) {
|
|
118
|
+
nextValue = Math.min(max, nextValue);
|
|
119
|
+
}
|
|
120
|
+
if (typeof min === 'number' && Number.isFinite(min)) {
|
|
121
|
+
nextValue = Math.max(min, nextValue);
|
|
122
|
+
}
|
|
123
|
+
const nextUnit = getUnitFromCSSValue({
|
|
124
|
+
cssValueType,
|
|
125
|
+
defaultUnit: unit,
|
|
126
|
+
value: currentValue,
|
|
127
|
+
});
|
|
128
|
+
return `${nextValue}${nextUnit}`;
|
|
129
|
+
}, [cssValueType, getValueAsNumber, max, min, step, unit]);
|
|
130
|
+
const handleKeyDown = useCallback((event) => {
|
|
131
|
+
var _a, _b;
|
|
132
|
+
const input = event.currentTarget;
|
|
133
|
+
inputRef.current = input;
|
|
134
|
+
if (onKeyDown)
|
|
135
|
+
onKeyDown(event);
|
|
136
|
+
const currentValue = (_b = (_a = input.value) !== null && _a !== void 0 ? _a : placeholder) !== null && _b !== void 0 ? _b : `0${unit}`;
|
|
137
|
+
let nextValue = '';
|
|
138
|
+
switch (event.key) {
|
|
139
|
+
case 'ArrowDown':
|
|
140
|
+
case 'ArrowUp':
|
|
141
|
+
nextValue = getNextValue({
|
|
142
|
+
currentValue,
|
|
143
|
+
multiplier: event.shiftKey ? 10 : 1,
|
|
144
|
+
signum: event.key === 'ArrowUp' ? 1 : -1,
|
|
145
|
+
});
|
|
146
|
+
if (nextValue === currentValue)
|
|
197
147
|
return;
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
{ className: `${ROOT_CLASS_NAME}-label` },
|
|
228
|
-
React.createElement(
|
|
229
|
-
'p',
|
|
230
|
-
{ className: `${ROOT_CLASS_NAME}-label-text` },
|
|
231
|
-
label,
|
|
232
|
-
),
|
|
233
|
-
)
|
|
234
|
-
: null,
|
|
235
|
-
React.createElement(
|
|
236
|
-
'div',
|
|
237
|
-
{ className: `${ROOT_CLASS_NAME}-value` },
|
|
238
|
-
React.createElement(InputText, {
|
|
239
|
-
disabled: disabled,
|
|
240
|
-
initialValue: value,
|
|
241
|
-
name: name,
|
|
242
|
-
onBlur: handleBlur,
|
|
243
|
-
onChange: onChange,
|
|
244
|
-
onFocus: onFocus,
|
|
245
|
-
onKeyDown: handleKeyDown,
|
|
246
|
-
onKeyUp: handleKeyUp,
|
|
247
|
-
placeholder: placeholder,
|
|
248
|
-
ref: inputRef,
|
|
249
|
-
selectTextOnFocus: true,
|
|
250
|
-
tabIndex: tabIndex,
|
|
251
|
-
}),
|
|
252
|
-
),
|
|
253
|
-
);
|
|
148
|
+
event.stopPropagation();
|
|
149
|
+
event.preventDefault();
|
|
150
|
+
input.value = nextValue;
|
|
151
|
+
return;
|
|
152
|
+
case 'Enter':
|
|
153
|
+
case 'Escape':
|
|
154
|
+
if (event.key === 'Escape') {
|
|
155
|
+
input.value = submittedValueRef.current;
|
|
156
|
+
}
|
|
157
|
+
input.blur();
|
|
158
|
+
return;
|
|
159
|
+
default:
|
|
160
|
+
// No default key handling
|
|
161
|
+
}
|
|
162
|
+
}, [getNextValue, onKeyDown, placeholder, unit]);
|
|
163
|
+
const handleKeyUp = useCallback((event) => {
|
|
164
|
+
if (onKeyUp)
|
|
165
|
+
onKeyUp(event);
|
|
166
|
+
// If this is the key up from ↑ or ↓ keys, time to handleSubmitValue
|
|
167
|
+
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
168
|
+
handleSubmitValue(event);
|
|
169
|
+
}
|
|
170
|
+
}, [handleSubmitValue, onKeyUp]);
|
|
171
|
+
return (React.createElement("label", { "aria-label": label ? undefined : title, className: clsx(ROOT_CLASS_NAME, className, { disabled }), title: title },
|
|
172
|
+
icon == null ? null : (React.createElement("div", { className: `${ROOT_CLASS_NAME}-icon` }, icon)),
|
|
173
|
+
label ? (React.createElement("div", { className: `${ROOT_CLASS_NAME}-label` },
|
|
174
|
+
React.createElement("p", { className: `${ROOT_CLASS_NAME}-label-text` }, label))) : null,
|
|
175
|
+
React.createElement("div", { className: `${ROOT_CLASS_NAME}-value` },
|
|
176
|
+
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 }))));
|
|
254
177
|
});
|
|
255
|
-
//# sourceMappingURL=CSSValueInput.js.map
|
|
178
|
+
//# sourceMappingURL=CSSValueInput.js.map
|
|
@@ -1,58 +1,58 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Flowtype definitions for CSSValueInput
|
|
3
3
|
* Generated by Flowgen from a Typescript Definition
|
|
4
|
-
* Flowgen v1.
|
|
4
|
+
* Flowgen v1.21.0
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { type, CSSValueType } from
|
|
9
|
-
import * as React from
|
|
8
|
+
import { type, CSSValueType } from "@acusti/css-values";
|
|
9
|
+
import * as React from "react";
|
|
10
10
|
export type Props = {|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Boolean indicating if the user can submit an empty value (i.e. clear
|
|
13
|
+
* the value). Defaults to true.
|
|
14
|
+
*/
|
|
15
|
+
allowEmpty?: boolean,
|
|
16
|
+
className?: string,
|
|
17
|
+
cssValueType?: CSSValueType,
|
|
18
|
+
disabled?: boolean,
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Function that receives a value and converts it to its numerical equivalent
|
|
22
|
+
* (i.e. '12px' → 12). Defaults to parseFloat().
|
|
23
|
+
*/
|
|
24
|
+
getValueAsNumber?: (value: number | string) => number,
|
|
25
|
+
icon?: React.Node,
|
|
26
|
+
label?: string,
|
|
27
|
+
max?: number,
|
|
28
|
+
min?: number,
|
|
29
|
+
name?: string,
|
|
30
|
+
onBlur?: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed,
|
|
31
|
+
onChange?: (event: SyntheticInputEvent<HTMLInputElement>) => mixed,
|
|
32
|
+
onFocus?: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed,
|
|
33
|
+
onKeyDown?: (event: SyntheticKeyboardEvent<HTMLInputElement>) => mixed,
|
|
34
|
+
onKeyUp?: (event: SyntheticKeyboardEvent<HTMLInputElement>) => mixed,
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Custom event handler triggered when the user presses enter/return
|
|
38
|
+
* or blurs the input after making a change. Hitting esc will restore
|
|
39
|
+
* the previous submitted value or original value.
|
|
40
|
+
*/
|
|
41
|
+
onSubmitValue: (value: string) => mixed,
|
|
42
|
+
placeholder?: string,
|
|
43
|
+
step?: number,
|
|
44
|
+
tabIndex?: number,
|
|
45
|
+
title?: string,
|
|
46
|
+
unit?: string,
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Regex or validator function to validate non-numeric values
|
|
50
|
+
*/
|
|
51
|
+
validator?: ((value: string) => boolean) | RegExp,
|
|
52
|
+
value?: string,
|
|
53
53
|
|};
|
|
54
54
|
declare var _default: React.StatelessFunctionalComponent<{|
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
...Props,
|
|
56
|
+
...{| ref?: HTMLInputElement |},
|
|
57
57
|
|}>;
|
|
58
58
|
declare export default typeof _default;
|
|
@@ -4,19 +4,17 @@ import { userEvent } from '@testing-library/user-event';
|
|
|
4
4
|
import React from 'react';
|
|
5
5
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
6
6
|
import CSSValueInput from './CSSValueInput.js';
|
|
7
|
-
const noop = () => {}; // eslint-disable-line @typescript-eslint/no-empty-function
|
|
7
|
+
const noop = () => { }; // eslint-disable-line @typescript-eslint/no-empty-function
|
|
8
8
|
afterEach(cleanup);
|
|
9
9
|
describe('CSSValueInput.tsx', () => {
|
|
10
10
|
it('renders a text input with the given props.value', () => {
|
|
11
|
-
render(
|
|
12
|
-
React.createElement(CSSValueInput, { onSubmitValue: noop, value: '24px' }),
|
|
13
|
-
);
|
|
11
|
+
render(React.createElement(CSSValueInput, { onSubmitValue: noop, value: "24px" }));
|
|
14
12
|
const input = screen.getByRole('textbox');
|
|
15
13
|
expect(input.value).toBe('24px');
|
|
16
14
|
});
|
|
17
15
|
it('handles ↑/↓ keys to increment/decrement by 1 and ⇧↑/⇧↓ to increment/decrement by 10 (preserving the CSS unit)', async () => {
|
|
18
16
|
const user = userEvent.setup();
|
|
19
|
-
render(React.createElement(CSSValueInput, { onSubmitValue: noop, value:
|
|
17
|
+
render(React.createElement(CSSValueInput, { onSubmitValue: noop, value: "75%" }));
|
|
20
18
|
const input = screen.getByRole('textbox');
|
|
21
19
|
expect(input.value).toBe('75%');
|
|
22
20
|
await user.type(input, '{ArrowUp}');
|
|
@@ -34,13 +32,7 @@ describe('CSSValueInput.tsx', () => {
|
|
|
34
32
|
});
|
|
35
33
|
it('supports custom props.step for ↑/↓ key handling', async () => {
|
|
36
34
|
const user = userEvent.setup();
|
|
37
|
-
render(
|
|
38
|
-
React.createElement(CSSValueInput, {
|
|
39
|
-
onSubmitValue: noop,
|
|
40
|
-
step: 0.1,
|
|
41
|
-
value: '2rem',
|
|
42
|
-
}),
|
|
43
|
-
);
|
|
35
|
+
render(React.createElement(CSSValueInput, { onSubmitValue: noop, step: 0.1, value: "2rem" }));
|
|
44
36
|
const input = screen.getByRole('textbox');
|
|
45
37
|
expect(input.value).toBe('2rem');
|
|
46
38
|
await user.type(input, '{ArrowUp}');
|
|
@@ -58,14 +50,7 @@ describe('CSSValueInput.tsx', () => {
|
|
|
58
50
|
});
|
|
59
51
|
it('uses props.unit as default unit when unit is missing', async () => {
|
|
60
52
|
const user = userEvent.setup();
|
|
61
|
-
render(
|
|
62
|
-
React.createElement(CSSValueInput, {
|
|
63
|
-
allowEmpty: true,
|
|
64
|
-
onSubmitValue: noop,
|
|
65
|
-
unit: 'px',
|
|
66
|
-
value: '',
|
|
67
|
-
}),
|
|
68
|
-
);
|
|
53
|
+
render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "px", value: "" }));
|
|
69
54
|
const input = screen.getByRole('textbox');
|
|
70
55
|
expect(input.value).toBe('');
|
|
71
56
|
await user.type(input, '14{Enter}');
|
|
@@ -73,14 +58,7 @@ describe('CSSValueInput.tsx', () => {
|
|
|
73
58
|
});
|
|
74
59
|
it('preserves last entered unit if different from props.unit when unit is missing', async () => {
|
|
75
60
|
const user = userEvent.setup();
|
|
76
|
-
render(
|
|
77
|
-
React.createElement(CSSValueInput, {
|
|
78
|
-
allowEmpty: true,
|
|
79
|
-
onSubmitValue: noop,
|
|
80
|
-
unit: 'px',
|
|
81
|
-
value: '',
|
|
82
|
-
}),
|
|
83
|
-
);
|
|
61
|
+
render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "px", value: "" }));
|
|
84
62
|
const input = screen.getByRole('textbox');
|
|
85
63
|
expect(input.value).toBe('');
|
|
86
64
|
await user.type(input, '25vw{Enter}');
|
|
@@ -90,14 +68,7 @@ describe('CSSValueInput.tsx', () => {
|
|
|
90
68
|
});
|
|
91
69
|
it('treats value as numeric if props.unit is an empty string', async () => {
|
|
92
70
|
const user = userEvent.setup();
|
|
93
|
-
render(
|
|
94
|
-
React.createElement(CSSValueInput, {
|
|
95
|
-
allowEmpty: true,
|
|
96
|
-
onSubmitValue: noop,
|
|
97
|
-
unit: '',
|
|
98
|
-
value: '100',
|
|
99
|
-
}),
|
|
100
|
-
);
|
|
71
|
+
render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "", value: "100" }));
|
|
101
72
|
const input = screen.getByRole('textbox');
|
|
102
73
|
expect(input.value).toBe('100');
|
|
103
74
|
await user.type(input, '1{Enter}');
|
|
@@ -111,34 +82,16 @@ describe('CSSValueInput.tsx', () => {
|
|
|
111
82
|
});
|
|
112
83
|
it('updates default unit as props.unit and props.value changes', async () => {
|
|
113
84
|
const user = userEvent.setup();
|
|
114
|
-
const { rerender } = render(
|
|
115
|
-
React.createElement(CSSValueInput, {
|
|
116
|
-
onSubmitValue: noop,
|
|
117
|
-
unit: 'px',
|
|
118
|
-
value: '12px',
|
|
119
|
-
}),
|
|
120
|
-
);
|
|
85
|
+
const { rerender } = render(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "px", value: "12px" }));
|
|
121
86
|
const input = screen.getByRole('textbox');
|
|
122
|
-
rerender(
|
|
123
|
-
React.createElement(CSSValueInput, {
|
|
124
|
-
onSubmitValue: noop,
|
|
125
|
-
unit: '',
|
|
126
|
-
value: '4',
|
|
127
|
-
}),
|
|
128
|
-
);
|
|
87
|
+
rerender(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "", value: "4" }));
|
|
129
88
|
expect(input.value).toBe('4');
|
|
130
89
|
await user.type(input, '25{Enter}');
|
|
131
90
|
expect(input.value).toBe('25');
|
|
132
|
-
rerender(
|
|
133
|
-
React.createElement(CSSValueInput, {
|
|
134
|
-
onSubmitValue: noop,
|
|
135
|
-
unit: 'rad',
|
|
136
|
-
value: '3rad',
|
|
137
|
-
}),
|
|
138
|
-
);
|
|
91
|
+
rerender(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "rad", value: "3rad" }));
|
|
139
92
|
expect(input.value).toBe('3rad');
|
|
140
93
|
await user.type(input, '-4.1{Enter}');
|
|
141
94
|
expect(input.value).toBe('-4.1rad');
|
|
142
95
|
});
|
|
143
96
|
});
|
|
144
|
-
//# sourceMappingURL=CSSValueInput.test.js.map
|
|
97
|
+
//# sourceMappingURL=CSSValueInput.test.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acusti/css-value-input",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"exports": "./dist/CSSValueInput.js",
|
|
@@ -40,18 +40,18 @@
|
|
|
40
40
|
"homepage": "https://github.com/acusti/uikit/tree/main/packages/css-value-input#readme",
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@testing-library/dom": "^10.4.0",
|
|
43
|
-
"@testing-library/react": "^16.
|
|
44
|
-
"@testing-library/user-event": "^14.
|
|
45
|
-
"@types/react": "^19.
|
|
46
|
-
"happy-dom": "^
|
|
43
|
+
"@testing-library/react": "^16.3.0",
|
|
44
|
+
"@testing-library/user-event": "^14.6.1",
|
|
45
|
+
"@types/react": "^19.1.2",
|
|
46
|
+
"happy-dom": "^17.4.6",
|
|
47
47
|
"react": "^19.0.0",
|
|
48
48
|
"react-dom": "^19.0.0",
|
|
49
|
-
"typescript": "5.
|
|
50
|
-
"vitest": "^
|
|
49
|
+
"typescript": "5.8.3",
|
|
50
|
+
"vitest": "^3.1.1"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@acusti/css-values": "^1.0
|
|
54
|
-
"@acusti/input-text": "^1.
|
|
53
|
+
"@acusti/css-values": "^1.1.0",
|
|
54
|
+
"@acusti/input-text": "^1.10.1",
|
|
55
55
|
"clsx": "^2"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|