@react-aria/numberfield 3.12.3 → 3.12.5
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/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/useNumberField.main.js +39 -3
- package/dist/useNumberField.main.js.map +1 -1
- package/dist/useNumberField.mjs +41 -5
- package/dist/useNumberField.module.js +41 -5
- package/dist/useNumberField.module.js.map +1 -1
- package/package.json +13 -12
- package/src/useNumberField.ts +37 -5
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AriaButtonProps } from "@react-types/button";
|
|
2
2
|
import { AriaNumberFieldProps } from "@react-types/numberfield";
|
|
3
|
-
import { DOMAttributes, GroupDOMAttributes, ValidationResult } from "@react-types/shared";
|
|
4
3
|
import { InputHTMLAttributes, LabelHTMLAttributes, RefObject } from "react";
|
|
4
|
+
import { DOMAttributes, GroupDOMAttributes, ValidationResult } from "@react-types/shared";
|
|
5
5
|
import { NumberFieldState } from "@react-stately/numberfield";
|
|
6
6
|
export interface NumberFieldAria extends ValidationResult {
|
|
7
7
|
/** Props for the label element. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;
|
|
1
|
+
{"mappings":";;;;;AAwCA,gCAAiC,SAAQ,gBAAgB;IACvD,mCAAmC;IACnC,UAAU,EAAE,oBAAoB,gBAAgB,CAAC,CAAC;IAClD,wEAAwE;IACxE,UAAU,EAAE,kBAAkB,CAAC;IAC/B,mCAAmC;IACnC,UAAU,EAAE,oBAAoB,gBAAgB,CAAC,CAAC;IAClD,mEAAmE;IACnE,oBAAoB,EAAE,eAAe,CAAC;IACtC,mEAAmE;IACnE,oBAAoB,EAAE,eAAe,CAAC;IACtC,gEAAgE;IAChE,gBAAgB,EAAE,aAAa,CAAC;IAChC,kEAAkE;IAClE,iBAAiB,EAAE,aAAa,CAAA;CACjC;AAED;;;GAGG;AACH,+BAA+B,KAAK,EAAE,oBAAoB,EAAE,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,UAAU,gBAAgB,GAAG,IAAI,CAAC,GAAG,eAAe,CA6SlJ;AC9VD,YAAY,EAAC,oBAAoB,EAAC,MAAM,0BAA0B,CAAC","sources":["packages/@react-aria/numberfield/src/packages/@react-aria/numberfield/src/useNumberField.ts","packages/@react-aria/numberfield/src/packages/@react-aria/numberfield/src/index.ts","packages/@react-aria/numberfield/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\nexport {useNumberField} from './useNumberField';\nexport type {AriaNumberFieldProps} from '@react-types/numberfield';\nexport type {NumberFieldAria} from './useNumberField';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
var $4932e21065cdc2cd$exports = require("./intlStrings.main.js");
|
|
2
|
+
var $9WaOX$reactarialiveannouncer = require("@react-aria/live-announcer");
|
|
2
3
|
var $9WaOX$reactariautils = require("@react-aria/utils");
|
|
3
4
|
var $9WaOX$react = require("react");
|
|
5
|
+
var $9WaOX$reactdom = require("react-dom");
|
|
4
6
|
var $9WaOX$reactstatelyform = require("@react-stately/form");
|
|
5
7
|
var $9WaOX$reactariainteractions = require("@react-aria/interactions");
|
|
6
8
|
var $9WaOX$reactariatextfield = require("@react-aria/textfield");
|
|
@@ -35,15 +37,30 @@ $parcel$export(module.exports, "useNumberField", () => $fa863e9b015ae839$export$
|
|
|
35
37
|
|
|
36
38
|
|
|
37
39
|
|
|
40
|
+
|
|
41
|
+
|
|
38
42
|
function $fa863e9b015ae839$export$23f548e970bdf099(props, state, inputRef) {
|
|
39
43
|
let { id: id, decrementAriaLabel: decrementAriaLabel, incrementAriaLabel: incrementAriaLabel, isDisabled: isDisabled, isReadOnly: isReadOnly, isRequired: isRequired, minValue: minValue, maxValue: maxValue, autoFocus: autoFocus, label: label, formatOptions: formatOptions, onBlur: onBlur = ()=>{}, onFocus: onFocus, onFocusChange: onFocusChange, onKeyDown: onKeyDown, onKeyUp: onKeyUp, description: description, errorMessage: errorMessage, isWheelDisabled: isWheelDisabled, ...otherProps } = props;
|
|
40
44
|
let { increment: increment, incrementToMax: incrementToMax, decrement: decrement, decrementToMin: decrementToMin, numberValue: numberValue, inputValue: inputValue, commit: commit, commitValidation: commitValidation } = state;
|
|
41
45
|
const stringFormatter = (0, $9WaOX$reactariai18n.useLocalizedStringFormatter)((0, ($parcel$interopDefault($4932e21065cdc2cd$exports))), '@react-aria/numberfield');
|
|
46
|
+
let commitAndAnnounce = (0, $9WaOX$react.useCallback)(()=>{
|
|
47
|
+
var _inputRef_current, _inputRef_current1, _inputRef_current2;
|
|
48
|
+
var _inputRef_current_value;
|
|
49
|
+
let oldValue = (_inputRef_current_value = (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.value) !== null && _inputRef_current_value !== void 0 ? _inputRef_current_value : '';
|
|
50
|
+
// Set input value to normalized valid value
|
|
51
|
+
(0, $9WaOX$reactdom.flushSync)(()=>{
|
|
52
|
+
commit();
|
|
53
|
+
});
|
|
54
|
+
var _inputRef_current_value1;
|
|
55
|
+
if (((_inputRef_current1 = inputRef.current) === null || _inputRef_current1 === void 0 ? void 0 : _inputRef_current1.value) !== oldValue) (0, $9WaOX$reactarialiveannouncer.announce)((_inputRef_current_value1 = (_inputRef_current2 = inputRef.current) === null || _inputRef_current2 === void 0 ? void 0 : _inputRef_current2.value) !== null && _inputRef_current_value1 !== void 0 ? _inputRef_current_value1 : '', 'assertive');
|
|
56
|
+
}, [
|
|
57
|
+
commit,
|
|
58
|
+
inputRef
|
|
59
|
+
]);
|
|
42
60
|
let inputId = (0, $9WaOX$reactariautils.useId)(id);
|
|
43
61
|
let { focusProps: focusProps } = (0, $9WaOX$reactariainteractions.useFocus)({
|
|
44
62
|
onBlur () {
|
|
45
|
-
|
|
46
|
-
commit();
|
|
63
|
+
commitAndAnnounce();
|
|
47
64
|
}
|
|
48
65
|
});
|
|
49
66
|
let numberFormatter = (0, $9WaOX$reactariai18n.useNumberFormatter)(formatOptions);
|
|
@@ -119,6 +136,24 @@ function $fa863e9b015ae839$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
119
136
|
let onChange = (value)=>{
|
|
120
137
|
if (state.validate(value)) state.setInputValue(value);
|
|
121
138
|
};
|
|
139
|
+
let onPaste = (e)=>{
|
|
140
|
+
var _props_onPaste;
|
|
141
|
+
(_props_onPaste = props.onPaste) === null || _props_onPaste === void 0 ? void 0 : _props_onPaste.call(props, e);
|
|
142
|
+
let inputElement = (0, $9WaOX$reactariautils.getEventTarget)(e);
|
|
143
|
+
var _inputElement_selectionEnd, _inputElement_selectionStart;
|
|
144
|
+
// we can only handle the case where the paste takes over the entire input, otherwise things get very complicated
|
|
145
|
+
// trying to calculate the new string based on what the paste is replacing and where in the source string it is
|
|
146
|
+
if (inputElement && ((_inputElement_selectionEnd = inputElement.selectionEnd) !== null && _inputElement_selectionEnd !== void 0 ? _inputElement_selectionEnd : -1) - ((_inputElement_selectionStart = inputElement.selectionStart) !== null && _inputElement_selectionStart !== void 0 ? _inputElement_selectionStart : 0) === inputElement.value.length) {
|
|
147
|
+
var _e_clipboardData_getData, _e_clipboardData_getData1, _e_clipboardData;
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
var _e_clipboardData_getData_trim;
|
|
150
|
+
// commit so that the user gets to see what it formats to immediately
|
|
151
|
+
// paste happens before inputRef's value is updated, so have to prevent the default and do it ourselves
|
|
152
|
+
// spin button will then handle announcing the new value, this should work with controlled state as well
|
|
153
|
+
// because the announcement is done as a result of the new rendered input value if there is one
|
|
154
|
+
commit((_e_clipboardData_getData_trim = (_e_clipboardData = e.clipboardData) === null || _e_clipboardData === void 0 ? void 0 : (_e_clipboardData_getData1 = _e_clipboardData.getData) === null || _e_clipboardData_getData1 === void 0 ? void 0 : (_e_clipboardData_getData = _e_clipboardData_getData1.call(_e_clipboardData, 'text/plain')) === null || _e_clipboardData_getData === void 0 ? void 0 : _e_clipboardData_getData.trim()) !== null && _e_clipboardData_getData_trim !== void 0 ? _e_clipboardData_getData_trim : '');
|
|
155
|
+
}
|
|
156
|
+
};
|
|
122
157
|
let domProps = (0, $9WaOX$reactariautils.filterDOMProps)(props);
|
|
123
158
|
let onKeyDownEnter = (0, $9WaOX$react.useCallback)((e)=>{
|
|
124
159
|
if (e.nativeEvent.isComposing) return;
|
|
@@ -161,6 +196,7 @@ function $fa863e9b015ae839$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
161
196
|
onKeyDown
|
|
162
197
|
]),
|
|
163
198
|
onKeyUp: onKeyUp,
|
|
199
|
+
onPaste: onPaste,
|
|
164
200
|
description: description,
|
|
165
201
|
errorMessage: errorMessage
|
|
166
202
|
}, state, inputRef);
|
|
@@ -182,7 +218,7 @@ function $fa863e9b015ae839$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
182
218
|
var _inputRef_current;
|
|
183
219
|
// If focus is already on the input, keep it there so we don't hide the
|
|
184
220
|
// software keyboard when tapping the increment/decrement buttons.
|
|
185
|
-
if (
|
|
221
|
+
if ((0, $9WaOX$reactariautils.getActiveElement)() === inputRef.current) return;
|
|
186
222
|
// Otherwise, when using a mouse, move focus to the input.
|
|
187
223
|
// On touch, or with a screen reader, focus the button so that the software
|
|
188
224
|
// keyboard does not appear and the screen reader cursor is not moved off the button.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;;;AA+CM,SAAS,0CAA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AAkB,CAAC,qBAAqB,GAAG,YAAY,CAAC,EAAE,gBAAgB,GAAG;QAChG,iBAAiB;QACjB,qBAAqB;QACrB,qBAAqB;QACrB,wBAAwB;QACxB,YAAY,CAAC,MAAM,YAAY;QAC/B,cAAc;IAChB;IAEA,OAAO;QACL,YAAY;YACV,GAAG,gBAAgB;YACnB,MAAM;YACN,iBAAiB;YACjB,gBAAgB,YAAY,SAAS;QACvC;oBACA;oBACA;8BACA;8BACA;2BACA;0BACA;mBACA;0BACA;2BACA;IACF;AACF","sources":["packages/@react-aria/numberfield/src/useNumberField.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {AriaButtonProps} from '@react-types/button';\nimport {AriaNumberFieldProps} from '@react-types/numberfield';\nimport {chain, filterDOMProps, isAndroid, isIOS, isIPhone, mergeProps, useFormReset, useId} from '@react-aria/utils';\nimport {DOMAttributes, GroupDOMAttributes, TextInputDOMProps, ValidationResult} from '@react-types/shared';\nimport {\n InputHTMLAttributes,\n LabelHTMLAttributes,\n RefObject,\n useCallback,\n useMemo,\n useState\n} from 'react';\n// @ts-ignore\nimport intlMessages from '../intl/*.json';\nimport {NumberFieldState} from '@react-stately/numberfield';\nimport {privateValidationStateProp} from '@react-stately/form';\nimport {useFocus, useFocusWithin, useScrollWheel} from '@react-aria/interactions';\nimport {useFormattedTextField} from '@react-aria/textfield';\nimport {\n useLocalizedStringFormatter,\n useNumberFormatter\n} from '@react-aria/i18n';\nimport {useSpinButton} from '@react-aria/spinbutton';\n\nexport interface NumberFieldAria extends ValidationResult {\n /** Props for the label element. */\n labelProps: LabelHTMLAttributes<HTMLLabelElement>,\n /** Props for the group wrapper around the input and stepper buttons. */\n groupProps: GroupDOMAttributes,\n /** Props for the input element. */\n inputProps: InputHTMLAttributes<HTMLInputElement>,\n /** Props for the increment button, to be passed to `useButton`. */\n incrementButtonProps: AriaButtonProps,\n /** Props for the decrement button, to be passed to `useButton`. */\n decrementButtonProps: AriaButtonProps,\n /** Props for the number field's description element, if any. */\n descriptionProps: DOMAttributes,\n /** Props for the number field's error message element, if any. */\n errorMessageProps: DOMAttributes\n}\n\n/**\n * Provides the behavior and accessibility implementation for a number field component.\n * Number fields allow users to enter a number, and increment or decrement the value using stepper buttons.\n */\nexport function useNumberField(props: AriaNumberFieldProps, state: NumberFieldState, inputRef: RefObject<HTMLInputElement | null>): NumberFieldAria {\n let {\n id,\n decrementAriaLabel,\n incrementAriaLabel,\n isDisabled,\n isReadOnly,\n isRequired,\n minValue,\n maxValue,\n autoFocus,\n label,\n formatOptions,\n onBlur = () => {},\n onFocus,\n onFocusChange,\n onKeyDown,\n onKeyUp,\n description,\n errorMessage,\n isWheelDisabled,\n ...otherProps\n } = props;\n\n let {\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n numberValue,\n inputValue,\n commit,\n commitValidation\n } = state;\n\n const stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/numberfield');\n\n let inputId = useId(id);\n let {focusProps} = useFocus({\n onBlur() {\n // Set input value to normalized valid value\n commit();\n }\n });\n\n let numberFormatter = useNumberFormatter(formatOptions);\n let intlOptions = useMemo(() => numberFormatter.resolvedOptions(), [numberFormatter]);\n\n // Replace negative textValue formatted using currencySign: 'accounting'\n // with a textValue that can be announced using a minus sign.\n let textValueFormatter = useNumberFormatter({...formatOptions, currencySign: undefined});\n let textValue = useMemo(() => isNaN(numberValue) ? '' : textValueFormatter.format(numberValue), [textValueFormatter, numberValue]);\n\n let {\n spinButtonProps,\n incrementButtonProps: incButtonProps,\n decrementButtonProps: decButtonProps\n } = useSpinButton(\n {\n isDisabled,\n isReadOnly,\n isRequired,\n maxValue,\n minValue,\n onIncrement: increment,\n onIncrementToMax: incrementToMax,\n onDecrement: decrement,\n onDecrementToMin: decrementToMin,\n value: numberValue,\n textValue\n }\n );\n\n let [focusWithin, setFocusWithin] = useState(false);\n let {focusWithinProps} = useFocusWithin({isDisabled, onFocusWithinChange: setFocusWithin});\n\n let onWheel = useCallback((e) => {\n // if on a trackpad, users can scroll in both X and Y at once, check the magnitude of the change\n // if it's mostly in the X direction, then just return, the user probably doesn't mean to inc/dec\n // this isn't perfect, events come in fast with small deltas and a part of the scroll may give a false indication\n // especially if the user is scrolling near 45deg\n if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) {\n return;\n }\n if (e.deltaY > 0) {\n increment();\n } else if (e.deltaY < 0) {\n decrement();\n }\n }, [decrement, increment]);\n // If the input isn't supposed to receive input, disable scrolling.\n let scrollingDisabled = isWheelDisabled || isDisabled || isReadOnly || !focusWithin;\n useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, inputRef);\n\n // The inputMode attribute influences the software keyboard that is shown on touch devices.\n // Browsers and operating systems are quite inconsistent about what keys are available, however.\n // We choose between numeric and decimal based on whether we allow negative and fractional numbers,\n // and based on testing on various devices to determine what keys are available in each inputMode.\n let hasDecimals = (intlOptions.maximumFractionDigits ?? 0) > 0;\n let hasNegative = (state.minValue === undefined || isNaN(state.minValue)) || state.minValue < 0;\n let inputMode: TextInputDOMProps['inputMode'] = 'numeric';\n if (isIPhone()) {\n // iPhone doesn't have a minus sign in either numeric or decimal.\n // Note this is only for iPhone, not iPad, which always has both\n // minus and decimal in numeric.\n if (hasNegative) {\n inputMode = 'text';\n } else if (hasDecimals) {\n inputMode = 'decimal';\n }\n } else if (isAndroid()) {\n // Android numeric has both a decimal point and minus key.\n // decimal does not have a minus key.\n if (hasNegative) {\n inputMode = 'numeric';\n } else if (hasDecimals) {\n inputMode = 'decimal';\n }\n }\n\n let onChange = value => {\n if (state.validate(value)) {\n state.setInputValue(value);\n }\n };\n\n let domProps = filterDOMProps(props);\n let onKeyDownEnter = useCallback((e) => {\n if (e.nativeEvent.isComposing) {\n return;\n }\n\n if (e.key === 'Enter') {\n commit();\n commitValidation();\n } else {\n e.continuePropagation();\n }\n }, [commit, commitValidation]);\n\n let {isInvalid, validationErrors, validationDetails} = state.displayValidation;\n let {labelProps, inputProps: textFieldProps, descriptionProps, errorMessageProps} = useFormattedTextField({\n ...otherProps,\n ...domProps,\n // These props are added to a hidden input rather than the formatted textfield.\n name: undefined,\n form: undefined,\n label,\n autoFocus,\n isDisabled,\n isReadOnly,\n isRequired,\n validate: undefined,\n [privateValidationStateProp]: state,\n value: inputValue,\n defaultValue: '!', // an invalid value so that form reset is ignored in onChange above\n autoComplete: 'off',\n 'aria-label': props['aria-label'] || undefined,\n 'aria-labelledby': props['aria-labelledby'] || undefined,\n id: inputId,\n type: 'text', // Can't use type=\"number\" because then we can't have things like $ in the field.\n inputMode,\n onChange,\n onBlur,\n onFocus,\n onFocusChange,\n onKeyDown: useMemo(() => chain(onKeyDownEnter, onKeyDown), [onKeyDownEnter, onKeyDown]),\n onKeyUp,\n description,\n errorMessage\n }, state, inputRef);\n\n useFormReset(inputRef, state.defaultNumberValue, state.setNumberValue);\n\n let inputProps: InputHTMLAttributes<HTMLInputElement> = mergeProps(\n spinButtonProps,\n focusProps,\n textFieldProps,\n {\n // override the spinbutton role, we can't focus a spin button with VO\n role: null,\n // ignore aria-roledescription on iOS so that required state will announce when it is present\n 'aria-roledescription': (!isIOS() ? stringFormatter.format('numberField') : null),\n 'aria-valuemax': null,\n 'aria-valuemin': null,\n 'aria-valuenow': null,\n 'aria-valuetext': null,\n autoCorrect: 'off',\n spellCheck: 'false'\n }\n );\n\n if (props.validationBehavior === 'native') {\n inputProps['aria-required'] = undefined;\n }\n\n let onButtonPressStart = (e) => {\n // If focus is already on the input, keep it there so we don't hide the\n // software keyboard when tapping the increment/decrement buttons.\n if (document.activeElement === inputRef.current) {\n return;\n }\n\n // Otherwise, when using a mouse, move focus to the input.\n // On touch, or with a screen reader, focus the button so that the software\n // keyboard does not appear and the screen reader cursor is not moved off the button.\n if (e.pointerType === 'mouse') {\n inputRef.current?.focus();\n } else {\n e.target.focus();\n }\n };\n\n // Determine the label for the increment and decrement buttons. There are 4 cases:\n //\n // 1. With a visible label that is a string: aria-label: `Increase ${props.label}`\n // 2. With a visible label that is JSX: aria-label: 'Increase', aria-labelledby: '${incrementId} ${labelId}'\n // 3. With an aria-label: aria-label: `Increase ${props['aria-label']}`\n // 4. With an aria-labelledby: aria-label: 'Increase', aria-labelledby: `${incrementId} ${props['aria-labelledby']}`\n //\n // (1) and (2) could possibly be combined and both use aria-labelledby. However, placing the label in\n // the aria-label string rather than using aria-labelledby gives more flexibility to translators to change\n // the order or add additional words around the label if needed.\n let fieldLabel = props['aria-label'] || (typeof props.label === 'string' ? props.label : '');\n let ariaLabelledby: string | undefined;\n if (!fieldLabel) {\n ariaLabelledby = props.label != null ? labelProps.id : props['aria-labelledby'];\n }\n\n let incrementId = useId();\n let decrementId = useId();\n\n let incrementButtonProps: AriaButtonProps = mergeProps(incButtonProps, {\n 'aria-label': incrementAriaLabel || stringFormatter.format('increase', {fieldLabel}).trim(),\n id: ariaLabelledby && !incrementAriaLabel ? incrementId : null,\n 'aria-labelledby': ariaLabelledby && !incrementAriaLabel ? `${incrementId} ${ariaLabelledby}` : null,\n 'aria-controls': inputId,\n excludeFromTabOrder: true,\n preventFocusOnPress: true,\n allowFocusWhenDisabled: true,\n isDisabled: !state.canIncrement,\n onPressStart: onButtonPressStart\n });\n\n let decrementButtonProps: AriaButtonProps = mergeProps(decButtonProps, {\n 'aria-label': decrementAriaLabel || stringFormatter.format('decrease', {fieldLabel}).trim(),\n id: ariaLabelledby && !decrementAriaLabel ? decrementId : null,\n 'aria-labelledby': ariaLabelledby && !decrementAriaLabel ? `${decrementId} ${ariaLabelledby}` : null,\n 'aria-controls': inputId,\n excludeFromTabOrder: true,\n preventFocusOnPress: true,\n allowFocusWhenDisabled: true,\n isDisabled: !state.canDecrement,\n onPressStart: onButtonPressStart\n });\n\n return {\n groupProps: {\n ...focusWithinProps,\n role: 'group',\n 'aria-disabled': isDisabled,\n 'aria-invalid': isInvalid ? 'true' : undefined\n },\n labelProps,\n inputProps,\n incrementButtonProps,\n decrementButtonProps,\n errorMessageProps,\n descriptionProps,\n isInvalid,\n validationErrors,\n validationDetails\n };\n}\n"],"names":[],"version":3,"file":"useNumberField.main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;;;;;AAmDM,SAAS,0CAAe,KAA2B,EAAE,KAAuB,EAAE,QAA4C;IAC/H,IAAI,MACF,EAAE,sBACF,kBAAkB,sBAClB,kBAAkB,cAClB,UAAU,cACV,UAAU,cACV,UAAU,YACV,QAAQ,YACR,QAAQ,aACR,SAAS,SACT,KAAK,iBACL,aAAa,UACb,SAAS,KAAO,YAChB,OAAO,iBACP,aAAa,aACb,SAAS,WACT,OAAO,eACP,WAAW,gBACX,YAAY,mBACZ,eAAe,EACf,GAAG,YACJ,GAAG;IAEJ,IAAI,aACF,SAAS,kBACT,cAAc,aACd,SAAS,kBACT,cAAc,eACd,WAAW,cACX,UAAU,UACV,MAAM,oBACN,gBAAgB,EACjB,GAAG;IAEJ,MAAM,kBAAkB,CAAA,GAAA,gDAA0B,EAAE,CAAA,GAAA,mDAAW,GAAG;IAClE,IAAI,oBAAoB,CAAA,GAAA,wBAAU,EAAE;YACnB,mBAMX,oBACO;YAPI;QAAf,IAAI,WAAW,CAAA,2BAAA,oBAAA,SAAS,OAAO,cAAhB,wCAAA,kBAAkB,KAAK,cAAvB,qCAAA,0BAA2B;QAC1C,4CAA4C;QAC5C,CAAA,GAAA,yBAAQ,EAAE;YACR;QACF;YAGW;QADX,IAAI,EAAA,qBAAA,SAAS,OAAO,cAAhB,yCAAA,mBAAkB,KAAK,MAAK,UAC9B,CAAA,GAAA,sC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iBAAiB;QACjB,qBAAqB;QACrB,qBAAqB;QACrB,wBAAwB;QACxB,YAAY,CAAC,MAAM,YAAY;QAC/B,cAAc;IAChB;IAEA,IAAI,uBAAwC,CAAA,GAAA,gCAAS,EAAE,gBAAgB;QACrE,cAAc,sBAAsB,gBAAgB,MAAM,CAAC,YAAY;wBAAC;QAAU,GAAG,IAAI;QACzF,IAAI,kBAAkB,CAAC,qBAAqB,cAAc;QAC1D,mBAAmB,kBAAkB,CAAC,qBAAqB,GAAG,YAAY,CAAC,EAAE,gBAAgB,GAAG;QAChG,iBAAiB;QACjB,qBAAqB;QACrB,qBAAqB;QACrB,wBAAwB;QACxB,YAAY,CAAC,MAAM,YAAY;QAC/B,cAAc;IAChB;IAEA,OAAO;QACL,YAAY;YACV,GAAG,gBAAgB;YACnB,MAAM;YACN,iBAAiB;YACjB,gBAAgB,YAAY,SAAS;QACvC;oBACA;oBACA;8BACA;8BACA;2BACA;0BACA;mBACA;0BACA;2BACA;IACF;AACF","sources":["packages/@react-aria/numberfield/src/useNumberField.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {announce} from '@react-aria/live-announcer';\nimport {AriaButtonProps} from '@react-types/button';\nimport {AriaNumberFieldProps} from '@react-types/numberfield';\nimport {chain, filterDOMProps, getActiveElement, getEventTarget, isAndroid, isIOS, isIPhone, mergeProps, useFormReset, useId} from '@react-aria/utils';\nimport {\n type ClipboardEvent,\n type ClipboardEventHandler,\n InputHTMLAttributes,\n LabelHTMLAttributes,\n RefObject,\n useCallback,\n useMemo,\n useState\n} from 'react';\nimport {DOMAttributes, GroupDOMAttributes, TextInputDOMProps, ValidationResult} from '@react-types/shared';\nimport {flushSync} from 'react-dom';\n// @ts-ignore\nimport intlMessages from '../intl/*.json';\nimport {NumberFieldState} from '@react-stately/numberfield';\nimport {privateValidationStateProp} from '@react-stately/form';\nimport {useFocus, useFocusWithin, useScrollWheel} from '@react-aria/interactions';\nimport {useFormattedTextField} from '@react-aria/textfield';\nimport {\n useLocalizedStringFormatter,\n useNumberFormatter\n} from '@react-aria/i18n';\nimport {useSpinButton} from '@react-aria/spinbutton';\n\nexport interface NumberFieldAria extends ValidationResult {\n /** Props for the label element. */\n labelProps: LabelHTMLAttributes<HTMLLabelElement>,\n /** Props for the group wrapper around the input and stepper buttons. */\n groupProps: GroupDOMAttributes,\n /** Props for the input element. */\n inputProps: InputHTMLAttributes<HTMLInputElement>,\n /** Props for the increment button, to be passed to `useButton`. */\n incrementButtonProps: AriaButtonProps,\n /** Props for the decrement button, to be passed to `useButton`. */\n decrementButtonProps: AriaButtonProps,\n /** Props for the number field's description element, if any. */\n descriptionProps: DOMAttributes,\n /** Props for the number field's error message element, if any. */\n errorMessageProps: DOMAttributes\n}\n\n/**\n * Provides the behavior and accessibility implementation for a number field component.\n * Number fields allow users to enter a number, and increment or decrement the value using stepper buttons.\n */\nexport function useNumberField(props: AriaNumberFieldProps, state: NumberFieldState, inputRef: RefObject<HTMLInputElement | null>): NumberFieldAria {\n let {\n id,\n decrementAriaLabel,\n incrementAriaLabel,\n isDisabled,\n isReadOnly,\n isRequired,\n minValue,\n maxValue,\n autoFocus,\n label,\n formatOptions,\n onBlur = () => {},\n onFocus,\n onFocusChange,\n onKeyDown,\n onKeyUp,\n description,\n errorMessage,\n isWheelDisabled,\n ...otherProps\n } = props;\n\n let {\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n numberValue,\n inputValue,\n commit,\n commitValidation\n } = state;\n\n const stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/numberfield');\n let commitAndAnnounce = useCallback(() => {\n let oldValue = inputRef.current?.value ?? '';\n // Set input value to normalized valid value\n flushSync(() => {\n commit();\n });\n\n if (inputRef.current?.value !== oldValue) {\n announce(inputRef.current?.value ?? '', 'assertive');\n }\n }, [commit, inputRef]);\n\n let inputId = useId(id);\n let {focusProps} = useFocus({\n onBlur() {\n commitAndAnnounce();\n }\n });\n\n let numberFormatter = useNumberFormatter(formatOptions);\n let intlOptions = useMemo(() => numberFormatter.resolvedOptions(), [numberFormatter]);\n\n // Replace negative textValue formatted using currencySign: 'accounting'\n // with a textValue that can be announced using a minus sign.\n let textValueFormatter = useNumberFormatter({...formatOptions, currencySign: undefined});\n let textValue = useMemo(() => isNaN(numberValue) ? '' : textValueFormatter.format(numberValue), [textValueFormatter, numberValue]);\n\n let {\n spinButtonProps,\n incrementButtonProps: incButtonProps,\n decrementButtonProps: decButtonProps\n } = useSpinButton(\n {\n isDisabled,\n isReadOnly,\n isRequired,\n maxValue,\n minValue,\n onIncrement: increment,\n onIncrementToMax: incrementToMax,\n onDecrement: decrement,\n onDecrementToMin: decrementToMin,\n value: numberValue,\n textValue\n }\n );\n\n let [focusWithin, setFocusWithin] = useState(false);\n let {focusWithinProps} = useFocusWithin({isDisabled, onFocusWithinChange: setFocusWithin});\n\n let onWheel = useCallback((e) => {\n // if on a trackpad, users can scroll in both X and Y at once, check the magnitude of the change\n // if it's mostly in the X direction, then just return, the user probably doesn't mean to inc/dec\n // this isn't perfect, events come in fast with small deltas and a part of the scroll may give a false indication\n // especially if the user is scrolling near 45deg\n if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) {\n return;\n }\n if (e.deltaY > 0) {\n increment();\n } else if (e.deltaY < 0) {\n decrement();\n }\n }, [decrement, increment]);\n // If the input isn't supposed to receive input, disable scrolling.\n let scrollingDisabled = isWheelDisabled || isDisabled || isReadOnly || !focusWithin;\n useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, inputRef);\n\n // The inputMode attribute influences the software keyboard that is shown on touch devices.\n // Browsers and operating systems are quite inconsistent about what keys are available, however.\n // We choose between numeric and decimal based on whether we allow negative and fractional numbers,\n // and based on testing on various devices to determine what keys are available in each inputMode.\n let hasDecimals = (intlOptions.maximumFractionDigits ?? 0) > 0;\n let hasNegative = (state.minValue === undefined || isNaN(state.minValue)) || state.minValue < 0;\n let inputMode: TextInputDOMProps['inputMode'] = 'numeric';\n if (isIPhone()) {\n // iPhone doesn't have a minus sign in either numeric or decimal.\n // Note this is only for iPhone, not iPad, which always has both\n // minus and decimal in numeric.\n if (hasNegative) {\n inputMode = 'text';\n } else if (hasDecimals) {\n inputMode = 'decimal';\n }\n } else if (isAndroid()) {\n // Android numeric has both a decimal point and minus key.\n // decimal does not have a minus key.\n if (hasNegative) {\n inputMode = 'numeric';\n } else if (hasDecimals) {\n inputMode = 'decimal';\n }\n }\n\n let onChange = value => {\n if (state.validate(value)) {\n state.setInputValue(value);\n }\n };\n\n let onPaste: ClipboardEventHandler<HTMLInputElement> = (e: ClipboardEvent<HTMLInputElement>) => {\n props.onPaste?.(e);\n let inputElement = getEventTarget(e) as HTMLInputElement;\n // we can only handle the case where the paste takes over the entire input, otherwise things get very complicated\n // trying to calculate the new string based on what the paste is replacing and where in the source string it is\n if (inputElement &&\n ((inputElement.selectionEnd ?? -1) - (inputElement.selectionStart ?? 0)) === inputElement.value.length\n ) {\n e.preventDefault();\n // commit so that the user gets to see what it formats to immediately\n // paste happens before inputRef's value is updated, so have to prevent the default and do it ourselves\n // spin button will then handle announcing the new value, this should work with controlled state as well\n // because the announcement is done as a result of the new rendered input value if there is one\n commit(e.clipboardData?.getData?.('text/plain')?.trim() ?? '');\n }\n };\n\n let domProps = filterDOMProps(props);\n let onKeyDownEnter = useCallback((e) => {\n if (e.nativeEvent.isComposing) {\n return;\n }\n\n if (e.key === 'Enter') {\n commit();\n commitValidation();\n } else {\n e.continuePropagation();\n }\n }, [commit, commitValidation]);\n\n let {isInvalid, validationErrors, validationDetails} = state.displayValidation;\n let {labelProps, inputProps: textFieldProps, descriptionProps, errorMessageProps} = useFormattedTextField({\n ...otherProps,\n ...domProps,\n // These props are added to a hidden input rather than the formatted textfield.\n name: undefined,\n form: undefined,\n label,\n autoFocus,\n isDisabled,\n isReadOnly,\n isRequired,\n validate: undefined,\n [privateValidationStateProp]: state,\n value: inputValue,\n defaultValue: '!', // an invalid value so that form reset is ignored in onChange above\n autoComplete: 'off',\n 'aria-label': props['aria-label'] || undefined,\n 'aria-labelledby': props['aria-labelledby'] || undefined,\n id: inputId,\n type: 'text', // Can't use type=\"number\" because then we can't have things like $ in the field.\n inputMode,\n onChange,\n onBlur,\n onFocus,\n onFocusChange,\n onKeyDown: useMemo(() => chain(onKeyDownEnter, onKeyDown), [onKeyDownEnter, onKeyDown]),\n onKeyUp,\n onPaste,\n description,\n errorMessage\n }, state, inputRef);\n\n useFormReset(inputRef, state.defaultNumberValue, state.setNumberValue);\n\n let inputProps: InputHTMLAttributes<HTMLInputElement> = mergeProps(\n spinButtonProps,\n focusProps,\n textFieldProps,\n {\n // override the spinbutton role, we can't focus a spin button with VO\n role: null,\n // ignore aria-roledescription on iOS so that required state will announce when it is present\n 'aria-roledescription': (!isIOS() ? stringFormatter.format('numberField') : null),\n 'aria-valuemax': null,\n 'aria-valuemin': null,\n 'aria-valuenow': null,\n 'aria-valuetext': null,\n autoCorrect: 'off',\n spellCheck: 'false'\n }\n );\n\n if (props.validationBehavior === 'native') {\n inputProps['aria-required'] = undefined;\n }\n\n let onButtonPressStart = (e) => {\n // If focus is already on the input, keep it there so we don't hide the\n // software keyboard when tapping the increment/decrement buttons.\n if (getActiveElement() === inputRef.current) {\n return;\n }\n\n // Otherwise, when using a mouse, move focus to the input.\n // On touch, or with a screen reader, focus the button so that the software\n // keyboard does not appear and the screen reader cursor is not moved off the button.\n if (e.pointerType === 'mouse') {\n inputRef.current?.focus();\n } else {\n e.target.focus();\n }\n };\n\n // Determine the label for the increment and decrement buttons. There are 4 cases:\n //\n // 1. With a visible label that is a string: aria-label: `Increase ${props.label}`\n // 2. With a visible label that is JSX: aria-label: 'Increase', aria-labelledby: '${incrementId} ${labelId}'\n // 3. With an aria-label: aria-label: `Increase ${props['aria-label']}`\n // 4. With an aria-labelledby: aria-label: 'Increase', aria-labelledby: `${incrementId} ${props['aria-labelledby']}`\n //\n // (1) and (2) could possibly be combined and both use aria-labelledby. However, placing the label in\n // the aria-label string rather than using aria-labelledby gives more flexibility to translators to change\n // the order or add additional words around the label if needed.\n let fieldLabel = props['aria-label'] || (typeof props.label === 'string' ? props.label : '');\n let ariaLabelledby: string | undefined;\n if (!fieldLabel) {\n ariaLabelledby = props.label != null ? labelProps.id : props['aria-labelledby'];\n }\n\n let incrementId = useId();\n let decrementId = useId();\n\n let incrementButtonProps: AriaButtonProps = mergeProps(incButtonProps, {\n 'aria-label': incrementAriaLabel || stringFormatter.format('increase', {fieldLabel}).trim(),\n id: ariaLabelledby && !incrementAriaLabel ? incrementId : null,\n 'aria-labelledby': ariaLabelledby && !incrementAriaLabel ? `${incrementId} ${ariaLabelledby}` : null,\n 'aria-controls': inputId,\n excludeFromTabOrder: true,\n preventFocusOnPress: true,\n allowFocusWhenDisabled: true,\n isDisabled: !state.canIncrement,\n onPressStart: onButtonPressStart\n });\n\n let decrementButtonProps: AriaButtonProps = mergeProps(decButtonProps, {\n 'aria-label': decrementAriaLabel || stringFormatter.format('decrease', {fieldLabel}).trim(),\n id: ariaLabelledby && !decrementAriaLabel ? decrementId : null,\n 'aria-labelledby': ariaLabelledby && !decrementAriaLabel ? `${decrementId} ${ariaLabelledby}` : null,\n 'aria-controls': inputId,\n excludeFromTabOrder: true,\n preventFocusOnPress: true,\n allowFocusWhenDisabled: true,\n isDisabled: !state.canDecrement,\n onPressStart: onButtonPressStart\n });\n\n return {\n groupProps: {\n ...focusWithinProps,\n role: 'group',\n 'aria-disabled': isDisabled,\n 'aria-invalid': isInvalid ? 'true' : undefined\n },\n labelProps,\n inputProps,\n incrementButtonProps,\n decrementButtonProps,\n errorMessageProps,\n descriptionProps,\n isInvalid,\n validationErrors,\n validationDetails\n };\n}\n"],"names":[],"version":3,"file":"useNumberField.main.js.map"}
|
package/dist/useNumberField.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import $jvaNJ$intlStringsmodulejs from "./intlStrings.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import {announce as $jvaNJ$announce} from "@react-aria/live-announcer";
|
|
3
|
+
import {useId as $jvaNJ$useId, isIPhone as $jvaNJ$isIPhone, isAndroid as $jvaNJ$isAndroid, getEventTarget as $jvaNJ$getEventTarget, filterDOMProps as $jvaNJ$filterDOMProps, chain as $jvaNJ$chain, useFormReset as $jvaNJ$useFormReset, mergeProps as $jvaNJ$mergeProps, isIOS as $jvaNJ$isIOS, getActiveElement as $jvaNJ$getActiveElement} from "@react-aria/utils";
|
|
4
|
+
import {useCallback as $jvaNJ$useCallback, useMemo as $jvaNJ$useMemo, useState as $jvaNJ$useState} from "react";
|
|
5
|
+
import {flushSync as $jvaNJ$flushSync} from "react-dom";
|
|
4
6
|
import {privateValidationStateProp as $jvaNJ$privateValidationStateProp} from "@react-stately/form";
|
|
5
7
|
import {useFocus as $jvaNJ$useFocus, useFocusWithin as $jvaNJ$useFocusWithin, useScrollWheel as $jvaNJ$useScrollWheel} from "@react-aria/interactions";
|
|
6
8
|
import {useFormattedTextField as $jvaNJ$useFormattedTextField} from "@react-aria/textfield";
|
|
@@ -29,15 +31,30 @@ function $parcel$interopDefault(a) {
|
|
|
29
31
|
|
|
30
32
|
|
|
31
33
|
|
|
34
|
+
|
|
35
|
+
|
|
32
36
|
function $81397a9303501bda$export$23f548e970bdf099(props, state, inputRef) {
|
|
33
37
|
let { id: id, decrementAriaLabel: decrementAriaLabel, incrementAriaLabel: incrementAriaLabel, isDisabled: isDisabled, isReadOnly: isReadOnly, isRequired: isRequired, minValue: minValue, maxValue: maxValue, autoFocus: autoFocus, label: label, formatOptions: formatOptions, onBlur: onBlur = ()=>{}, onFocus: onFocus, onFocusChange: onFocusChange, onKeyDown: onKeyDown, onKeyUp: onKeyUp, description: description, errorMessage: errorMessage, isWheelDisabled: isWheelDisabled, ...otherProps } = props;
|
|
34
38
|
let { increment: increment, incrementToMax: incrementToMax, decrement: decrement, decrementToMin: decrementToMin, numberValue: numberValue, inputValue: inputValue, commit: commit, commitValidation: commitValidation } = state;
|
|
35
39
|
const stringFormatter = (0, $jvaNJ$useLocalizedStringFormatter)((0, ($parcel$interopDefault($jvaNJ$intlStringsmodulejs))), '@react-aria/numberfield');
|
|
40
|
+
let commitAndAnnounce = (0, $jvaNJ$useCallback)(()=>{
|
|
41
|
+
var _inputRef_current, _inputRef_current1, _inputRef_current2;
|
|
42
|
+
var _inputRef_current_value;
|
|
43
|
+
let oldValue = (_inputRef_current_value = (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.value) !== null && _inputRef_current_value !== void 0 ? _inputRef_current_value : '';
|
|
44
|
+
// Set input value to normalized valid value
|
|
45
|
+
(0, $jvaNJ$flushSync)(()=>{
|
|
46
|
+
commit();
|
|
47
|
+
});
|
|
48
|
+
var _inputRef_current_value1;
|
|
49
|
+
if (((_inputRef_current1 = inputRef.current) === null || _inputRef_current1 === void 0 ? void 0 : _inputRef_current1.value) !== oldValue) (0, $jvaNJ$announce)((_inputRef_current_value1 = (_inputRef_current2 = inputRef.current) === null || _inputRef_current2 === void 0 ? void 0 : _inputRef_current2.value) !== null && _inputRef_current_value1 !== void 0 ? _inputRef_current_value1 : '', 'assertive');
|
|
50
|
+
}, [
|
|
51
|
+
commit,
|
|
52
|
+
inputRef
|
|
53
|
+
]);
|
|
36
54
|
let inputId = (0, $jvaNJ$useId)(id);
|
|
37
55
|
let { focusProps: focusProps } = (0, $jvaNJ$useFocus)({
|
|
38
56
|
onBlur () {
|
|
39
|
-
|
|
40
|
-
commit();
|
|
57
|
+
commitAndAnnounce();
|
|
41
58
|
}
|
|
42
59
|
});
|
|
43
60
|
let numberFormatter = (0, $jvaNJ$useNumberFormatter)(formatOptions);
|
|
@@ -113,6 +130,24 @@ function $81397a9303501bda$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
113
130
|
let onChange = (value)=>{
|
|
114
131
|
if (state.validate(value)) state.setInputValue(value);
|
|
115
132
|
};
|
|
133
|
+
let onPaste = (e)=>{
|
|
134
|
+
var _props_onPaste;
|
|
135
|
+
(_props_onPaste = props.onPaste) === null || _props_onPaste === void 0 ? void 0 : _props_onPaste.call(props, e);
|
|
136
|
+
let inputElement = (0, $jvaNJ$getEventTarget)(e);
|
|
137
|
+
var _inputElement_selectionEnd, _inputElement_selectionStart;
|
|
138
|
+
// we can only handle the case where the paste takes over the entire input, otherwise things get very complicated
|
|
139
|
+
// trying to calculate the new string based on what the paste is replacing and where in the source string it is
|
|
140
|
+
if (inputElement && ((_inputElement_selectionEnd = inputElement.selectionEnd) !== null && _inputElement_selectionEnd !== void 0 ? _inputElement_selectionEnd : -1) - ((_inputElement_selectionStart = inputElement.selectionStart) !== null && _inputElement_selectionStart !== void 0 ? _inputElement_selectionStart : 0) === inputElement.value.length) {
|
|
141
|
+
var _e_clipboardData_getData, _e_clipboardData_getData1, _e_clipboardData;
|
|
142
|
+
e.preventDefault();
|
|
143
|
+
var _e_clipboardData_getData_trim;
|
|
144
|
+
// commit so that the user gets to see what it formats to immediately
|
|
145
|
+
// paste happens before inputRef's value is updated, so have to prevent the default and do it ourselves
|
|
146
|
+
// spin button will then handle announcing the new value, this should work with controlled state as well
|
|
147
|
+
// because the announcement is done as a result of the new rendered input value if there is one
|
|
148
|
+
commit((_e_clipboardData_getData_trim = (_e_clipboardData = e.clipboardData) === null || _e_clipboardData === void 0 ? void 0 : (_e_clipboardData_getData1 = _e_clipboardData.getData) === null || _e_clipboardData_getData1 === void 0 ? void 0 : (_e_clipboardData_getData = _e_clipboardData_getData1.call(_e_clipboardData, 'text/plain')) === null || _e_clipboardData_getData === void 0 ? void 0 : _e_clipboardData_getData.trim()) !== null && _e_clipboardData_getData_trim !== void 0 ? _e_clipboardData_getData_trim : '');
|
|
149
|
+
}
|
|
150
|
+
};
|
|
116
151
|
let domProps = (0, $jvaNJ$filterDOMProps)(props);
|
|
117
152
|
let onKeyDownEnter = (0, $jvaNJ$useCallback)((e)=>{
|
|
118
153
|
if (e.nativeEvent.isComposing) return;
|
|
@@ -155,6 +190,7 @@ function $81397a9303501bda$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
155
190
|
onKeyDown
|
|
156
191
|
]),
|
|
157
192
|
onKeyUp: onKeyUp,
|
|
193
|
+
onPaste: onPaste,
|
|
158
194
|
description: description,
|
|
159
195
|
errorMessage: errorMessage
|
|
160
196
|
}, state, inputRef);
|
|
@@ -176,7 +212,7 @@ function $81397a9303501bda$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
176
212
|
var _inputRef_current;
|
|
177
213
|
// If focus is already on the input, keep it there so we don't hide the
|
|
178
214
|
// software keyboard when tapping the increment/decrement buttons.
|
|
179
|
-
if (
|
|
215
|
+
if ((0, $jvaNJ$getActiveElement)() === inputRef.current) return;
|
|
180
216
|
// Otherwise, when using a mouse, move focus to the input.
|
|
181
217
|
// On touch, or with a screen reader, focus the button so that the software
|
|
182
218
|
// keyboard does not appear and the screen reader cursor is not moved off the button.
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import $jvaNJ$intlStringsmodulejs from "./intlStrings.module.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import {announce as $jvaNJ$announce} from "@react-aria/live-announcer";
|
|
3
|
+
import {useId as $jvaNJ$useId, isIPhone as $jvaNJ$isIPhone, isAndroid as $jvaNJ$isAndroid, getEventTarget as $jvaNJ$getEventTarget, filterDOMProps as $jvaNJ$filterDOMProps, chain as $jvaNJ$chain, useFormReset as $jvaNJ$useFormReset, mergeProps as $jvaNJ$mergeProps, isIOS as $jvaNJ$isIOS, getActiveElement as $jvaNJ$getActiveElement} from "@react-aria/utils";
|
|
4
|
+
import {useCallback as $jvaNJ$useCallback, useMemo as $jvaNJ$useMemo, useState as $jvaNJ$useState} from "react";
|
|
5
|
+
import {flushSync as $jvaNJ$flushSync} from "react-dom";
|
|
4
6
|
import {privateValidationStateProp as $jvaNJ$privateValidationStateProp} from "@react-stately/form";
|
|
5
7
|
import {useFocus as $jvaNJ$useFocus, useFocusWithin as $jvaNJ$useFocusWithin, useScrollWheel as $jvaNJ$useScrollWheel} from "@react-aria/interactions";
|
|
6
8
|
import {useFormattedTextField as $jvaNJ$useFormattedTextField} from "@react-aria/textfield";
|
|
@@ -29,15 +31,30 @@ function $parcel$interopDefault(a) {
|
|
|
29
31
|
|
|
30
32
|
|
|
31
33
|
|
|
34
|
+
|
|
35
|
+
|
|
32
36
|
function $81397a9303501bda$export$23f548e970bdf099(props, state, inputRef) {
|
|
33
37
|
let { id: id, decrementAriaLabel: decrementAriaLabel, incrementAriaLabel: incrementAriaLabel, isDisabled: isDisabled, isReadOnly: isReadOnly, isRequired: isRequired, minValue: minValue, maxValue: maxValue, autoFocus: autoFocus, label: label, formatOptions: formatOptions, onBlur: onBlur = ()=>{}, onFocus: onFocus, onFocusChange: onFocusChange, onKeyDown: onKeyDown, onKeyUp: onKeyUp, description: description, errorMessage: errorMessage, isWheelDisabled: isWheelDisabled, ...otherProps } = props;
|
|
34
38
|
let { increment: increment, incrementToMax: incrementToMax, decrement: decrement, decrementToMin: decrementToMin, numberValue: numberValue, inputValue: inputValue, commit: commit, commitValidation: commitValidation } = state;
|
|
35
39
|
const stringFormatter = (0, $jvaNJ$useLocalizedStringFormatter)((0, ($parcel$interopDefault($jvaNJ$intlStringsmodulejs))), '@react-aria/numberfield');
|
|
40
|
+
let commitAndAnnounce = (0, $jvaNJ$useCallback)(()=>{
|
|
41
|
+
var _inputRef_current, _inputRef_current1, _inputRef_current2;
|
|
42
|
+
var _inputRef_current_value;
|
|
43
|
+
let oldValue = (_inputRef_current_value = (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.value) !== null && _inputRef_current_value !== void 0 ? _inputRef_current_value : '';
|
|
44
|
+
// Set input value to normalized valid value
|
|
45
|
+
(0, $jvaNJ$flushSync)(()=>{
|
|
46
|
+
commit();
|
|
47
|
+
});
|
|
48
|
+
var _inputRef_current_value1;
|
|
49
|
+
if (((_inputRef_current1 = inputRef.current) === null || _inputRef_current1 === void 0 ? void 0 : _inputRef_current1.value) !== oldValue) (0, $jvaNJ$announce)((_inputRef_current_value1 = (_inputRef_current2 = inputRef.current) === null || _inputRef_current2 === void 0 ? void 0 : _inputRef_current2.value) !== null && _inputRef_current_value1 !== void 0 ? _inputRef_current_value1 : '', 'assertive');
|
|
50
|
+
}, [
|
|
51
|
+
commit,
|
|
52
|
+
inputRef
|
|
53
|
+
]);
|
|
36
54
|
let inputId = (0, $jvaNJ$useId)(id);
|
|
37
55
|
let { focusProps: focusProps } = (0, $jvaNJ$useFocus)({
|
|
38
56
|
onBlur () {
|
|
39
|
-
|
|
40
|
-
commit();
|
|
57
|
+
commitAndAnnounce();
|
|
41
58
|
}
|
|
42
59
|
});
|
|
43
60
|
let numberFormatter = (0, $jvaNJ$useNumberFormatter)(formatOptions);
|
|
@@ -113,6 +130,24 @@ function $81397a9303501bda$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
113
130
|
let onChange = (value)=>{
|
|
114
131
|
if (state.validate(value)) state.setInputValue(value);
|
|
115
132
|
};
|
|
133
|
+
let onPaste = (e)=>{
|
|
134
|
+
var _props_onPaste;
|
|
135
|
+
(_props_onPaste = props.onPaste) === null || _props_onPaste === void 0 ? void 0 : _props_onPaste.call(props, e);
|
|
136
|
+
let inputElement = (0, $jvaNJ$getEventTarget)(e);
|
|
137
|
+
var _inputElement_selectionEnd, _inputElement_selectionStart;
|
|
138
|
+
// we can only handle the case where the paste takes over the entire input, otherwise things get very complicated
|
|
139
|
+
// trying to calculate the new string based on what the paste is replacing and where in the source string it is
|
|
140
|
+
if (inputElement && ((_inputElement_selectionEnd = inputElement.selectionEnd) !== null && _inputElement_selectionEnd !== void 0 ? _inputElement_selectionEnd : -1) - ((_inputElement_selectionStart = inputElement.selectionStart) !== null && _inputElement_selectionStart !== void 0 ? _inputElement_selectionStart : 0) === inputElement.value.length) {
|
|
141
|
+
var _e_clipboardData_getData, _e_clipboardData_getData1, _e_clipboardData;
|
|
142
|
+
e.preventDefault();
|
|
143
|
+
var _e_clipboardData_getData_trim;
|
|
144
|
+
// commit so that the user gets to see what it formats to immediately
|
|
145
|
+
// paste happens before inputRef's value is updated, so have to prevent the default and do it ourselves
|
|
146
|
+
// spin button will then handle announcing the new value, this should work with controlled state as well
|
|
147
|
+
// because the announcement is done as a result of the new rendered input value if there is one
|
|
148
|
+
commit((_e_clipboardData_getData_trim = (_e_clipboardData = e.clipboardData) === null || _e_clipboardData === void 0 ? void 0 : (_e_clipboardData_getData1 = _e_clipboardData.getData) === null || _e_clipboardData_getData1 === void 0 ? void 0 : (_e_clipboardData_getData = _e_clipboardData_getData1.call(_e_clipboardData, 'text/plain')) === null || _e_clipboardData_getData === void 0 ? void 0 : _e_clipboardData_getData.trim()) !== null && _e_clipboardData_getData_trim !== void 0 ? _e_clipboardData_getData_trim : '');
|
|
149
|
+
}
|
|
150
|
+
};
|
|
116
151
|
let domProps = (0, $jvaNJ$filterDOMProps)(props);
|
|
117
152
|
let onKeyDownEnter = (0, $jvaNJ$useCallback)((e)=>{
|
|
118
153
|
if (e.nativeEvent.isComposing) return;
|
|
@@ -155,6 +190,7 @@ function $81397a9303501bda$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
155
190
|
onKeyDown
|
|
156
191
|
]),
|
|
157
192
|
onKeyUp: onKeyUp,
|
|
193
|
+
onPaste: onPaste,
|
|
158
194
|
description: description,
|
|
159
195
|
errorMessage: errorMessage
|
|
160
196
|
}, state, inputRef);
|
|
@@ -176,7 +212,7 @@ function $81397a9303501bda$export$23f548e970bdf099(props, state, inputRef) {
|
|
|
176
212
|
var _inputRef_current;
|
|
177
213
|
// If focus is already on the input, keep it there so we don't hide the
|
|
178
214
|
// software keyboard when tapping the increment/decrement buttons.
|
|
179
|
-
if (
|
|
215
|
+
if ((0, $jvaNJ$getActiveElement)() === inputRef.current) return;
|
|
180
216
|
// Otherwise, when using a mouse, move focus to the input.
|
|
181
217
|
// On touch, or with a screen reader, focus the button so that the software
|
|
182
218
|
// keyboard does not appear and the screen reader cursor is not moved off the button.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;;;AA+CM,SAAS,0CAAe,KAA2B,EAAE,KAAuB,EAAE,QAA4C;IAC/H,IAAI,MACF,EAAE,sBACF,kBAAkB,sBAClB,kBAAkB,cAClB,UAAU,cACV,UAAU,cACV,UAAU,YACV,QAAQ,YACR,QAAQ,aACR,SAAS,SACT,KAAK,iBACL,aAAa,UACb,SAAS,KAAO,YAChB,OAAO,iBACP,aAAa,aACb,SAAS,WACT,OAAO,eACP,WAAW,gBACX,YAAY,mBACZ,eAAe,EACf,GAAG,YACJ,GAAG;IAEJ,IAAI,aACF,SAAS,kBACT,cAAc,aACd,SAAS,kBACT,cAAc,eACd,WAAW,cACX,UAAU,UACV,MAAM,oBACN,gBAAgB,EACjB,GAAG;IAEJ,MAAM,kBAAkB,CAAA,GAAA,kCAA0B,EAAE,CAAA,GAAA,oDAAW,GAAG;IAElE,IAAI,UAAU,CAAA,GAAA,YAAI,EAAE;IACpB,IAAI,cAAC,UAAU,EAAC,GAAG,CAAA,GAAA,eAAO,EAAE;QAC1B;YACE,4CAA4C;YAC5C;QACF;IACF;IAEA,IAAI,kBAAkB,CAAA,GAAA,yBAAiB,EAAE;IACzC,IAAI,cAAc,CAAA,GAAA,cAAM,EAAE,IAAM,gBAAgB,eAAe,IAAI;QAAC;KAAgB;IAEpF,wEAAwE;IACxE,6DAA6D;IAC7D,IAAI,qBAAqB,CAAA,GAAA,yBAAiB,EAAE;QAAC,GAAG,aAAa;QAAE,cAAc;IAAS;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C,qBAAqB,GAAG,YAAY,CAAC,EAAE,gBAAgB,GAAG;QAChG,iBAAiB;QACjB,qBAAqB;QACrB,qBAAqB;QACrB,wBAAwB;QACxB,YAAY,CAAC,MAAM,YAAY;QAC/B,cAAc;IAChB;IAEA,IAAI,uBAAwC,CAAA,GAAA,iBAAS,EAAE,gBAAgB;QACrE,cAAc,sBAAsB,gBAAgB,MAAM,CAAC,YAAY;wBAAC;QAAU,GAAG,IAAI;QACzF,IAAI,kBAAkB,CAAC,qBAAqB,cAAc;QAC1D,mBAAmB,kBAAkB,CAAC,qBAAqB,GAAG,YAAY,CAAC,EAAE,gBAAgB,GAAG;QAChG,iBAAiB;QACjB,qBAAqB;QACrB,qBAAqB;QACrB,wBAAwB;QACxB,YAAY,CAAC,MAAM,YAAY;QAC/B,cAAc;IAChB;IAEA,OAAO;QACL,YAAY;YACV,GAAG,gBAAgB;YACnB,MAAM;YACN,iBAAiB;YACjB,gBAAgB,YAAY,SAAS;QACvC;oBACA;oBACA;8BACA;8BACA;2BACA;0BACA;mBACA;0BACA;2BACA;IACF;AACF","sources":["packages/@react-aria/numberfield/src/useNumberField.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {AriaButtonProps} from '@react-types/button';\nimport {AriaNumberFieldProps} from '@react-types/numberfield';\nimport {chain, filterDOMProps, isAndroid, isIOS, isIPhone, mergeProps, useFormReset, useId} from '@react-aria/utils';\nimport {DOMAttributes, GroupDOMAttributes, TextInputDOMProps, ValidationResult} from '@react-types/shared';\nimport {\n InputHTMLAttributes,\n LabelHTMLAttributes,\n RefObject,\n useCallback,\n useMemo,\n useState\n} from 'react';\n// @ts-ignore\nimport intlMessages from '../intl/*.json';\nimport {NumberFieldState} from '@react-stately/numberfield';\nimport {privateValidationStateProp} from '@react-stately/form';\nimport {useFocus, useFocusWithin, useScrollWheel} from '@react-aria/interactions';\nimport {useFormattedTextField} from '@react-aria/textfield';\nimport {\n useLocalizedStringFormatter,\n useNumberFormatter\n} from '@react-aria/i18n';\nimport {useSpinButton} from '@react-aria/spinbutton';\n\nexport interface NumberFieldAria extends ValidationResult {\n /** Props for the label element. */\n labelProps: LabelHTMLAttributes<HTMLLabelElement>,\n /** Props for the group wrapper around the input and stepper buttons. */\n groupProps: GroupDOMAttributes,\n /** Props for the input element. */\n inputProps: InputHTMLAttributes<HTMLInputElement>,\n /** Props for the increment button, to be passed to `useButton`. */\n incrementButtonProps: AriaButtonProps,\n /** Props for the decrement button, to be passed to `useButton`. */\n decrementButtonProps: AriaButtonProps,\n /** Props for the number field's description element, if any. */\n descriptionProps: DOMAttributes,\n /** Props for the number field's error message element, if any. */\n errorMessageProps: DOMAttributes\n}\n\n/**\n * Provides the behavior and accessibility implementation for a number field component.\n * Number fields allow users to enter a number, and increment or decrement the value using stepper buttons.\n */\nexport function useNumberField(props: AriaNumberFieldProps, state: NumberFieldState, inputRef: RefObject<HTMLInputElement | null>): NumberFieldAria {\n let {\n id,\n decrementAriaLabel,\n incrementAriaLabel,\n isDisabled,\n isReadOnly,\n isRequired,\n minValue,\n maxValue,\n autoFocus,\n label,\n formatOptions,\n onBlur = () => {},\n onFocus,\n onFocusChange,\n onKeyDown,\n onKeyUp,\n description,\n errorMessage,\n isWheelDisabled,\n ...otherProps\n } = props;\n\n let {\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n numberValue,\n inputValue,\n commit,\n commitValidation\n } = state;\n\n const stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/numberfield');\n\n let inputId = useId(id);\n let {focusProps} = useFocus({\n onBlur() {\n // Set input value to normalized valid value\n commit();\n }\n });\n\n let numberFormatter = useNumberFormatter(formatOptions);\n let intlOptions = useMemo(() => numberFormatter.resolvedOptions(), [numberFormatter]);\n\n // Replace negative textValue formatted using currencySign: 'accounting'\n // with a textValue that can be announced using a minus sign.\n let textValueFormatter = useNumberFormatter({...formatOptions, currencySign: undefined});\n let textValue = useMemo(() => isNaN(numberValue) ? '' : textValueFormatter.format(numberValue), [textValueFormatter, numberValue]);\n\n let {\n spinButtonProps,\n incrementButtonProps: incButtonProps,\n decrementButtonProps: decButtonProps\n } = useSpinButton(\n {\n isDisabled,\n isReadOnly,\n isRequired,\n maxValue,\n minValue,\n onIncrement: increment,\n onIncrementToMax: incrementToMax,\n onDecrement: decrement,\n onDecrementToMin: decrementToMin,\n value: numberValue,\n textValue\n }\n );\n\n let [focusWithin, setFocusWithin] = useState(false);\n let {focusWithinProps} = useFocusWithin({isDisabled, onFocusWithinChange: setFocusWithin});\n\n let onWheel = useCallback((e) => {\n // if on a trackpad, users can scroll in both X and Y at once, check the magnitude of the change\n // if it's mostly in the X direction, then just return, the user probably doesn't mean to inc/dec\n // this isn't perfect, events come in fast with small deltas and a part of the scroll may give a false indication\n // especially if the user is scrolling near 45deg\n if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) {\n return;\n }\n if (e.deltaY > 0) {\n increment();\n } else if (e.deltaY < 0) {\n decrement();\n }\n }, [decrement, increment]);\n // If the input isn't supposed to receive input, disable scrolling.\n let scrollingDisabled = isWheelDisabled || isDisabled || isReadOnly || !focusWithin;\n useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, inputRef);\n\n // The inputMode attribute influences the software keyboard that is shown on touch devices.\n // Browsers and operating systems are quite inconsistent about what keys are available, however.\n // We choose between numeric and decimal based on whether we allow negative and fractional numbers,\n // and based on testing on various devices to determine what keys are available in each inputMode.\n let hasDecimals = (intlOptions.maximumFractionDigits ?? 0) > 0;\n let hasNegative = (state.minValue === undefined || isNaN(state.minValue)) || state.minValue < 0;\n let inputMode: TextInputDOMProps['inputMode'] = 'numeric';\n if (isIPhone()) {\n // iPhone doesn't have a minus sign in either numeric or decimal.\n // Note this is only for iPhone, not iPad, which always has both\n // minus and decimal in numeric.\n if (hasNegative) {\n inputMode = 'text';\n } else if (hasDecimals) {\n inputMode = 'decimal';\n }\n } else if (isAndroid()) {\n // Android numeric has both a decimal point and minus key.\n // decimal does not have a minus key.\n if (hasNegative) {\n inputMode = 'numeric';\n } else if (hasDecimals) {\n inputMode = 'decimal';\n }\n }\n\n let onChange = value => {\n if (state.validate(value)) {\n state.setInputValue(value);\n }\n };\n\n let domProps = filterDOMProps(props);\n let onKeyDownEnter = useCallback((e) => {\n if (e.nativeEvent.isComposing) {\n return;\n }\n\n if (e.key === 'Enter') {\n commit();\n commitValidation();\n } else {\n e.continuePropagation();\n }\n }, [commit, commitValidation]);\n\n let {isInvalid, validationErrors, validationDetails} = state.displayValidation;\n let {labelProps, inputProps: textFieldProps, descriptionProps, errorMessageProps} = useFormattedTextField({\n ...otherProps,\n ...domProps,\n // These props are added to a hidden input rather than the formatted textfield.\n name: undefined,\n form: undefined,\n label,\n autoFocus,\n isDisabled,\n isReadOnly,\n isRequired,\n validate: undefined,\n [privateValidationStateProp]: state,\n value: inputValue,\n defaultValue: '!', // an invalid value so that form reset is ignored in onChange above\n autoComplete: 'off',\n 'aria-label': props['aria-label'] || undefined,\n 'aria-labelledby': props['aria-labelledby'] || undefined,\n id: inputId,\n type: 'text', // Can't use type=\"number\" because then we can't have things like $ in the field.\n inputMode,\n onChange,\n onBlur,\n onFocus,\n onFocusChange,\n onKeyDown: useMemo(() => chain(onKeyDownEnter, onKeyDown), [onKeyDownEnter, onKeyDown]),\n onKeyUp,\n description,\n errorMessage\n }, state, inputRef);\n\n useFormReset(inputRef, state.defaultNumberValue, state.setNumberValue);\n\n let inputProps: InputHTMLAttributes<HTMLInputElement> = mergeProps(\n spinButtonProps,\n focusProps,\n textFieldProps,\n {\n // override the spinbutton role, we can't focus a spin button with VO\n role: null,\n // ignore aria-roledescription on iOS so that required state will announce when it is present\n 'aria-roledescription': (!isIOS() ? stringFormatter.format('numberField') : null),\n 'aria-valuemax': null,\n 'aria-valuemin': null,\n 'aria-valuenow': null,\n 'aria-valuetext': null,\n autoCorrect: 'off',\n spellCheck: 'false'\n }\n );\n\n if (props.validationBehavior === 'native') {\n inputProps['aria-required'] = undefined;\n }\n\n let onButtonPressStart = (e) => {\n // If focus is already on the input, keep it there so we don't hide the\n // software keyboard when tapping the increment/decrement buttons.\n if (document.activeElement === inputRef.current) {\n return;\n }\n\n // Otherwise, when using a mouse, move focus to the input.\n // On touch, or with a screen reader, focus the button so that the software\n // keyboard does not appear and the screen reader cursor is not moved off the button.\n if (e.pointerType === 'mouse') {\n inputRef.current?.focus();\n } else {\n e.target.focus();\n }\n };\n\n // Determine the label for the increment and decrement buttons. There are 4 cases:\n //\n // 1. With a visible label that is a string: aria-label: `Increase ${props.label}`\n // 2. With a visible label that is JSX: aria-label: 'Increase', aria-labelledby: '${incrementId} ${labelId}'\n // 3. With an aria-label: aria-label: `Increase ${props['aria-label']}`\n // 4. With an aria-labelledby: aria-label: 'Increase', aria-labelledby: `${incrementId} ${props['aria-labelledby']}`\n //\n // (1) and (2) could possibly be combined and both use aria-labelledby. However, placing the label in\n // the aria-label string rather than using aria-labelledby gives more flexibility to translators to change\n // the order or add additional words around the label if needed.\n let fieldLabel = props['aria-label'] || (typeof props.label === 'string' ? props.label : '');\n let ariaLabelledby: string | undefined;\n if (!fieldLabel) {\n ariaLabelledby = props.label != null ? labelProps.id : props['aria-labelledby'];\n }\n\n let incrementId = useId();\n let decrementId = useId();\n\n let incrementButtonProps: AriaButtonProps = mergeProps(incButtonProps, {\n 'aria-label': incrementAriaLabel || stringFormatter.format('increase', {fieldLabel}).trim(),\n id: ariaLabelledby && !incrementAriaLabel ? incrementId : null,\n 'aria-labelledby': ariaLabelledby && !incrementAriaLabel ? `${incrementId} ${ariaLabelledby}` : null,\n 'aria-controls': inputId,\n excludeFromTabOrder: true,\n preventFocusOnPress: true,\n allowFocusWhenDisabled: true,\n isDisabled: !state.canIncrement,\n onPressStart: onButtonPressStart\n });\n\n let decrementButtonProps: AriaButtonProps = mergeProps(decButtonProps, {\n 'aria-label': decrementAriaLabel || stringFormatter.format('decrease', {fieldLabel}).trim(),\n id: ariaLabelledby && !decrementAriaLabel ? decrementId : null,\n 'aria-labelledby': ariaLabelledby && !decrementAriaLabel ? `${decrementId} ${ariaLabelledby}` : null,\n 'aria-controls': inputId,\n excludeFromTabOrder: true,\n preventFocusOnPress: true,\n allowFocusWhenDisabled: true,\n isDisabled: !state.canDecrement,\n onPressStart: onButtonPressStart\n });\n\n return {\n groupProps: {\n ...focusWithinProps,\n role: 'group',\n 'aria-disabled': isDisabled,\n 'aria-invalid': isInvalid ? 'true' : undefined\n },\n labelProps,\n inputProps,\n incrementButtonProps,\n decrementButtonProps,\n errorMessageProps,\n descriptionProps,\n isInvalid,\n validationErrors,\n validationDetails\n };\n}\n"],"names":[],"version":3,"file":"useNumberField.module.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;;;;;AAmDM,SAAS,0CAAe,KAA2B,EAAE,KAAuB,EAAE,QAA4C;IAC/H,IAAI,MACF,EAAE,sBACF,kBAAkB,sBAClB,kBAAkB,cAClB,UAAU,cACV,UAAU,cACV,UAAU,YACV,QAAQ,YACR,QAAQ,aACR,SAAS,SACT,KAAK,iBACL,aAAa,UACb,SAAS,KAAO,YAChB,OAAO,iBACP,aAAa,aACb,SAAS,WACT,OAAO,eACP,WAAW,gBACX,YAAY,mBACZ,eAAe,EACf,GAAG,YACJ,GAAG;IAEJ,IAAI,aACF,SAAS,kBACT,cAAc,aACd,SAAS,kBACT,cAAc,eACd,WAAW,cACX,UAAU,UACV,MAAM,oBACN,gBAAgB,EACjB,GAAG;IAEJ,MAAM,kBAAkB,CAAA,GAAA,kCAA0B,EAAE,CAAA,GAAA,oDAAW,GA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F,IAAI,cAAc,CAAA,GAAA,YAAI;IACtB,IAAI,cAAc,CAAA,GAAA,YAAI;IAEtB,IAAI,uBAAwC,CAAA,GAAA,iBAAS,EAAE,gBAAgB;QACrE,cAAc,sBAAsB,gBAAgB,MAAM,CAAC,YAAY;wBAAC;QAAU,GAAG,IAAI;QACzF,IAAI,kBAAkB,CAAC,qBAAqB,cAAc;QAC1D,mBAAmB,kBAAkB,CAAC,qBAAqB,GAAG,YAAY,CAAC,EAAE,gBAAgB,GAAG;QAChG,iBAAiB;QACjB,qBAAqB;QACrB,qBAAqB;QACrB,wBAAwB;QACxB,YAAY,CAAC,MAAM,YAAY;QAC/B,cAAc;IAChB;IAEA,IAAI,uBAAwC,CAAA,GAAA,iBAAS,EAAE,gBAAgB;QACrE,cAAc,sBAAsB,gBAAgB,MAAM,CAAC,YAAY;wBAAC;QAAU,GAAG,IAAI;QACzF,IAAI,kBAAkB,CAAC,qBAAqB,cAAc;QAC1D,mBAAmB,kBAAkB,CAAC,qBAAqB,GAAG,YAAY,CAAC,EAAE,gBAAgB,GAAG;QAChG,iBAAiB;QACjB,qBAAqB;QACrB,qBAAqB;QACrB,wBAAwB;QACxB,YAAY,CAAC,MAAM,YAAY;QAC/B,cAAc;IAChB;IAEA,OAAO;QACL,YAAY;YACV,GAAG,gBAAgB;YACnB,MAAM;YACN,iBAAiB;YACjB,gBAAgB,YAAY,SAAS;QACvC;oBACA;oBACA;8BACA;8BACA;2BACA;0BACA;mBACA;0BACA;2BACA;IACF;AACF","sources":["packages/@react-aria/numberfield/src/useNumberField.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {announce} from '@react-aria/live-announcer';\nimport {AriaButtonProps} from '@react-types/button';\nimport {AriaNumberFieldProps} from '@react-types/numberfield';\nimport {chain, filterDOMProps, getActiveElement, getEventTarget, isAndroid, isIOS, isIPhone, mergeProps, useFormReset, useId} from '@react-aria/utils';\nimport {\n type ClipboardEvent,\n type ClipboardEventHandler,\n InputHTMLAttributes,\n LabelHTMLAttributes,\n RefObject,\n useCallback,\n useMemo,\n useState\n} from 'react';\nimport {DOMAttributes, GroupDOMAttributes, TextInputDOMProps, ValidationResult} from '@react-types/shared';\nimport {flushSync} from 'react-dom';\n// @ts-ignore\nimport intlMessages from '../intl/*.json';\nimport {NumberFieldState} from '@react-stately/numberfield';\nimport {privateValidationStateProp} from '@react-stately/form';\nimport {useFocus, useFocusWithin, useScrollWheel} from '@react-aria/interactions';\nimport {useFormattedTextField} from '@react-aria/textfield';\nimport {\n useLocalizedStringFormatter,\n useNumberFormatter\n} from '@react-aria/i18n';\nimport {useSpinButton} from '@react-aria/spinbutton';\n\nexport interface NumberFieldAria extends ValidationResult {\n /** Props for the label element. */\n labelProps: LabelHTMLAttributes<HTMLLabelElement>,\n /** Props for the group wrapper around the input and stepper buttons. */\n groupProps: GroupDOMAttributes,\n /** Props for the input element. */\n inputProps: InputHTMLAttributes<HTMLInputElement>,\n /** Props for the increment button, to be passed to `useButton`. */\n incrementButtonProps: AriaButtonProps,\n /** Props for the decrement button, to be passed to `useButton`. */\n decrementButtonProps: AriaButtonProps,\n /** Props for the number field's description element, if any. */\n descriptionProps: DOMAttributes,\n /** Props for the number field's error message element, if any. */\n errorMessageProps: DOMAttributes\n}\n\n/**\n * Provides the behavior and accessibility implementation for a number field component.\n * Number fields allow users to enter a number, and increment or decrement the value using stepper buttons.\n */\nexport function useNumberField(props: AriaNumberFieldProps, state: NumberFieldState, inputRef: RefObject<HTMLInputElement | null>): NumberFieldAria {\n let {\n id,\n decrementAriaLabel,\n incrementAriaLabel,\n isDisabled,\n isReadOnly,\n isRequired,\n minValue,\n maxValue,\n autoFocus,\n label,\n formatOptions,\n onBlur = () => {},\n onFocus,\n onFocusChange,\n onKeyDown,\n onKeyUp,\n description,\n errorMessage,\n isWheelDisabled,\n ...otherProps\n } = props;\n\n let {\n increment,\n incrementToMax,\n decrement,\n decrementToMin,\n numberValue,\n inputValue,\n commit,\n commitValidation\n } = state;\n\n const stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/numberfield');\n let commitAndAnnounce = useCallback(() => {\n let oldValue = inputRef.current?.value ?? '';\n // Set input value to normalized valid value\n flushSync(() => {\n commit();\n });\n\n if (inputRef.current?.value !== oldValue) {\n announce(inputRef.current?.value ?? '', 'assertive');\n }\n }, [commit, inputRef]);\n\n let inputId = useId(id);\n let {focusProps} = useFocus({\n onBlur() {\n commitAndAnnounce();\n }\n });\n\n let numberFormatter = useNumberFormatter(formatOptions);\n let intlOptions = useMemo(() => numberFormatter.resolvedOptions(), [numberFormatter]);\n\n // Replace negative textValue formatted using currencySign: 'accounting'\n // with a textValue that can be announced using a minus sign.\n let textValueFormatter = useNumberFormatter({...formatOptions, currencySign: undefined});\n let textValue = useMemo(() => isNaN(numberValue) ? '' : textValueFormatter.format(numberValue), [textValueFormatter, numberValue]);\n\n let {\n spinButtonProps,\n incrementButtonProps: incButtonProps,\n decrementButtonProps: decButtonProps\n } = useSpinButton(\n {\n isDisabled,\n isReadOnly,\n isRequired,\n maxValue,\n minValue,\n onIncrement: increment,\n onIncrementToMax: incrementToMax,\n onDecrement: decrement,\n onDecrementToMin: decrementToMin,\n value: numberValue,\n textValue\n }\n );\n\n let [focusWithin, setFocusWithin] = useState(false);\n let {focusWithinProps} = useFocusWithin({isDisabled, onFocusWithinChange: setFocusWithin});\n\n let onWheel = useCallback((e) => {\n // if on a trackpad, users can scroll in both X and Y at once, check the magnitude of the change\n // if it's mostly in the X direction, then just return, the user probably doesn't mean to inc/dec\n // this isn't perfect, events come in fast with small deltas and a part of the scroll may give a false indication\n // especially if the user is scrolling near 45deg\n if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) {\n return;\n }\n if (e.deltaY > 0) {\n increment();\n } else if (e.deltaY < 0) {\n decrement();\n }\n }, [decrement, increment]);\n // If the input isn't supposed to receive input, disable scrolling.\n let scrollingDisabled = isWheelDisabled || isDisabled || isReadOnly || !focusWithin;\n useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, inputRef);\n\n // The inputMode attribute influences the software keyboard that is shown on touch devices.\n // Browsers and operating systems are quite inconsistent about what keys are available, however.\n // We choose between numeric and decimal based on whether we allow negative and fractional numbers,\n // and based on testing on various devices to determine what keys are available in each inputMode.\n let hasDecimals = (intlOptions.maximumFractionDigits ?? 0) > 0;\n let hasNegative = (state.minValue === undefined || isNaN(state.minValue)) || state.minValue < 0;\n let inputMode: TextInputDOMProps['inputMode'] = 'numeric';\n if (isIPhone()) {\n // iPhone doesn't have a minus sign in either numeric or decimal.\n // Note this is only for iPhone, not iPad, which always has both\n // minus and decimal in numeric.\n if (hasNegative) {\n inputMode = 'text';\n } else if (hasDecimals) {\n inputMode = 'decimal';\n }\n } else if (isAndroid()) {\n // Android numeric has both a decimal point and minus key.\n // decimal does not have a minus key.\n if (hasNegative) {\n inputMode = 'numeric';\n } else if (hasDecimals) {\n inputMode = 'decimal';\n }\n }\n\n let onChange = value => {\n if (state.validate(value)) {\n state.setInputValue(value);\n }\n };\n\n let onPaste: ClipboardEventHandler<HTMLInputElement> = (e: ClipboardEvent<HTMLInputElement>) => {\n props.onPaste?.(e);\n let inputElement = getEventTarget(e) as HTMLInputElement;\n // we can only handle the case where the paste takes over the entire input, otherwise things get very complicated\n // trying to calculate the new string based on what the paste is replacing and where in the source string it is\n if (inputElement &&\n ((inputElement.selectionEnd ?? -1) - (inputElement.selectionStart ?? 0)) === inputElement.value.length\n ) {\n e.preventDefault();\n // commit so that the user gets to see what it formats to immediately\n // paste happens before inputRef's value is updated, so have to prevent the default and do it ourselves\n // spin button will then handle announcing the new value, this should work with controlled state as well\n // because the announcement is done as a result of the new rendered input value if there is one\n commit(e.clipboardData?.getData?.('text/plain')?.trim() ?? '');\n }\n };\n\n let domProps = filterDOMProps(props);\n let onKeyDownEnter = useCallback((e) => {\n if (e.nativeEvent.isComposing) {\n return;\n }\n\n if (e.key === 'Enter') {\n commit();\n commitValidation();\n } else {\n e.continuePropagation();\n }\n }, [commit, commitValidation]);\n\n let {isInvalid, validationErrors, validationDetails} = state.displayValidation;\n let {labelProps, inputProps: textFieldProps, descriptionProps, errorMessageProps} = useFormattedTextField({\n ...otherProps,\n ...domProps,\n // These props are added to a hidden input rather than the formatted textfield.\n name: undefined,\n form: undefined,\n label,\n autoFocus,\n isDisabled,\n isReadOnly,\n isRequired,\n validate: undefined,\n [privateValidationStateProp]: state,\n value: inputValue,\n defaultValue: '!', // an invalid value so that form reset is ignored in onChange above\n autoComplete: 'off',\n 'aria-label': props['aria-label'] || undefined,\n 'aria-labelledby': props['aria-labelledby'] || undefined,\n id: inputId,\n type: 'text', // Can't use type=\"number\" because then we can't have things like $ in the field.\n inputMode,\n onChange,\n onBlur,\n onFocus,\n onFocusChange,\n onKeyDown: useMemo(() => chain(onKeyDownEnter, onKeyDown), [onKeyDownEnter, onKeyDown]),\n onKeyUp,\n onPaste,\n description,\n errorMessage\n }, state, inputRef);\n\n useFormReset(inputRef, state.defaultNumberValue, state.setNumberValue);\n\n let inputProps: InputHTMLAttributes<HTMLInputElement> = mergeProps(\n spinButtonProps,\n focusProps,\n textFieldProps,\n {\n // override the spinbutton role, we can't focus a spin button with VO\n role: null,\n // ignore aria-roledescription on iOS so that required state will announce when it is present\n 'aria-roledescription': (!isIOS() ? stringFormatter.format('numberField') : null),\n 'aria-valuemax': null,\n 'aria-valuemin': null,\n 'aria-valuenow': null,\n 'aria-valuetext': null,\n autoCorrect: 'off',\n spellCheck: 'false'\n }\n );\n\n if (props.validationBehavior === 'native') {\n inputProps['aria-required'] = undefined;\n }\n\n let onButtonPressStart = (e) => {\n // If focus is already on the input, keep it there so we don't hide the\n // software keyboard when tapping the increment/decrement buttons.\n if (getActiveElement() === inputRef.current) {\n return;\n }\n\n // Otherwise, when using a mouse, move focus to the input.\n // On touch, or with a screen reader, focus the button so that the software\n // keyboard does not appear and the screen reader cursor is not moved off the button.\n if (e.pointerType === 'mouse') {\n inputRef.current?.focus();\n } else {\n e.target.focus();\n }\n };\n\n // Determine the label for the increment and decrement buttons. There are 4 cases:\n //\n // 1. With a visible label that is a string: aria-label: `Increase ${props.label}`\n // 2. With a visible label that is JSX: aria-label: 'Increase', aria-labelledby: '${incrementId} ${labelId}'\n // 3. With an aria-label: aria-label: `Increase ${props['aria-label']}`\n // 4. With an aria-labelledby: aria-label: 'Increase', aria-labelledby: `${incrementId} ${props['aria-labelledby']}`\n //\n // (1) and (2) could possibly be combined and both use aria-labelledby. However, placing the label in\n // the aria-label string rather than using aria-labelledby gives more flexibility to translators to change\n // the order or add additional words around the label if needed.\n let fieldLabel = props['aria-label'] || (typeof props.label === 'string' ? props.label : '');\n let ariaLabelledby: string | undefined;\n if (!fieldLabel) {\n ariaLabelledby = props.label != null ? labelProps.id : props['aria-labelledby'];\n }\n\n let incrementId = useId();\n let decrementId = useId();\n\n let incrementButtonProps: AriaButtonProps = mergeProps(incButtonProps, {\n 'aria-label': incrementAriaLabel || stringFormatter.format('increase', {fieldLabel}).trim(),\n id: ariaLabelledby && !incrementAriaLabel ? incrementId : null,\n 'aria-labelledby': ariaLabelledby && !incrementAriaLabel ? `${incrementId} ${ariaLabelledby}` : null,\n 'aria-controls': inputId,\n excludeFromTabOrder: true,\n preventFocusOnPress: true,\n allowFocusWhenDisabled: true,\n isDisabled: !state.canIncrement,\n onPressStart: onButtonPressStart\n });\n\n let decrementButtonProps: AriaButtonProps = mergeProps(decButtonProps, {\n 'aria-label': decrementAriaLabel || stringFormatter.format('decrease', {fieldLabel}).trim(),\n id: ariaLabelledby && !decrementAriaLabel ? decrementId : null,\n 'aria-labelledby': ariaLabelledby && !decrementAriaLabel ? `${decrementId} ${ariaLabelledby}` : null,\n 'aria-controls': inputId,\n excludeFromTabOrder: true,\n preventFocusOnPress: true,\n allowFocusWhenDisabled: true,\n isDisabled: !state.canDecrement,\n onPressStart: onButtonPressStart\n });\n\n return {\n groupProps: {\n ...focusWithinProps,\n role: 'group',\n 'aria-disabled': isDisabled,\n 'aria-invalid': isInvalid ? 'true' : undefined\n },\n labelProps,\n inputProps,\n incrementButtonProps,\n decrementButtonProps,\n errorMessageProps,\n descriptionProps,\n isInvalid,\n validationErrors,\n validationDetails\n };\n}\n"],"names":[],"version":3,"file":"useNumberField.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/numberfield",
|
|
3
|
-
"version": "3.12.
|
|
3
|
+
"version": "3.12.5",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -26,16 +26,17 @@
|
|
|
26
26
|
"url": "https://github.com/adobe/react-spectrum"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@react-aria/i18n": "^3.12.
|
|
30
|
-
"@react-aria/interactions": "^3.
|
|
31
|
-
"@react-aria/
|
|
32
|
-
"@react-aria/
|
|
33
|
-
"@react-aria/
|
|
34
|
-
"@react-
|
|
35
|
-
"@react-stately/
|
|
36
|
-
"@react-
|
|
37
|
-
"@react-types/
|
|
38
|
-
"@react-types/
|
|
29
|
+
"@react-aria/i18n": "^3.12.16",
|
|
30
|
+
"@react-aria/interactions": "^3.27.1",
|
|
31
|
+
"@react-aria/live-announcer": "^3.4.4",
|
|
32
|
+
"@react-aria/spinbutton": "^3.7.2",
|
|
33
|
+
"@react-aria/textfield": "^3.18.5",
|
|
34
|
+
"@react-aria/utils": "^3.33.1",
|
|
35
|
+
"@react-stately/form": "^3.2.4",
|
|
36
|
+
"@react-stately/numberfield": "^3.11.0",
|
|
37
|
+
"@react-types/button": "^3.15.1",
|
|
38
|
+
"@react-types/numberfield": "^3.8.18",
|
|
39
|
+
"@react-types/shared": "^3.33.1",
|
|
39
40
|
"@swc/helpers": "^0.5.0"
|
|
40
41
|
},
|
|
41
42
|
"peerDependencies": {
|
|
@@ -45,5 +46,5 @@
|
|
|
45
46
|
"publishConfig": {
|
|
46
47
|
"access": "public"
|
|
47
48
|
},
|
|
48
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "8df187370053aa35f553cb388ad670f65e1ab371"
|
|
49
50
|
}
|
package/src/useNumberField.ts
CHANGED
|
@@ -10,11 +10,13 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import {announce} from '@react-aria/live-announcer';
|
|
13
14
|
import {AriaButtonProps} from '@react-types/button';
|
|
14
15
|
import {AriaNumberFieldProps} from '@react-types/numberfield';
|
|
15
|
-
import {chain, filterDOMProps, isAndroid, isIOS, isIPhone, mergeProps, useFormReset, useId} from '@react-aria/utils';
|
|
16
|
-
import {DOMAttributes, GroupDOMAttributes, TextInputDOMProps, ValidationResult} from '@react-types/shared';
|
|
16
|
+
import {chain, filterDOMProps, getActiveElement, getEventTarget, isAndroid, isIOS, isIPhone, mergeProps, useFormReset, useId} from '@react-aria/utils';
|
|
17
17
|
import {
|
|
18
|
+
type ClipboardEvent,
|
|
19
|
+
type ClipboardEventHandler,
|
|
18
20
|
InputHTMLAttributes,
|
|
19
21
|
LabelHTMLAttributes,
|
|
20
22
|
RefObject,
|
|
@@ -22,6 +24,8 @@ import {
|
|
|
22
24
|
useMemo,
|
|
23
25
|
useState
|
|
24
26
|
} from 'react';
|
|
27
|
+
import {DOMAttributes, GroupDOMAttributes, TextInputDOMProps, ValidationResult} from '@react-types/shared';
|
|
28
|
+
import {flushSync} from 'react-dom';
|
|
25
29
|
// @ts-ignore
|
|
26
30
|
import intlMessages from '../intl/*.json';
|
|
27
31
|
import {NumberFieldState} from '@react-stately/numberfield';
|
|
@@ -91,12 +95,22 @@ export function useNumberField(props: AriaNumberFieldProps, state: NumberFieldSt
|
|
|
91
95
|
} = state;
|
|
92
96
|
|
|
93
97
|
const stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/numberfield');
|
|
98
|
+
let commitAndAnnounce = useCallback(() => {
|
|
99
|
+
let oldValue = inputRef.current?.value ?? '';
|
|
100
|
+
// Set input value to normalized valid value
|
|
101
|
+
flushSync(() => {
|
|
102
|
+
commit();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
if (inputRef.current?.value !== oldValue) {
|
|
106
|
+
announce(inputRef.current?.value ?? '', 'assertive');
|
|
107
|
+
}
|
|
108
|
+
}, [commit, inputRef]);
|
|
94
109
|
|
|
95
110
|
let inputId = useId(id);
|
|
96
111
|
let {focusProps} = useFocus({
|
|
97
112
|
onBlur() {
|
|
98
|
-
|
|
99
|
-
commit();
|
|
113
|
+
commitAndAnnounce();
|
|
100
114
|
}
|
|
101
115
|
});
|
|
102
116
|
|
|
@@ -181,6 +195,23 @@ export function useNumberField(props: AriaNumberFieldProps, state: NumberFieldSt
|
|
|
181
195
|
}
|
|
182
196
|
};
|
|
183
197
|
|
|
198
|
+
let onPaste: ClipboardEventHandler<HTMLInputElement> = (e: ClipboardEvent<HTMLInputElement>) => {
|
|
199
|
+
props.onPaste?.(e);
|
|
200
|
+
let inputElement = getEventTarget(e) as HTMLInputElement;
|
|
201
|
+
// we can only handle the case where the paste takes over the entire input, otherwise things get very complicated
|
|
202
|
+
// trying to calculate the new string based on what the paste is replacing and where in the source string it is
|
|
203
|
+
if (inputElement &&
|
|
204
|
+
((inputElement.selectionEnd ?? -1) - (inputElement.selectionStart ?? 0)) === inputElement.value.length
|
|
205
|
+
) {
|
|
206
|
+
e.preventDefault();
|
|
207
|
+
// commit so that the user gets to see what it formats to immediately
|
|
208
|
+
// paste happens before inputRef's value is updated, so have to prevent the default and do it ourselves
|
|
209
|
+
// spin button will then handle announcing the new value, this should work with controlled state as well
|
|
210
|
+
// because the announcement is done as a result of the new rendered input value if there is one
|
|
211
|
+
commit(e.clipboardData?.getData?.('text/plain')?.trim() ?? '');
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
184
215
|
let domProps = filterDOMProps(props);
|
|
185
216
|
let onKeyDownEnter = useCallback((e) => {
|
|
186
217
|
if (e.nativeEvent.isComposing) {
|
|
@@ -223,6 +254,7 @@ export function useNumberField(props: AriaNumberFieldProps, state: NumberFieldSt
|
|
|
223
254
|
onFocusChange,
|
|
224
255
|
onKeyDown: useMemo(() => chain(onKeyDownEnter, onKeyDown), [onKeyDownEnter, onKeyDown]),
|
|
225
256
|
onKeyUp,
|
|
257
|
+
onPaste,
|
|
226
258
|
description,
|
|
227
259
|
errorMessage
|
|
228
260
|
}, state, inputRef);
|
|
@@ -254,7 +286,7 @@ export function useNumberField(props: AriaNumberFieldProps, state: NumberFieldSt
|
|
|
254
286
|
let onButtonPressStart = (e) => {
|
|
255
287
|
// If focus is already on the input, keep it there so we don't hide the
|
|
256
288
|
// software keyboard when tapping the increment/decrement buttons.
|
|
257
|
-
if (
|
|
289
|
+
if (getActiveElement() === inputRef.current) {
|
|
258
290
|
return;
|
|
259
291
|
}
|
|
260
292
|
|