@acusti/css-value-input 1.0.0-rc.0 → 1.0.0-rc.10
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 +61 -0
- package/dist/CSSValueInput.d.ts +16 -4
- package/dist/CSSValueInput.js +25 -15
- package/dist/CSSValueInput.js.flow +19 -4
- package/dist/CSSValueInput.js.map +1 -1
- package/dist/CSSValueInput.test.d.ts +1 -0
- package/dist/CSSValueInput.test.js +104 -0
- package/dist/CSSValueInput.test.js.flow +6 -0
- package/dist/CSSValueInput.test.js.map +1 -0
- package/package.json +20 -16
- package/src/CSSValueInput.test.tsx +55 -22
- package/src/CSSValueInput.tsx +250 -238
package/README.md
CHANGED
|
@@ -9,3 +9,64 @@
|
|
|
9
9
|
take and update a CSS value of a particular type with a default unit. The
|
|
10
10
|
input’s behavior is similar to those of design applications such as Adobe
|
|
11
11
|
Illustrator.
|
|
12
|
+
|
|
13
|
+
See the [storybook docs and demo][] to get a feel for what it can do.
|
|
14
|
+
|
|
15
|
+
[storybook docs and demo]:
|
|
16
|
+
https://acusti-uikit.netlify.app/?path=/docs/uikit-controls-CSSValueInput--docs
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
npm install @acusti/css-value-input
|
|
22
|
+
# or
|
|
23
|
+
yarn add @acusti/css-value-input
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Props
|
|
27
|
+
|
|
28
|
+
This is the type signature for the props you can pass to `CSSValueInput`.
|
|
29
|
+
The unique features provided by the component are called out and explained
|
|
30
|
+
above the corresponding prop via JSDoc comments:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
type Props = {
|
|
34
|
+
/**
|
|
35
|
+
* Boolean indicating if the user can submit an empty value (i.e. clear
|
|
36
|
+
* the value). Defaults to true.
|
|
37
|
+
*/
|
|
38
|
+
allowEmpty?: boolean;
|
|
39
|
+
className?: string;
|
|
40
|
+
cssValueType?: CSSValueType;
|
|
41
|
+
disabled?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Function that receives a value and converts it to its numerical equivalent
|
|
44
|
+
* (i.e. '12px' → 12). Defaults to parseFloat().
|
|
45
|
+
*/
|
|
46
|
+
getValueAsNumber?: (value: string | number) => number;
|
|
47
|
+
icon?: React.ReactNode;
|
|
48
|
+
label?: string;
|
|
49
|
+
max?: number;
|
|
50
|
+
min?: number;
|
|
51
|
+
name?: string;
|
|
52
|
+
onBlur?: (event: React.FocusEvent<HTMLInputElement>) => unknown;
|
|
53
|
+
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
54
|
+
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => unknown;
|
|
55
|
+
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
56
|
+
onKeyUp?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
57
|
+
/**
|
|
58
|
+
* Custom event handler triggered when the user presses enter/return
|
|
59
|
+
* or blurs the input after making a change. Hitting esc will restore
|
|
60
|
+
* the previous submitted value or original value.
|
|
61
|
+
*/
|
|
62
|
+
onSubmitValue: (value: string) => unknown;
|
|
63
|
+
placeholder?: string;
|
|
64
|
+
step?: number;
|
|
65
|
+
tabIndex?: number;
|
|
66
|
+
title?: string;
|
|
67
|
+
unit?: string;
|
|
68
|
+
/** Regex or validator function to validate non-numeric values */
|
|
69
|
+
validator?: RegExp | ((value: string) => boolean);
|
|
70
|
+
value?: string;
|
|
71
|
+
};
|
|
72
|
+
```
|
package/dist/CSSValueInput.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
1
|
import type { CSSValueType } from '@acusti/css-values';
|
|
2
|
+
import * as React from 'react';
|
|
3
3
|
export type Props = {
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Boolean indicating if the user can submit an empty value (i.e. clear
|
|
6
|
+
* the value). Defaults to true.
|
|
7
|
+
*/
|
|
5
8
|
allowEmpty?: boolean;
|
|
6
9
|
className?: string;
|
|
7
10
|
cssValueType?: CSSValueType;
|
|
8
11
|
disabled?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Function that receives a value and converts it to its numerical equivalent
|
|
14
|
+
* (i.e. '12px' → 12). Defaults to parseFloat().
|
|
15
|
+
*/
|
|
9
16
|
getValueAsNumber?: (value: string | number) => number;
|
|
10
17
|
icon?: React.ReactNode;
|
|
11
18
|
label?: string;
|
|
@@ -17,6 +24,11 @@ export type Props = {
|
|
|
17
24
|
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => unknown;
|
|
18
25
|
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
19
26
|
onKeyUp?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
27
|
+
/**
|
|
28
|
+
* Custom event handler triggered when the user presses enter/return
|
|
29
|
+
* or blurs the input after making a change. Hitting esc will restore
|
|
30
|
+
* the previous submitted value or original value.
|
|
31
|
+
*/
|
|
20
32
|
onSubmitValue: (value: string) => unknown;
|
|
21
33
|
placeholder?: string;
|
|
22
34
|
step?: number;
|
|
@@ -27,5 +39,5 @@ export type Props = {
|
|
|
27
39
|
validator?: RegExp | ((value: string) => boolean);
|
|
28
40
|
value?: string;
|
|
29
41
|
};
|
|
30
|
-
declare const
|
|
31
|
-
export default
|
|
42
|
+
declare const _default: React.ForwardRefExoticComponent<Props & React.RefAttributes<HTMLInputElement>>;
|
|
43
|
+
export default _default;
|
package/dist/CSSValueInput.js
CHANGED
|
@@ -2,12 +2,19 @@ import { DEFAULT_CSS_VALUE_TYPE, DEFAULT_UNIT_BY_CSS_VALUE_TYPE, getCSSValueAsNu
|
|
|
2
2
|
import InputText from '@acusti/input-text';
|
|
3
3
|
import clsx from 'clsx';
|
|
4
4
|
import * as React from 'react';
|
|
5
|
-
const { useCallback, useImperativeHandle, useRef } = React;
|
|
5
|
+
const { useCallback, useEffect, useImperativeHandle, useRef } = React;
|
|
6
6
|
const ROOT_CLASS_NAME = 'cssvalueinput';
|
|
7
|
-
|
|
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) {
|
|
8
8
|
const inputRef = useRef(null);
|
|
9
9
|
useImperativeHandle(ref, () => inputRef.current);
|
|
10
|
-
|
|
10
|
+
// props.value should be a string; if it’s a number, convert it here
|
|
11
|
+
if (typeof value === 'number' && !Number.isNaN(value)) {
|
|
12
|
+
value = `${value}`; // eslint-disable-line @typescript-eslint/restrict-template-expressions
|
|
13
|
+
}
|
|
14
|
+
const submittedValueRef = useRef(value !== null && value !== void 0 ? value : '');
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
submittedValueRef.current = value !== null && value !== void 0 ? value : '';
|
|
17
|
+
}, [value]);
|
|
11
18
|
const handleSubmitValue = useCallback((event) => {
|
|
12
19
|
const currentValue = event.currentTarget.value;
|
|
13
20
|
// Store last submittedValue (used to reset value on invalid input)
|
|
@@ -27,11 +34,15 @@ const CSSValueInput = React.forwardRef(({ allowEmpty = true, className, cssValue
|
|
|
27
34
|
}
|
|
28
35
|
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
29
36
|
const isCurrentValueFinite = Number.isFinite(currentValueAsNumber);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
+
: '';
|
|
35
46
|
if (!isCurrentValueFinite) {
|
|
36
47
|
let isValid = false;
|
|
37
48
|
if (validator instanceof RegExp) {
|
|
@@ -93,8 +104,7 @@ const CSSValueInput = React.forwardRef(({ allowEmpty = true, className, cssValue
|
|
|
93
104
|
const modifier = multiplier * step * signum;
|
|
94
105
|
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
95
106
|
// If currentValue isn’t numeric, don’t try to increment/decrement it
|
|
96
|
-
if (typeof currentValue === 'string' &&
|
|
97
|
-
Number.isNaN(currentValueAsNumber)) {
|
|
107
|
+
if (typeof currentValue === 'string' && Number.isNaN(currentValueAsNumber)) {
|
|
98
108
|
return currentValue;
|
|
99
109
|
}
|
|
100
110
|
let nextValue = currentValueAsNumber + modifier;
|
|
@@ -118,11 +128,12 @@ const CSSValueInput = React.forwardRef(({ allowEmpty = true, className, cssValue
|
|
|
118
128
|
return `${nextValue}${nextUnit}`;
|
|
119
129
|
}, [cssValueType, getValueAsNumber, max, min, step, unit]);
|
|
120
130
|
const handleKeyDown = useCallback((event) => {
|
|
131
|
+
var _a, _b;
|
|
121
132
|
const input = event.currentTarget;
|
|
122
133
|
inputRef.current = input;
|
|
123
134
|
if (onKeyDown)
|
|
124
135
|
onKeyDown(event);
|
|
125
|
-
const currentValue = input.value
|
|
136
|
+
const currentValue = (_b = (_a = input.value) !== null && _a !== void 0 ? _a : placeholder) !== null && _b !== void 0 ? _b : `0${unit}`;
|
|
126
137
|
let nextValue = '';
|
|
127
138
|
switch (event.key) {
|
|
128
139
|
case 'Escape':
|
|
@@ -158,11 +169,10 @@ const CSSValueInput = React.forwardRef(({ allowEmpty = true, className, cssValue
|
|
|
158
169
|
}
|
|
159
170
|
}, [handleSubmitValue, onKeyUp]);
|
|
160
171
|
return (React.createElement("label", { className: clsx(ROOT_CLASS_NAME, className, { disabled }), title: title },
|
|
161
|
-
icon
|
|
162
|
-
label
|
|
163
|
-
React.createElement("p", { className: `${ROOT_CLASS_NAME}-label-text` }, label))),
|
|
172
|
+
icon ? React.createElement("div", { className: `${ROOT_CLASS_NAME}-icon` }, icon) : null,
|
|
173
|
+
label ? (React.createElement("div", { className: `${ROOT_CLASS_NAME}-label` },
|
|
174
|
+
React.createElement("p", { className: `${ROOT_CLASS_NAME}-label-text` }, label))) : null,
|
|
164
175
|
React.createElement("div", { className: `${ROOT_CLASS_NAME}-value` },
|
|
165
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 }))));
|
|
166
177
|
});
|
|
167
|
-
export default CSSValueInput;
|
|
168
178
|
//# sourceMappingURL=CSSValueInput.js.map
|
|
@@ -5,16 +5,22 @@
|
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import * as React from "react";
|
|
9
8
|
import type { CSSValueType } from "@acusti/css-values";
|
|
9
|
+
import * as React from "react";
|
|
10
10
|
export type Props = {|
|
|
11
11
|
/**
|
|
12
|
-
* Boolean indicating if the user can submit an empty value (i.e. clear
|
|
12
|
+
* Boolean indicating if the user can submit an empty value (i.e. clear
|
|
13
|
+
* the value). Defaults to true.
|
|
13
14
|
*/
|
|
14
15
|
allowEmpty?: boolean,
|
|
15
16
|
className?: string,
|
|
16
17
|
cssValueType?: CSSValueType,
|
|
17
18
|
disabled?: boolean,
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Function that receives a value and converts it to its numerical equivalent
|
|
22
|
+
* (i.e. '12px' → 12). Defaults to parseFloat().
|
|
23
|
+
*/
|
|
18
24
|
getValueAsNumber?: (value: string | number) => number,
|
|
19
25
|
icon?: React.Node,
|
|
20
26
|
label?: string,
|
|
@@ -26,6 +32,12 @@ export type Props = {|
|
|
|
26
32
|
onFocus?: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed,
|
|
27
33
|
onKeyDown?: (event: SyntheticKeyboardEvent<HTMLInputElement>) => mixed,
|
|
28
34
|
onKeyUp?: (event: SyntheticKeyboardEvent<HTMLInputElement>) => mixed,
|
|
35
|
+
|
|
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
|
+
*/
|
|
29
41
|
onSubmitValue: (value: string) => mixed,
|
|
30
42
|
placeholder?: string,
|
|
31
43
|
step?: number,
|
|
@@ -39,5 +51,8 @@ export type Props = {|
|
|
|
39
51
|
validator?: RegExp | ((value: string) => boolean),
|
|
40
52
|
value?: string,
|
|
41
53
|
|};
|
|
42
|
-
declare var
|
|
43
|
-
|
|
54
|
+
declare var _default: React.StatelessFunctionalComponent<{|
|
|
55
|
+
...Props,
|
|
56
|
+
...{| ref?: HTMLInputElement |},
|
|
57
|
+
|}>;
|
|
58
|
+
declare export default typeof _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CSSValueInput.js","sourceRoot":"","sources":["../src/CSSValueInput.tsx"],"names":[],"mappings":"AAAA,OAAO,EACH,sBAAsB,EACtB,8BAA8B,EAC9B,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,GACnB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"CSSValueInput.js","sourceRoot":"","sources":["../src/CSSValueInput.tsx"],"names":[],"mappings":"AAAA,OAAO,EACH,sBAAsB,EACtB,8BAA8B,EAC9B,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,GACnB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAC3C,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AA4C/B,MAAM,EAAE,WAAW,EAAE,SAAS,EAA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B,oEAAoE;QACpE,IAAI,KAAK,CAAC,GAAG,KAAK,SAAS,IAAI,KAAK,CAAC,GAAG,KAAK,WAAW,EAAE,CAAC;YACvD,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAC7B,CAAC;IACL,CAAC,EACD,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAC/B,CAAC;IAEF,OAAO,CACH,+BAAO,SAAS,EAAE,IAAI,CAAC,eAAe,EAAE,SAAS,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK;QACzE,IAAI,CAAC,CAAC,CAAC,6BAAK,SAAS,EAAE,GAAG,eAAe,OAAO,IAAG,IAAI,CAAO,CAAC,CAAC,CAAC,IAAI;QACrE,KAAK,CAAC,CAAC,CAAC,CACL,6BAAK,SAAS,EAAE,GAAG,eAAe,QAAQ;YACtC,2BAAG,SAAS,EAAE,GAAG,eAAe,aAAa,IAAG,KAAK,CAAK,CACxD,CACT,CAAC,CAAC,CAAC,IAAI;QACR,6BAAK,SAAS,EAAE,GAAG,eAAe,QAAQ;YACtC,oBAAC,SAAS,IACN,QAAQ,EAAE,QAAQ,EAClB,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,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { cleanup, render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
import React from 'react'; // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
5
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
6
|
+
import CSSValueInput from './CSSValueInput.js';
|
|
7
|
+
const noop = () => { }; // eslint-disable-line @typescript-eslint/no-empty-function
|
|
8
|
+
afterEach(cleanup);
|
|
9
|
+
describe('CSSValueInput.tsx', () => {
|
|
10
|
+
it('renders a text input with the given props.value', () => {
|
|
11
|
+
render(React.createElement(CSSValueInput, { onSubmitValue: noop, value: "24px" }));
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
13
|
+
const input = screen.getByRole('textbox');
|
|
14
|
+
expect(input.value).toBe('24px');
|
|
15
|
+
});
|
|
16
|
+
it('handles ↑/↓ keys to increment/decrement by 1 and ⇧↑/⇧↓ to increment/decrement by 10 (preserving the CSS unit)', async () => {
|
|
17
|
+
const user = userEvent.setup();
|
|
18
|
+
render(React.createElement(CSSValueInput, { onSubmitValue: noop, value: "75%" }));
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
20
|
+
const input = screen.getByRole('textbox');
|
|
21
|
+
expect(input.value).toBe('75%');
|
|
22
|
+
await user.type(input, '{ArrowUp}');
|
|
23
|
+
expect(input.value).toBe('76%');
|
|
24
|
+
await user.type(input, '{Shift>}{ArrowUp}{/Shift}');
|
|
25
|
+
expect(input.value).toBe('86%');
|
|
26
|
+
await user.type(input, '{Shift>}{ArrowUp}{/Shift}');
|
|
27
|
+
expect(input.value).toBe('96%');
|
|
28
|
+
await user.type(input, '{ArrowUp}');
|
|
29
|
+
expect(input.value).toBe('97%');
|
|
30
|
+
await user.type(input, '{Shift>}{ArrowDown}{/Shift}');
|
|
31
|
+
expect(input.value).toBe('87%');
|
|
32
|
+
await user.type(input, '{Shift>}{ArrowDown}{/Shift}');
|
|
33
|
+
expect(input.value).toBe('77%');
|
|
34
|
+
});
|
|
35
|
+
it('supports custom props.step for ↑/↓ key handling', async () => {
|
|
36
|
+
const user = userEvent.setup();
|
|
37
|
+
render(React.createElement(CSSValueInput, { onSubmitValue: noop, step: 0.1, value: "2rem" }));
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
39
|
+
const input = screen.getByRole('textbox');
|
|
40
|
+
expect(input.value).toBe('2rem');
|
|
41
|
+
await user.type(input, '{ArrowUp}');
|
|
42
|
+
expect(input.value).toBe('2.1rem');
|
|
43
|
+
await user.type(input, '{Shift>}{ArrowUp}{/Shift}');
|
|
44
|
+
expect(input.value).toBe('3.1rem');
|
|
45
|
+
await user.type(input, '{Shift>}{ArrowUp}{/Shift}');
|
|
46
|
+
expect(input.value).toBe('4.1rem');
|
|
47
|
+
await user.type(input, '{ArrowUp}');
|
|
48
|
+
expect(input.value).toBe('4.2rem');
|
|
49
|
+
await user.type(input, '{Shift>}{ArrowDown}{/Shift}');
|
|
50
|
+
expect(input.value).toBe('3.2rem');
|
|
51
|
+
await user.type(input, '{Shift>}{ArrowDown}{/Shift}');
|
|
52
|
+
expect(input.value).toBe('2.2rem');
|
|
53
|
+
});
|
|
54
|
+
it('uses props.unit as default unit when unit is missing', async () => {
|
|
55
|
+
const user = userEvent.setup();
|
|
56
|
+
render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "px", value: "" }));
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
58
|
+
const input = screen.getByRole('textbox');
|
|
59
|
+
expect(input.value).toBe('');
|
|
60
|
+
await user.type(input, '14{Enter}');
|
|
61
|
+
expect(input.value).toBe('14px');
|
|
62
|
+
});
|
|
63
|
+
it('preserves last entered unit if different from props.unit when unit is missing', async () => {
|
|
64
|
+
const user = userEvent.setup();
|
|
65
|
+
render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "px", value: "" }));
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
67
|
+
const input = screen.getByRole('textbox');
|
|
68
|
+
expect(input.value).toBe('');
|
|
69
|
+
await user.type(input, '25vw{Enter}');
|
|
70
|
+
expect(input.value).toBe('25vw');
|
|
71
|
+
await user.type(input, '50{Enter}');
|
|
72
|
+
expect(input.value).toBe('50vw');
|
|
73
|
+
});
|
|
74
|
+
it('treats value as numeric if props.unit is an empty string', async () => {
|
|
75
|
+
const user = userEvent.setup();
|
|
76
|
+
render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "", value: "100" }));
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
78
|
+
const input = screen.getByRole('textbox');
|
|
79
|
+
expect(input.value).toBe('100');
|
|
80
|
+
await user.type(input, '1{Enter}');
|
|
81
|
+
expect(input.value).toBe('1');
|
|
82
|
+
await user.type(input, '{Shift>}{ArrowUp}{/Shift}');
|
|
83
|
+
expect(input.value).toBe('11');
|
|
84
|
+
await user.clear(input);
|
|
85
|
+
expect(input.value).toBe('');
|
|
86
|
+
await user.type(input, '200{Enter}');
|
|
87
|
+
expect(input.value).toBe('200');
|
|
88
|
+
});
|
|
89
|
+
it('updates default unit as props.unit and props.value changes', async () => {
|
|
90
|
+
const user = userEvent.setup();
|
|
91
|
+
const { rerender } = render(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "px", value: "12px" }));
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
93
|
+
const input = screen.getByRole('textbox');
|
|
94
|
+
rerender(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "", value: "4" }));
|
|
95
|
+
expect(input.value).toBe('4');
|
|
96
|
+
await user.type(input, '25{Enter}');
|
|
97
|
+
expect(input.value).toBe('25');
|
|
98
|
+
rerender(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "rad", value: "3rad" }));
|
|
99
|
+
expect(input.value).toBe('3rad');
|
|
100
|
+
await user.type(input, '-4.1{Enter}');
|
|
101
|
+
expect(input.value).toBe('-4.1rad');
|
|
102
|
+
});
|
|
103
|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CAAC,oBAAC,aAAa,IAAC,aAAa,EAAE,IAAI,EAAE,IAAI,EAAC,EAAE,EAAC,KAAK,EAAC,GAAG,GAAG,CAAC,CAAC;QACnE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC9B,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;QACpC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/B,QAAQ,CAAC,oBAAC,aAAa,IAAC,aAAa,EAAE,IAAI,EAAE,IAAI,EAAC,KAAK,EAAC,KAAK,EAAC,MAAM,GAAG,CAAC,CAAC;QACzE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACjC,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;QACtC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acusti/css-value-input",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.10",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"exports": "./dist/CSSValueInput.js",
|
|
7
|
+
"main": "./dist/CSSValueInput.js",
|
|
8
|
+
"types": "./dist/CSSValueInput.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
4
13
|
"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
14
|
"keywords": [
|
|
6
15
|
"react",
|
|
@@ -15,15 +24,9 @@
|
|
|
15
24
|
"ts",
|
|
16
25
|
"flow"
|
|
17
26
|
],
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"main": "./dist/CSSValueInput.js",
|
|
22
|
-
"types": "./dist/CSSValueInput.d.ts",
|
|
23
|
-
"files": [
|
|
24
|
-
"dist",
|
|
25
|
-
"src"
|
|
26
|
-
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "vitest"
|
|
29
|
+
},
|
|
27
30
|
"repository": {
|
|
28
31
|
"type": "git",
|
|
29
32
|
"url": "https://github.com/acusti/uikit.git",
|
|
@@ -39,16 +42,17 @@
|
|
|
39
42
|
"@testing-library/dom": "^9.3.1",
|
|
40
43
|
"@testing-library/react": "^14.0.0",
|
|
41
44
|
"@testing-library/user-event": "^14.4.3",
|
|
42
|
-
"@types/react": "^18.
|
|
43
|
-
"
|
|
45
|
+
"@types/react": "^18.2.45",
|
|
46
|
+
"happy-dom": "^12.10.3",
|
|
44
47
|
"react": "^18",
|
|
45
48
|
"react-dom": "^18",
|
|
46
|
-
"typescript": "^5.
|
|
49
|
+
"typescript": "^5.3.3",
|
|
50
|
+
"vitest": "^1.1.0"
|
|
47
51
|
},
|
|
48
52
|
"dependencies": {
|
|
49
|
-
"@acusti/css-values": "^1.0.
|
|
50
|
-
"@acusti/input-text": "^1.
|
|
51
|
-
"clsx": "^
|
|
53
|
+
"@acusti/css-values": "^1.0.2",
|
|
54
|
+
"@acusti/input-text": "^1.5.0",
|
|
55
|
+
"clsx": "^2"
|
|
52
56
|
},
|
|
53
57
|
"peerDependencies": {
|
|
54
58
|
"react": "^16.8 || ^17 || ^18",
|
|
@@ -1,25 +1,28 @@
|
|
|
1
|
-
// @vitest-environment
|
|
2
|
-
import { cleanup, render } from '@testing-library/react';
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { cleanup, render, screen } from '@testing-library/react';
|
|
3
3
|
import userEvent from '@testing-library/user-event';
|
|
4
|
-
import React from 'react';
|
|
4
|
+
import React from 'react'; // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
5
5
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
6
6
|
|
|
7
7
|
import CSSValueInput from './CSSValueInput.js';
|
|
8
8
|
|
|
9
|
-
const noop = () => {};
|
|
9
|
+
const noop = () => {}; // eslint-disable-line @typescript-eslint/no-empty-function
|
|
10
10
|
|
|
11
11
|
afterEach(cleanup);
|
|
12
12
|
|
|
13
|
-
describe('CSSValueInput.tsx',
|
|
14
|
-
it('renders a text input with the given props.value',
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
describe('CSSValueInput.tsx', () => {
|
|
14
|
+
it('renders a text input with the given props.value', () => {
|
|
15
|
+
render(<CSSValueInput onSubmitValue={noop} value="24px" />);
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
17
|
+
const input = screen.getByRole('textbox') as HTMLInputElement;
|
|
18
|
+
expect(input.value).toBe('24px');
|
|
17
19
|
});
|
|
18
20
|
|
|
19
21
|
it('handles ↑/↓ keys to increment/decrement by 1 and ⇧↑/⇧↓ to increment/decrement by 10 (preserving the CSS unit)', async () => {
|
|
20
22
|
const user = userEvent.setup();
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
render(<CSSValueInput onSubmitValue={noop} value="75%" />);
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
25
|
+
const input = screen.getByRole('textbox') as HTMLInputElement;
|
|
23
26
|
expect(input.value).toBe('75%');
|
|
24
27
|
await user.type(input, '{ArrowUp}');
|
|
25
28
|
expect(input.value).toBe('76%');
|
|
@@ -37,10 +40,9 @@ describe('CSSValueInput.tsx', async () => {
|
|
|
37
40
|
|
|
38
41
|
it('supports custom props.step for ↑/↓ key handling', async () => {
|
|
39
42
|
const user = userEvent.setup();
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
);
|
|
43
|
-
const input = getByRole('textbox');
|
|
43
|
+
render(<CSSValueInput onSubmitValue={noop} step={0.1} value="2rem" />);
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
45
|
+
const input = screen.getByRole('textbox') as HTMLInputElement;
|
|
44
46
|
expect(input.value).toBe('2rem');
|
|
45
47
|
await user.type(input, '{ArrowUp}');
|
|
46
48
|
expect(input.value).toBe('2.1rem');
|
|
@@ -58,10 +60,9 @@ describe('CSSValueInput.tsx', async () => {
|
|
|
58
60
|
|
|
59
61
|
it('uses props.unit as default unit when unit is missing', async () => {
|
|
60
62
|
const user = userEvent.setup();
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
);
|
|
64
|
-
const input = getByRole('textbox');
|
|
63
|
+
render(<CSSValueInput allowEmpty onSubmitValue={noop} unit="px" value="" />);
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
65
|
+
const input = screen.getByRole('textbox') as HTMLInputElement;
|
|
65
66
|
expect(input.value).toBe('');
|
|
66
67
|
await user.type(input, '14{Enter}');
|
|
67
68
|
expect(input.value).toBe('14px');
|
|
@@ -69,14 +70,46 @@ describe('CSSValueInput.tsx', async () => {
|
|
|
69
70
|
|
|
70
71
|
it('preserves last entered unit if different from props.unit when unit is missing', async () => {
|
|
71
72
|
const user = userEvent.setup();
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
);
|
|
75
|
-
const input = getByRole('textbox');
|
|
73
|
+
render(<CSSValueInput allowEmpty onSubmitValue={noop} unit="px" value="" />);
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
75
|
+
const input = screen.getByRole('textbox') as HTMLInputElement;
|
|
76
76
|
expect(input.value).toBe('');
|
|
77
77
|
await user.type(input, '25vw{Enter}');
|
|
78
78
|
expect(input.value).toBe('25vw');
|
|
79
79
|
await user.type(input, '50{Enter}');
|
|
80
80
|
expect(input.value).toBe('50vw');
|
|
81
81
|
});
|
|
82
|
+
|
|
83
|
+
it('treats value as numeric if props.unit is an empty string', async () => {
|
|
84
|
+
const user = userEvent.setup();
|
|
85
|
+
render(<CSSValueInput allowEmpty onSubmitValue={noop} unit="" value="100" />);
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
87
|
+
const input = screen.getByRole('textbox') as HTMLInputElement;
|
|
88
|
+
expect(input.value).toBe('100');
|
|
89
|
+
await user.type(input, '1{Enter}');
|
|
90
|
+
expect(input.value).toBe('1');
|
|
91
|
+
await user.type(input, '{Shift>}{ArrowUp}{/Shift}');
|
|
92
|
+
expect(input.value).toBe('11');
|
|
93
|
+
await user.clear(input);
|
|
94
|
+
expect(input.value).toBe('');
|
|
95
|
+
await user.type(input, '200{Enter}');
|
|
96
|
+
expect(input.value).toBe('200');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('updates default unit as props.unit and props.value changes', async () => {
|
|
100
|
+
const user = userEvent.setup();
|
|
101
|
+
const { rerender } = render(
|
|
102
|
+
<CSSValueInput onSubmitValue={noop} unit="px" value="12px" />,
|
|
103
|
+
);
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
105
|
+
const input = screen.getByRole('textbox') as HTMLInputElement;
|
|
106
|
+
rerender(<CSSValueInput onSubmitValue={noop} unit="" value="4" />);
|
|
107
|
+
expect(input.value).toBe('4');
|
|
108
|
+
await user.type(input, '25{Enter}');
|
|
109
|
+
expect(input.value).toBe('25');
|
|
110
|
+
rerender(<CSSValueInput onSubmitValue={noop} unit="rad" value="3rad" />);
|
|
111
|
+
expect(input.value).toBe('3rad');
|
|
112
|
+
await user.type(input, '-4.1{Enter}');
|
|
113
|
+
expect(input.value).toBe('-4.1rad');
|
|
114
|
+
});
|
|
82
115
|
});
|
package/src/CSSValueInput.tsx
CHANGED
|
@@ -6,18 +6,24 @@ import {
|
|
|
6
6
|
getUnitFromCSSValue,
|
|
7
7
|
roundToPrecision,
|
|
8
8
|
} from '@acusti/css-values';
|
|
9
|
+
import type { CSSValueType } from '@acusti/css-values';
|
|
9
10
|
import InputText from '@acusti/input-text';
|
|
10
11
|
import clsx from 'clsx';
|
|
11
12
|
import * as React from 'react';
|
|
12
13
|
|
|
13
|
-
import type { CSSValueType } from '@acusti/css-values';
|
|
14
|
-
|
|
15
14
|
export type Props = {
|
|
16
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Boolean indicating if the user can submit an empty value (i.e. clear
|
|
17
|
+
* the value). Defaults to true.
|
|
18
|
+
*/
|
|
17
19
|
allowEmpty?: boolean;
|
|
18
20
|
className?: string;
|
|
19
21
|
cssValueType?: CSSValueType;
|
|
20
22
|
disabled?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Function that receives a value and converts it to its numerical equivalent
|
|
25
|
+
* (i.e. '12px' → 12). Defaults to parseFloat().
|
|
26
|
+
*/
|
|
21
27
|
getValueAsNumber?: (value: string | number) => number;
|
|
22
28
|
icon?: React.ReactNode;
|
|
23
29
|
label?: string;
|
|
@@ -29,6 +35,11 @@ export type Props = {
|
|
|
29
35
|
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => unknown;
|
|
30
36
|
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
31
37
|
onKeyUp?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
38
|
+
/**
|
|
39
|
+
* Custom event handler triggered when the user presses enter/return
|
|
40
|
+
* or blurs the input after making a change. Hitting esc will restore
|
|
41
|
+
* the previous submitted value or original value.
|
|
42
|
+
*/
|
|
32
43
|
onSubmitValue: (value: string) => unknown;
|
|
33
44
|
placeholder?: string;
|
|
34
45
|
step?: number;
|
|
@@ -42,262 +53,263 @@ export type Props = {
|
|
|
42
53
|
|
|
43
54
|
type InputRef = HTMLInputElement | null;
|
|
44
55
|
|
|
45
|
-
const { useCallback, useImperativeHandle, useRef } = React;
|
|
56
|
+
const { useCallback, useEffect, useImperativeHandle, useRef } = React;
|
|
46
57
|
|
|
47
58
|
const ROOT_CLASS_NAME = 'cssvalueinput';
|
|
48
59
|
|
|
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
|
-
|
|
60
|
+
export default React.forwardRef<HTMLInputElement, Props>(function CSSValueInput(
|
|
61
|
+
{
|
|
62
|
+
allowEmpty = true,
|
|
63
|
+
className,
|
|
64
|
+
cssValueType = DEFAULT_CSS_VALUE_TYPE,
|
|
65
|
+
disabled,
|
|
66
|
+
getValueAsNumber = getCSSValueAsNumber,
|
|
67
|
+
icon,
|
|
68
|
+
label,
|
|
69
|
+
max,
|
|
70
|
+
min,
|
|
71
|
+
name,
|
|
72
|
+
onBlur,
|
|
73
|
+
onChange,
|
|
74
|
+
onFocus,
|
|
75
|
+
onKeyDown,
|
|
76
|
+
onKeyUp,
|
|
77
|
+
onSubmitValue,
|
|
78
|
+
placeholder,
|
|
79
|
+
step = 1,
|
|
80
|
+
tabIndex,
|
|
81
|
+
title,
|
|
82
|
+
unit = DEFAULT_UNIT_BY_CSS_VALUE_TYPE[cssValueType],
|
|
83
|
+
validator,
|
|
84
|
+
value,
|
|
85
|
+
}: Props,
|
|
86
|
+
ref,
|
|
87
|
+
) {
|
|
88
|
+
const inputRef = useRef<InputRef>(null);
|
|
89
|
+
useImperativeHandle<InputRef, InputRef>(ref, () => inputRef.current);
|
|
90
|
+
// props.value should be a string; if it’s a number, convert it here
|
|
91
|
+
if (typeof value === 'number' && !Number.isNaN(value)) {
|
|
92
|
+
value = `${value}`; // eslint-disable-line @typescript-eslint/restrict-template-expressions
|
|
93
|
+
}
|
|
94
|
+
const submittedValueRef = useRef<string>(value ?? '');
|
|
95
|
+
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
submittedValueRef.current = value ?? '';
|
|
98
|
+
}, [value]);
|
|
99
|
+
|
|
100
|
+
const handleSubmitValue = useCallback(
|
|
101
|
+
(event: React.SyntheticEvent<HTMLInputElement>) => {
|
|
102
|
+
const currentValue = event.currentTarget.value;
|
|
103
|
+
// Store last submittedValue (used to reset value on invalid input)
|
|
104
|
+
submittedValueRef.current = currentValue;
|
|
105
|
+
onSubmitValue(currentValue);
|
|
75
106
|
},
|
|
76
|
-
|
|
77
|
-
)
|
|
78
|
-
const inputRef = useRef<InputRef>(null);
|
|
79
|
-
useImperativeHandle<InputRef, InputRef>(ref, () => inputRef.current);
|
|
80
|
-
|
|
81
|
-
const submittedValueRef = useRef<string>(value || '');
|
|
82
|
-
|
|
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
|
-
);
|
|
92
|
-
|
|
93
|
-
const handleBlur = useCallback(
|
|
94
|
-
(event: React.FocusEvent<HTMLInputElement>) => {
|
|
95
|
-
const input = event.currentTarget;
|
|
96
|
-
inputRef.current = input;
|
|
97
|
-
if (onBlur) onBlur(event);
|
|
98
|
-
|
|
99
|
-
const currentValue = input.value.trim();
|
|
100
|
-
|
|
101
|
-
// If allowEmpty and value is empty, skip all validation + normalization
|
|
102
|
-
if (allowEmpty && !currentValue) {
|
|
103
|
-
handleSubmitValue(event);
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
108
|
-
const isCurrentValueFinite = Number.isFinite(currentValueAsNumber);
|
|
109
|
-
const defaultUnit = getUnitFromCSSValue({
|
|
110
|
-
cssValueType,
|
|
111
|
-
defaultUnit: unit,
|
|
112
|
-
value: submittedValueRef.current,
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
if (!isCurrentValueFinite) {
|
|
116
|
-
let isValid = false;
|
|
117
|
-
if (validator instanceof RegExp) {
|
|
118
|
-
isValid = validator.test(currentValue);
|
|
119
|
-
} else if (validator) {
|
|
120
|
-
isValid = validator(currentValue);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (isValid) {
|
|
124
|
-
handleSubmitValue(event);
|
|
125
|
-
} else {
|
|
126
|
-
// If current value isn’t valid, revert to last submitted value
|
|
127
|
-
input.value = submittedValueRef.current;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
107
|
+
[onSubmitValue],
|
|
108
|
+
);
|
|
132
109
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
normalizedValueAsNumber = min;
|
|
139
|
-
} else if (max != null && currentValueAsNumber > max) {
|
|
140
|
-
normalizedValueAsNumber = max;
|
|
141
|
-
} else if (cssValueType === 'integer') {
|
|
142
|
-
normalizedValueAsNumber = Math.floor(currentValueAsNumber);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
110
|
+
const handleBlur = useCallback(
|
|
111
|
+
(event: React.FocusEvent<HTMLInputElement>) => {
|
|
112
|
+
const input = event.currentTarget;
|
|
113
|
+
inputRef.current = input;
|
|
114
|
+
if (onBlur) onBlur(event);
|
|
145
115
|
|
|
146
|
-
|
|
147
|
-
const currentUnit = getUnitFromCSSValue({
|
|
148
|
-
cssValueType,
|
|
149
|
-
defaultUnit,
|
|
150
|
-
value: currentValue,
|
|
151
|
-
});
|
|
152
|
-
input.value = normalizedValueAsNumber + currentUnit;
|
|
153
|
-
} else {
|
|
154
|
-
input.value = getCSSValueWithUnit({
|
|
155
|
-
cssValueType,
|
|
156
|
-
defaultUnit,
|
|
157
|
-
value: currentValue,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
116
|
+
const currentValue = input.value.trim();
|
|
160
117
|
|
|
118
|
+
// If allowEmpty and value is empty, skip all validation + normalization
|
|
119
|
+
if (allowEmpty && !currentValue) {
|
|
161
120
|
handleSubmitValue(event);
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
({
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
multiplier?: number;
|
|
184
|
-
signum?: number;
|
|
185
|
-
}) => {
|
|
186
|
-
const modifier = multiplier * step * signum;
|
|
187
|
-
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
188
|
-
// If currentValue isn’t numeric, don’t try to increment/decrement it
|
|
189
|
-
if (
|
|
190
|
-
typeof currentValue === 'string' &&
|
|
191
|
-
Number.isNaN(currentValueAsNumber)
|
|
192
|
-
) {
|
|
193
|
-
return currentValue;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
125
|
+
const isCurrentValueFinite = Number.isFinite(currentValueAsNumber);
|
|
126
|
+
// Inherit unit from last submitted value unless default is unitless;
|
|
127
|
+
// ensures that submitting a new value with no unit doesn’t add a unit
|
|
128
|
+
const defaultUnit = unit
|
|
129
|
+
? getUnitFromCSSValue({
|
|
130
|
+
cssValueType,
|
|
131
|
+
defaultUnit: unit,
|
|
132
|
+
value: submittedValueRef.current,
|
|
133
|
+
})
|
|
134
|
+
: '';
|
|
135
|
+
|
|
136
|
+
if (!isCurrentValueFinite) {
|
|
137
|
+
let isValid = false;
|
|
138
|
+
if (validator instanceof RegExp) {
|
|
139
|
+
isValid = validator.test(currentValue);
|
|
140
|
+
} else if (validator) {
|
|
141
|
+
isValid = validator(currentValue);
|
|
194
142
|
}
|
|
195
143
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
nextValue = Math.floor(nextValue);
|
|
144
|
+
if (isValid) {
|
|
145
|
+
handleSubmitValue(event);
|
|
199
146
|
} else {
|
|
200
|
-
|
|
147
|
+
// If current value isn’t valid, revert to last submitted value
|
|
148
|
+
input.value = submittedValueRef.current;
|
|
201
149
|
}
|
|
202
150
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Normalize value by applying min/max and integer constraints
|
|
155
|
+
let normalizedValueAsNumber = currentValueAsNumber;
|
|
206
156
|
|
|
207
|
-
|
|
208
|
-
|
|
157
|
+
if (isCurrentValueFinite) {
|
|
158
|
+
if (min != null && currentValueAsNumber < min) {
|
|
159
|
+
normalizedValueAsNumber = min;
|
|
160
|
+
} else if (max != null && currentValueAsNumber > max) {
|
|
161
|
+
normalizedValueAsNumber = max;
|
|
162
|
+
} else if (cssValueType === 'integer') {
|
|
163
|
+
normalizedValueAsNumber = Math.floor(currentValueAsNumber);
|
|
209
164
|
}
|
|
165
|
+
}
|
|
210
166
|
|
|
211
|
-
|
|
167
|
+
if (normalizedValueAsNumber !== currentValueAsNumber) {
|
|
168
|
+
const currentUnit = getUnitFromCSSValue({
|
|
212
169
|
cssValueType,
|
|
213
|
-
defaultUnit
|
|
170
|
+
defaultUnit,
|
|
214
171
|
value: currentValue,
|
|
215
172
|
});
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
inputRef.current = input;
|
|
225
|
-
if (onKeyDown) onKeyDown(event);
|
|
226
|
-
|
|
227
|
-
const currentValue = input.value || placeholder || `0${unit}`;
|
|
228
|
-
let nextValue = '';
|
|
229
|
-
|
|
230
|
-
switch (event.key) {
|
|
231
|
-
case 'Escape':
|
|
232
|
-
case 'Enter':
|
|
233
|
-
if (event.key === 'Escape') {
|
|
234
|
-
input.value = submittedValueRef.current;
|
|
235
|
-
}
|
|
236
|
-
input.blur();
|
|
237
|
-
return;
|
|
238
|
-
case 'ArrowUp':
|
|
239
|
-
case 'ArrowDown':
|
|
240
|
-
nextValue = getNextValue({
|
|
241
|
-
currentValue,
|
|
242
|
-
multiplier: event.shiftKey ? 10 : 1,
|
|
243
|
-
signum: event.key === 'ArrowUp' ? 1 : -1,
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
if (nextValue === currentValue) return;
|
|
173
|
+
input.value = normalizedValueAsNumber + currentUnit;
|
|
174
|
+
} else {
|
|
175
|
+
input.value = getCSSValueWithUnit({
|
|
176
|
+
cssValueType,
|
|
177
|
+
defaultUnit,
|
|
178
|
+
value: currentValue,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
247
181
|
|
|
248
|
-
|
|
249
|
-
|
|
182
|
+
handleSubmitValue(event);
|
|
183
|
+
},
|
|
184
|
+
[
|
|
185
|
+
allowEmpty,
|
|
186
|
+
cssValueType,
|
|
187
|
+
getValueAsNumber,
|
|
188
|
+
handleSubmitValue,
|
|
189
|
+
max,
|
|
190
|
+
min,
|
|
191
|
+
onBlur,
|
|
192
|
+
unit,
|
|
193
|
+
validator,
|
|
194
|
+
],
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const getNextValue = useCallback(
|
|
198
|
+
({
|
|
199
|
+
currentValue,
|
|
200
|
+
multiplier = 1,
|
|
201
|
+
signum = 1,
|
|
202
|
+
}: {
|
|
203
|
+
currentValue: string | number;
|
|
204
|
+
multiplier?: number;
|
|
205
|
+
signum?: number;
|
|
206
|
+
}) => {
|
|
207
|
+
const modifier = multiplier * step * signum;
|
|
208
|
+
const currentValueAsNumber = getValueAsNumber(currentValue);
|
|
209
|
+
// If currentValue isn’t numeric, don’t try to increment/decrement it
|
|
210
|
+
if (typeof currentValue === 'string' && Number.isNaN(currentValueAsNumber)) {
|
|
211
|
+
return currentValue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let nextValue = currentValueAsNumber + modifier;
|
|
215
|
+
if (cssValueType === 'integer') {
|
|
216
|
+
nextValue = Math.floor(nextValue);
|
|
217
|
+
} else {
|
|
218
|
+
nextValue = roundToPrecision(nextValue, 5);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (typeof max === 'number' && Number.isFinite(max)) {
|
|
222
|
+
nextValue = Math.min(max, nextValue);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (typeof min === 'number' && Number.isFinite(min)) {
|
|
226
|
+
nextValue = Math.max(min, nextValue);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const nextUnit = getUnitFromCSSValue({
|
|
230
|
+
cssValueType,
|
|
231
|
+
defaultUnit: unit,
|
|
232
|
+
value: currentValue,
|
|
233
|
+
});
|
|
234
|
+
return `${nextValue}${nextUnit}`;
|
|
235
|
+
},
|
|
236
|
+
[cssValueType, getValueAsNumber, max, min, step, unit],
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
const handleKeyDown = useCallback(
|
|
240
|
+
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
241
|
+
const input = event.currentTarget;
|
|
242
|
+
inputRef.current = input;
|
|
243
|
+
if (onKeyDown) onKeyDown(event);
|
|
244
|
+
|
|
245
|
+
const currentValue = input.value ?? placeholder ?? `0${unit}`;
|
|
246
|
+
let nextValue = '';
|
|
247
|
+
|
|
248
|
+
switch (event.key) {
|
|
249
|
+
case 'Escape':
|
|
250
|
+
case 'Enter':
|
|
251
|
+
if (event.key === 'Escape') {
|
|
252
|
+
input.value = submittedValueRef.current;
|
|
253
|
+
}
|
|
254
|
+
input.blur();
|
|
255
|
+
return;
|
|
256
|
+
case 'ArrowUp':
|
|
257
|
+
case 'ArrowDown':
|
|
258
|
+
nextValue = getNextValue({
|
|
259
|
+
currentValue,
|
|
260
|
+
multiplier: event.shiftKey ? 10 : 1,
|
|
261
|
+
signum: event.key === 'ArrowUp' ? 1 : -1,
|
|
262
|
+
});
|
|
250
263
|
|
|
251
|
-
|
|
252
|
-
return;
|
|
253
|
-
default:
|
|
254
|
-
// No default key handling
|
|
255
|
-
}
|
|
256
|
-
},
|
|
257
|
-
[getNextValue, onKeyDown, placeholder, unit],
|
|
258
|
-
);
|
|
264
|
+
if (nextValue === currentValue) return;
|
|
259
265
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
if (onKeyUp) onKeyUp(event);
|
|
263
|
-
// If this is the key up from ↑ or ↓ keys, time to handleSubmitValue
|
|
264
|
-
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
265
|
-
handleSubmitValue(event);
|
|
266
|
-
}
|
|
267
|
-
},
|
|
268
|
-
[handleSubmitValue, onKeyUp],
|
|
269
|
-
);
|
|
266
|
+
event.stopPropagation();
|
|
267
|
+
event.preventDefault();
|
|
270
268
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
269
|
+
input.value = nextValue;
|
|
270
|
+
return;
|
|
271
|
+
default:
|
|
272
|
+
// No default key handling
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
[getNextValue, onKeyDown, placeholder, unit],
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const handleKeyUp = useCallback(
|
|
279
|
+
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
280
|
+
if (onKeyUp) onKeyUp(event);
|
|
281
|
+
// If this is the key up from ↑ or ↓ keys, time to handleSubmitValue
|
|
282
|
+
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
283
|
+
handleSubmitValue(event);
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
[handleSubmitValue, onKeyUp],
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
return (
|
|
290
|
+
<label className={clsx(ROOT_CLASS_NAME, className, { disabled })} title={title}>
|
|
291
|
+
{icon ? <div className={`${ROOT_CLASS_NAME}-icon`}>{icon}</div> : null}
|
|
292
|
+
{label ? (
|
|
293
|
+
<div className={`${ROOT_CLASS_NAME}-label`}>
|
|
294
|
+
<p className={`${ROOT_CLASS_NAME}-label-text`}>{label}</p>
|
|
297
295
|
</div>
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
296
|
+
) : null}
|
|
297
|
+
<div className={`${ROOT_CLASS_NAME}-value`}>
|
|
298
|
+
<InputText
|
|
299
|
+
disabled={disabled}
|
|
300
|
+
initialValue={value}
|
|
301
|
+
name={name}
|
|
302
|
+
onBlur={handleBlur}
|
|
303
|
+
onChange={onChange}
|
|
304
|
+
onFocus={onFocus}
|
|
305
|
+
onKeyDown={handleKeyDown}
|
|
306
|
+
onKeyUp={handleKeyUp}
|
|
307
|
+
placeholder={placeholder}
|
|
308
|
+
ref={inputRef}
|
|
309
|
+
selectTextOnFocus
|
|
310
|
+
tabIndex={tabIndex}
|
|
311
|
+
/>
|
|
312
|
+
</div>
|
|
313
|
+
</label>
|
|
314
|
+
);
|
|
315
|
+
});
|