@acusti/css-value-input 1.0.0-rc.13 → 1.0.0-rc.14

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.
@@ -39,7 +39,5 @@ export type Props = {
39
39
  validator?: RegExp | ((value: string) => boolean);
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;
@@ -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,203 +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
- (event) => {
54
- const currentValue = event.currentTarget.value;
55
- // Store last submittedValue (used to reset value on invalid input)
56
- submittedValueRef.current = currentValue;
57
- onSubmitValue(currentValue);
58
- },
59
- [onSubmitValue],
60
- );
61
- const handleBlur = useCallback(
62
- (event) => {
63
- const input = event.currentTarget;
64
- inputRef.current = input;
65
- if (onBlur) onBlur(event);
66
- const currentValue = input.value.trim();
67
- // If allowEmpty and value is empty, skip all validation + normalization
68
- if (allowEmpty && !currentValue) {
69
- handleSubmitValue(event);
70
- return;
71
- }
72
- const currentValueAsNumber = getValueAsNumber(currentValue);
73
- const isCurrentValueFinite = Number.isFinite(currentValueAsNumber);
74
- // Inherit unit from last submitted value unless default is unitless;
75
- // ensures that submitting a new value with no unit doesn’t add a unit
76
- const defaultUnit = unit
77
- ? getUnitFromCSSValue({
78
- cssValueType,
79
- defaultUnit: unit,
80
- value: submittedValueRef.current,
81
- })
82
- : '';
83
- if (!isCurrentValueFinite) {
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
- // Normalize value by applying min/max and integer constraints
99
- let normalizedValueAsNumber = currentValueAsNumber;
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 (normalizedValueAsNumber !== currentValueAsNumber) {
110
- const currentUnit = getUnitFromCSSValue({
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
- handleSubmitValue(event);
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
- let nextValue = currentValueAsNumber + modifier;
146
- if (cssValueType === 'integer') {
147
- nextValue = Math.floor(nextValue);
148
- } else {
149
- nextValue = roundToPrecision(nextValue, 5);
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 (typeof max === 'number' && Number.isFinite(max)) {
152
- nextValue = Math.min(max, nextValue);
69
+ else if (max != null && currentValueAsNumber > max) {
70
+ normalizedValueAsNumber = max;
153
71
  }
154
- if (typeof min === 'number' && Number.isFinite(min)) {
155
- nextValue = Math.max(min, nextValue);
72
+ else if (cssValueType === 'integer') {
73
+ normalizedValueAsNumber = Math.floor(currentValueAsNumber);
156
74
  }
157
- const nextUnit = getUnitFromCSSValue({
75
+ }
76
+ if (normalizedValueAsNumber !== currentValueAsNumber) {
77
+ const currentUnit = getUnitFromCSSValue({
158
78
  cssValueType,
159
- defaultUnit: unit,
79
+ defaultUnit,
160
80
  value: currentValue,
161
81
  });
162
- return `${nextValue}${nextUnit}`;
163
- },
164
- [cssValueType, getValueAsNumber, max, min, step, unit],
165
- );
166
- const handleKeyDown = useCallback(
167
- (event) => {
168
- var _a, _b;
169
- const input = event.currentTarget;
170
- inputRef.current = input;
171
- if (onKeyDown) onKeyDown(event);
172
- const currentValue =
173
- (_b = (_a = input.value) !== null && _a !== void 0 ? _a : placeholder) !==
174
- null && _b !== void 0
175
- ? _b
176
- : `0${unit}`;
177
- let nextValue = '';
178
- switch (event.key) {
179
- case 'Escape':
180
- case 'Enter':
181
- if (event.key === 'Escape') {
182
- input.value = submittedValueRef.current;
183
- }
184
- input.blur();
185
- return;
186
- case 'ArrowUp':
187
- case 'ArrowDown':
188
- nextValue = getNextValue({
189
- currentValue,
190
- multiplier: event.shiftKey ? 10 : 1,
191
- signum: event.key === 'ArrowUp' ? 1 : -1,
192
- });
193
- if (nextValue === currentValue) return;
194
- event.stopPropagation();
195
- event.preventDefault();
196
- input.value = nextValue;
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 'Escape':
140
+ case 'Enter':
141
+ if (event.key === 'Escape') {
142
+ input.value = submittedValueRef.current;
143
+ }
144
+ input.blur();
145
+ return;
146
+ case 'ArrowUp':
147
+ case 'ArrowDown':
148
+ nextValue = getNextValue({
149
+ currentValue,
150
+ multiplier: event.shiftKey ? 10 : 1,
151
+ signum: event.key === 'ArrowUp' ? 1 : -1,
152
+ });
153
+ if (nextValue === currentValue)
197
154
  return;
198
- default:
199
- // No default key handling
200
- }
201
- },
202
- [getNextValue, onKeyDown, placeholder, unit],
203
- );
204
- const handleKeyUp = useCallback(
205
- (event) => {
206
- if (onKeyUp) onKeyUp(event);
207
- // If this is the key up from ↑ or ↓ keys, time to handleSubmitValue
208
- if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
209
- handleSubmitValue(event);
210
- }
211
- },
212
- [handleSubmitValue, onKeyUp],
213
- );
214
- return React.createElement(
215
- 'label',
216
- { className: clsx(ROOT_CLASS_NAME, className, { disabled }), title: title },
217
- icon
218
- ? React.createElement('div', { className: `${ROOT_CLASS_NAME}-icon` }, icon)
219
- : null,
220
- label
221
- ? React.createElement(
222
- 'div',
223
- { className: `${ROOT_CLASS_NAME}-label` },
224
- React.createElement(
225
- 'p',
226
- { className: `${ROOT_CLASS_NAME}-label-text` },
227
- label,
228
- ),
229
- )
230
- : null,
231
- React.createElement(
232
- 'div',
233
- { className: `${ROOT_CLASS_NAME}-value` },
234
- React.createElement(InputText, {
235
- disabled: disabled,
236
- initialValue: value,
237
- name: name,
238
- onBlur: handleBlur,
239
- onChange: onChange,
240
- onFocus: onFocus,
241
- onKeyDown: handleKeyDown,
242
- onKeyUp: handleKeyUp,
243
- placeholder: placeholder,
244
- ref: inputRef,
245
- selectTextOnFocus: true,
246
- tabIndex: tabIndex,
247
- }),
248
- ),
249
- );
155
+ event.stopPropagation();
156
+ event.preventDefault();
157
+ input.value = nextValue;
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", { className: clsx(ROOT_CLASS_NAME, className, { disabled }), title: title },
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,
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 }))));
250
177
  });
251
- //# sourceMappingURL=CSSValueInput.js.map
178
+ //# sourceMappingURL=CSSValueInput.js.map
@@ -4,20 +4,18 @@ import userEvent from '@testing-library/user-event';
4
4
  import React from 'react'; // eslint-disable-line @typescript-eslint/no-unused-vars
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
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
15
13
  const input = screen.getByRole('textbox');
16
14
  expect(input.value).toBe('24px');
17
15
  });
18
16
  it('handles ↑/↓ keys to increment/decrement by 1 and ⇧↑/⇧↓ to increment/decrement by 10 (preserving the CSS unit)', async () => {
19
17
  const user = userEvent.setup();
20
- render(React.createElement(CSSValueInput, { onSubmitValue: noop, value: '75%' }));
18
+ render(React.createElement(CSSValueInput, { onSubmitValue: noop, value: "75%" }));
21
19
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
22
20
  const input = screen.getByRole('textbox');
23
21
  expect(input.value).toBe('75%');
@@ -36,13 +34,7 @@ describe('CSSValueInput.tsx', () => {
36
34
  });
37
35
  it('supports custom props.step for ↑/↓ key handling', async () => {
38
36
  const user = userEvent.setup();
39
- render(
40
- React.createElement(CSSValueInput, {
41
- onSubmitValue: noop,
42
- step: 0.1,
43
- value: '2rem',
44
- }),
45
- );
37
+ render(React.createElement(CSSValueInput, { onSubmitValue: noop, step: 0.1, value: "2rem" }));
46
38
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
47
39
  const input = screen.getByRole('textbox');
48
40
  expect(input.value).toBe('2rem');
@@ -61,14 +53,7 @@ describe('CSSValueInput.tsx', () => {
61
53
  });
62
54
  it('uses props.unit as default unit when unit is missing', async () => {
63
55
  const user = userEvent.setup();
64
- render(
65
- React.createElement(CSSValueInput, {
66
- allowEmpty: true,
67
- onSubmitValue: noop,
68
- unit: 'px',
69
- value: '',
70
- }),
71
- );
56
+ render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "px", value: "" }));
72
57
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
73
58
  const input = screen.getByRole('textbox');
74
59
  expect(input.value).toBe('');
@@ -77,14 +62,7 @@ describe('CSSValueInput.tsx', () => {
77
62
  });
78
63
  it('preserves last entered unit if different from props.unit when unit is missing', async () => {
79
64
  const user = userEvent.setup();
80
- render(
81
- React.createElement(CSSValueInput, {
82
- allowEmpty: true,
83
- onSubmitValue: noop,
84
- unit: 'px',
85
- value: '',
86
- }),
87
- );
65
+ render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "px", value: "" }));
88
66
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
89
67
  const input = screen.getByRole('textbox');
90
68
  expect(input.value).toBe('');
@@ -95,14 +73,7 @@ describe('CSSValueInput.tsx', () => {
95
73
  });
96
74
  it('treats value as numeric if props.unit is an empty string', async () => {
97
75
  const user = userEvent.setup();
98
- render(
99
- React.createElement(CSSValueInput, {
100
- allowEmpty: true,
101
- onSubmitValue: noop,
102
- unit: '',
103
- value: '100',
104
- }),
105
- );
76
+ render(React.createElement(CSSValueInput, { allowEmpty: true, onSubmitValue: noop, unit: "", value: "100" }));
106
77
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
107
78
  const input = screen.getByRole('textbox');
108
79
  expect(input.value).toBe('100');
@@ -117,35 +88,17 @@ describe('CSSValueInput.tsx', () => {
117
88
  });
118
89
  it('updates default unit as props.unit and props.value changes', async () => {
119
90
  const user = userEvent.setup();
120
- const { rerender } = render(
121
- React.createElement(CSSValueInput, {
122
- onSubmitValue: noop,
123
- unit: 'px',
124
- value: '12px',
125
- }),
126
- );
91
+ const { rerender } = render(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "px", value: "12px" }));
127
92
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
128
93
  const input = screen.getByRole('textbox');
129
- rerender(
130
- React.createElement(CSSValueInput, {
131
- onSubmitValue: noop,
132
- unit: '',
133
- value: '4',
134
- }),
135
- );
94
+ rerender(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "", value: "4" }));
136
95
  expect(input.value).toBe('4');
137
96
  await user.type(input, '25{Enter}');
138
97
  expect(input.value).toBe('25');
139
- rerender(
140
- React.createElement(CSSValueInput, {
141
- onSubmitValue: noop,
142
- unit: 'rad',
143
- value: '3rad',
144
- }),
145
- );
98
+ rerender(React.createElement(CSSValueInput, { onSubmitValue: noop, unit: "rad", value: "3rad" }));
146
99
  expect(input.value).toBe('3rad');
147
100
  await user.type(input, '-4.1{Enter}');
148
101
  expect(input.value).toBe('-4.1rad');
149
102
  });
150
103
  });
151
- //# sourceMappingURL=CSSValueInput.test.js.map
104
+ //# 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.0.0-rc.13",
3
+ "version": "1.0.0-rc.14",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "exports": "./dist/CSSValueInput.js",
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "@acusti/css-values": "^1.0.2",
54
- "@acusti/input-text": "^1.5.3",
54
+ "@acusti/input-text": "^1.6.0",
55
55
  "clsx": "^2"
56
56
  },
57
57
  "peerDependencies": {