@react-aria/textfield 3.0.0-nightly-641446f65-240905

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @react-aria/textfield
2
+
3
+ This package is part of [react-spectrum](https://github.com/adobe/react-spectrum). See the repo for more details.
@@ -0,0 +1,19 @@
1
+ import {useTextField as $2d73ec29415bd339$export$712718f7aec83d5} from "./useTextField.mjs";
2
+ import {useFormattedTextField as $d841c8010a73d545$export$4f384c9210e583c3} from "./useFormattedTextField.mjs";
3
+
4
+ /*
5
+ * Copyright 2020 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+
17
+
18
+ export {$2d73ec29415bd339$export$712718f7aec83d5 as useTextField, $d841c8010a73d545$export$4f384c9210e583c3 as useFormattedTextField};
19
+ //# sourceMappingURL=module.js.map
package/dist/main.js ADDED
@@ -0,0 +1,25 @@
1
+ var $9076f978e02df845$exports = require("./useTextField.main.js");
2
+ var $56b68c3dab9bf16c$exports = require("./useFormattedTextField.main.js");
3
+
4
+
5
+ function $parcel$export(e, n, v, s) {
6
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
+ }
8
+
9
+ $parcel$export(module.exports, "useTextField", () => $9076f978e02df845$exports.useTextField);
10
+ $parcel$export(module.exports, "useFormattedTextField", () => $56b68c3dab9bf16c$exports.useFormattedTextField);
11
+ /*
12
+ * Copyright 2020 Adobe. All rights reserved.
13
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
14
+ * you may not use this file except in compliance with the License. You may obtain a copy
15
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software distributed under
18
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
19
+ * OF ANY KIND, either express or implied. See the License for the specific language
20
+ * governing permissions and limitations under the License.
21
+ */
22
+
23
+
24
+
25
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/textfield/src/index.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 */\nexport type {TextFieldAria} from './useTextField';\nexport {useTextField} from './useTextField';\nexport {useFormattedTextField} from './useFormattedTextField';\nexport type {AriaTextFieldOptions} from './useTextField';\nexport type {AriaTextFieldProps} from '@react-types/textfield';\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js ADDED
@@ -0,0 +1,19 @@
1
+ import {useTextField as $2d73ec29415bd339$export$712718f7aec83d5} from "./useTextField.module.js";
2
+ import {useFormattedTextField as $d841c8010a73d545$export$4f384c9210e583c3} from "./useFormattedTextField.module.js";
3
+
4
+ /*
5
+ * Copyright 2020 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+
17
+
18
+ export {$2d73ec29415bd339$export$712718f7aec83d5 as useTextField, $d841c8010a73d545$export$4f384c9210e583c3 as useFormattedTextField};
19
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/textfield/src/index.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 */\nexport type {TextFieldAria} from './useTextField';\nexport {useTextField} from './useTextField';\nexport {useFormattedTextField} from './useFormattedTextField';\nexport type {AriaTextFieldOptions} from './useTextField';\nexport type {AriaTextFieldProps} from '@react-types/textfield';\n"],"names":[],"version":3,"file":"module.js.map"}
@@ -0,0 +1,82 @@
1
+ import { AriaTextFieldProps } from "@react-types/textfield";
2
+ import { HTMLAttributes, JSX, LabelHTMLAttributes, RefObject } from "react";
3
+ import { DOMAttributes, ValidationResult, RefObject as _RefObject1 } from "@react-types/shared";
4
+ /**
5
+ * A map of HTML element names and their interface types.
6
+ * For example `'a'` -> `HTMLAnchorElement`.
7
+ */
8
+ type IntrinsicHTMLElements = {
9
+ [K in keyof IntrinsicHTMLAttributes]: IntrinsicHTMLAttributes[K] extends HTMLAttributes<infer T> ? T : never;
10
+ };
11
+ /**
12
+ * A map of HTML element names and their attribute interface types.
13
+ * For example `'a'` -> `AnchorHTMLAttributes<HTMLAnchorElement>`.
14
+ */
15
+ type IntrinsicHTMLAttributes = JSX.IntrinsicElements;
16
+ type DefaultElementType = 'input';
17
+ /**
18
+ * The intrinsic HTML element names that `useTextField` supports; e.g. `input`,
19
+ * `textarea`.
20
+ */
21
+ type TextFieldIntrinsicElements = keyof Pick<IntrinsicHTMLElements, 'input' | 'textarea'>;
22
+ /**
23
+ * The HTML element interfaces that `useTextField` supports based on what is
24
+ * defined for `TextFieldIntrinsicElements`; e.g. `HTMLInputElement`,
25
+ * `HTMLTextAreaElement`.
26
+ */
27
+ type TextFieldHTMLElementType = Pick<IntrinsicHTMLElements, TextFieldIntrinsicElements>;
28
+ /**
29
+ * The HTML attributes interfaces that `useTextField` supports based on what
30
+ * is defined for `TextFieldIntrinsicElements`; e.g. `InputHTMLAttributes`,
31
+ * `TextareaHTMLAttributes`.
32
+ */
33
+ type TextFieldHTMLAttributesType = Pick<IntrinsicHTMLAttributes, TextFieldIntrinsicElements>;
34
+ /**
35
+ * The type of `inputProps` returned by `useTextField`; e.g. `InputHTMLAttributes`,
36
+ * `TextareaHTMLAttributes`.
37
+ */
38
+ type TextFieldInputProps<T extends TextFieldIntrinsicElements> = TextFieldHTMLAttributesType[T];
39
+ export interface AriaTextFieldOptions<T extends TextFieldIntrinsicElements> extends AriaTextFieldProps {
40
+ /**
41
+ * The HTML element used to render the input, e.g. 'input', or 'textarea'.
42
+ * It determines whether certain HTML attributes will be included in `inputProps`.
43
+ * For example, [`type`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-type).
44
+ * @default 'input'
45
+ */
46
+ inputElementType?: T;
47
+ /**
48
+ * Controls whether inputted text is automatically capitalized and, if so, in what manner.
49
+ * See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autocapitalize).
50
+ */
51
+ autoCapitalize?: 'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters';
52
+ }
53
+ /**
54
+ * The type of `ref` object that can be passed to `useTextField` based on the given
55
+ * intrinsic HTML element name; e.g.`RefObject<HTMLInputElement>`,
56
+ * `RefObject<HTMLTextAreaElement>`.
57
+ */
58
+ type TextFieldRefObject<T extends TextFieldIntrinsicElements> = RefObject<TextFieldHTMLElementType[T] | null>;
59
+ export interface TextFieldAria<T extends TextFieldIntrinsicElements = DefaultElementType> extends ValidationResult {
60
+ /** Props for the input element. */
61
+ inputProps: TextFieldInputProps<T>;
62
+ /** Props for the text field's visible label element, if any. */
63
+ labelProps: DOMAttributes | LabelHTMLAttributes<HTMLLabelElement>;
64
+ /** Props for the text field's description element, if any. */
65
+ descriptionProps: DOMAttributes;
66
+ /** Props for the text field's error message element, if any. */
67
+ errorMessageProps: DOMAttributes;
68
+ }
69
+ /**
70
+ * Provides the behavior and accessibility implementation for a text field.
71
+ * @param props - Props for the text field.
72
+ * @param ref - Ref to the HTML input or textarea element.
73
+ */
74
+ export function useTextField<T extends TextFieldIntrinsicElements = DefaultElementType>(props: AriaTextFieldOptions<T>, ref: TextFieldRefObject<T>): TextFieldAria<T>;
75
+ interface FormattedTextFieldState {
76
+ validate: (val: string) => boolean;
77
+ setInputValue: (val: string) => void;
78
+ }
79
+ export function useFormattedTextField(props: AriaTextFieldProps, state: FormattedTextFieldState, inputRef: _RefObject1<HTMLInputElement | null>): TextFieldAria;
80
+ export type { AriaTextFieldProps } from '@react-types/textfield';
81
+
82
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;AA6BA;;;GAGG;AACH,6BAA6B;KAC1B,CAAC,IAAI,MAAM,uBAAuB,GAAG,uBAAuB,CAAC,CAAC,CAAC,SAAS,eAAe,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK;CAC7G,CAAC;AAEF;;;GAGG;AACH,+BAA+B,IAAI,iBAAiB,CAAC;AAErD,0BAA0B,OAAO,CAAC;AAElC;;;GAGG;AACH,kCAAkC,MAAM,IAAI,CAAC,qBAAqB,EAAE,OAAO,GAAG,UAAU,CAAC,CAAC;AAE1F;;;;GAIG;AACH,gCAAgC,IAAI,CAAC,qBAAqB,EAAE,0BAA0B,CA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AAC,GAAG,aAAa,CAiI5J;ACjJD,YAAY,EAAC,kBAAkB,EAAC,MAAM,wBAAwB,CAAC","sources":["packages/@react-aria/textfield/src/packages/@react-aria/textfield/src/useTextField.ts","packages/@react-aria/textfield/src/packages/@react-aria/textfield/src/useFormattedTextField.ts","packages/@react-aria/textfield/src/packages/@react-aria/textfield/src/index.ts","packages/@react-aria/textfield/src/index.ts"],"sourcesContent":[null,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 type {TextFieldAria} from './useTextField';\nexport {useTextField} from './useTextField';\nexport {useFormattedTextField} from './useFormattedTextField';\nexport type {AriaTextFieldOptions} from './useTextField';\nexport type {AriaTextFieldProps} from '@react-types/textfield';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
@@ -0,0 +1,136 @@
1
+ var $9076f978e02df845$exports = require("./useTextField.main.js");
2
+ var $6PuoT$reactariautils = require("@react-aria/utils");
3
+ var $6PuoT$react = require("react");
4
+
5
+
6
+ function $parcel$export(e, n, v, s) {
7
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8
+ }
9
+
10
+ $parcel$export(module.exports, "useFormattedTextField", () => $56b68c3dab9bf16c$export$4f384c9210e583c3);
11
+ /*
12
+ * Copyright 2021 Adobe. All rights reserved.
13
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
14
+ * you may not use this file except in compliance with the License. You may obtain a copy
15
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software distributed under
18
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
19
+ * OF ANY KIND, either express or implied. See the License for the specific language
20
+ * governing permissions and limitations under the License.
21
+ */
22
+
23
+
24
+ function $56b68c3dab9bf16c$var$supportsNativeBeforeInputEvent() {
25
+ return typeof window !== 'undefined' && window.InputEvent && // @ts-ignore
26
+ typeof InputEvent.prototype.getTargetRanges === 'function';
27
+ }
28
+ function $56b68c3dab9bf16c$export$4f384c9210e583c3(props, state, inputRef) {
29
+ // All browsers implement the 'beforeinput' event natively except Firefox
30
+ // (currently behind a flag as of Firefox 84). React's polyfill does not
31
+ // run in all cases that the native event fires, e.g. when deleting text.
32
+ // Use the native event if available so that we can prevent invalid deletions.
33
+ // We do not attempt to polyfill this in Firefox since it would be very complicated,
34
+ // the benefit of doing so is fairly minor, and it's going to be natively supported soon.
35
+ let onBeforeInputFallback = (0, $6PuoT$reactariautils.useEffectEvent)((e)=>{
36
+ let input = inputRef.current;
37
+ // Compute the next value of the input if the event is allowed to proceed.
38
+ // See https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes for a full list of input types.
39
+ let nextValue;
40
+ switch(e.inputType){
41
+ case 'historyUndo':
42
+ case 'historyRedo':
43
+ // Explicitly allow undo/redo. e.data is null in this case, but there's no need to validate,
44
+ // because presumably the input would have already been validated previously.
45
+ return;
46
+ case 'insertLineBreak':
47
+ // Explicitly allow "insertLineBreak" event, to allow onSubmit for "enter" key. e.data is null in this case.
48
+ return;
49
+ case 'deleteContent':
50
+ case 'deleteByCut':
51
+ case 'deleteByDrag':
52
+ nextValue = input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
53
+ break;
54
+ case 'deleteContentForward':
55
+ // This is potentially incorrect, since the browser may actually delete more than a single UTF-16
56
+ // character. In reality, a full Unicode grapheme cluster consisting of multiple UTF-16 characters
57
+ // or code points may be deleted. However, in our currently supported locales, there are no such cases.
58
+ // If we support additional locales in the future, this may need to change.
59
+ nextValue = input.selectionEnd === input.selectionStart ? input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd + 1) : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
60
+ break;
61
+ case 'deleteContentBackward':
62
+ nextValue = input.selectionEnd === input.selectionStart ? input.value.slice(0, input.selectionStart - 1) + input.value.slice(input.selectionStart) : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
63
+ break;
64
+ case 'deleteSoftLineBackward':
65
+ case 'deleteHardLineBackward':
66
+ nextValue = input.value.slice(input.selectionStart);
67
+ break;
68
+ default:
69
+ if (e.data != null) nextValue = input.value.slice(0, input.selectionStart) + e.data + input.value.slice(input.selectionEnd);
70
+ break;
71
+ }
72
+ // If we did not compute a value, or the new value is invalid, prevent the event
73
+ // so that the browser does not update the input text, move the selection, or add to
74
+ // the undo/redo stack.
75
+ if (nextValue == null || !state.validate(nextValue)) e.preventDefault();
76
+ });
77
+ (0, $6PuoT$react.useEffect)(()=>{
78
+ if (!$56b68c3dab9bf16c$var$supportsNativeBeforeInputEvent()) return;
79
+ let input = inputRef.current;
80
+ input.addEventListener('beforeinput', onBeforeInputFallback, false);
81
+ return ()=>{
82
+ input.removeEventListener('beforeinput', onBeforeInputFallback, false);
83
+ };
84
+ }, [
85
+ inputRef,
86
+ onBeforeInputFallback
87
+ ]);
88
+ let onBeforeInput = !$56b68c3dab9bf16c$var$supportsNativeBeforeInputEvent() ? (e)=>{
89
+ let nextValue = e.target.value.slice(0, e.target.selectionStart) + e.data + e.target.value.slice(e.target.selectionEnd);
90
+ if (!state.validate(nextValue)) e.preventDefault();
91
+ } : null;
92
+ let { labelProps: labelProps, inputProps: textFieldProps, descriptionProps: descriptionProps, errorMessageProps: errorMessageProps, ...validation } = (0, $9076f978e02df845$exports.useTextField)(props, inputRef);
93
+ let compositionStartState = (0, $6PuoT$react.useRef)(null);
94
+ return {
95
+ inputProps: (0, $6PuoT$reactariautils.mergeProps)(textFieldProps, {
96
+ onBeforeInput: onBeforeInput,
97
+ onCompositionStart () {
98
+ // Chrome does not implement Input Events Level 2, which specifies the insertFromComposition
99
+ // and deleteByComposition inputType values for the beforeinput event. These are meant to occur
100
+ // at the end of a composition (e.g. Pinyin IME, Android auto correct, etc.), and crucially, are
101
+ // cancelable. The insertCompositionText and deleteCompositionText input types are not cancelable,
102
+ // nor would we want to cancel them because the input from the user is incomplete at that point.
103
+ // In Safari, insertFromComposition/deleteFromComposition will fire, however, allowing us to cancel
104
+ // the final composition result if it is invalid. As a fallback for Chrome and Firefox, which either
105
+ // don't support Input Events Level 2, or beforeinput at all, we store the state of the input when
106
+ // the compositionstart event fires, and undo the changes in compositionend (below) if it is invalid.
107
+ // Unfortunately, this messes up the undo/redo stack, but until insertFromComposition/deleteByComposition
108
+ // are implemented, there is no other way to prevent composed input.
109
+ // See https://bugs.chromium.org/p/chromium/issues/detail?id=1022204
110
+ let { value: value, selectionStart: selectionStart, selectionEnd: selectionEnd } = inputRef.current;
111
+ compositionStartState.current = {
112
+ value: value,
113
+ selectionStart: selectionStart,
114
+ selectionEnd: selectionEnd
115
+ };
116
+ },
117
+ onCompositionEnd () {
118
+ if (!state.validate(inputRef.current.value)) {
119
+ // Restore the input value in the DOM immediately so we can synchronously update the selection position.
120
+ // But also update the value in React state as well so it is correct for future updates.
121
+ let { value: value, selectionStart: selectionStart, selectionEnd: selectionEnd } = compositionStartState.current;
122
+ inputRef.current.value = value;
123
+ inputRef.current.setSelectionRange(selectionStart, selectionEnd);
124
+ state.setInputValue(value);
125
+ }
126
+ }
127
+ }),
128
+ labelProps: labelProps,
129
+ descriptionProps: descriptionProps,
130
+ errorMessageProps: errorMessageProps,
131
+ ...validation
132
+ };
133
+ }
134
+
135
+
136
+ //# sourceMappingURL=useFormattedTextField.main.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;AAcD,SAAS;IACP,OAAO,OAAO,WAAW,eACvB,OAAO,UAAU,IACjB,aAAa;IACb,OAAO,WAAW,SAAS,CAAC,eAAe,KAAK;AAC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ACpE,IAAI,SAAC,KAAK,kBAAE,cAAc,gBAAE,YAAY,EAAC,GAAG,SAAS,OAAO;gBAC5D,sBAAsB,OAAO,GAAG;2BAAC;oCAAO;kCAAgB;gBAAY;YACtE;YACA;gBACE,IAAI,CAAC,MAAM,QAAQ,CAAC,SAAS,OAAO,CAAC,KAAK,GAAG;oBAC3C,wGAAwG;oBACxG,wFAAwF;oBACxF,IAAI,SAAC,KAAK,kBAAE,cAAc,gBAAE,YAAY,EAAC,GAAG,sBAAsB,OAAO;oBACzE,SAAS,OAAO,CAAC,KAAK,GAAG;oBACzB,SAAS,OAAO,CAAC,iBAAiB,CAAC,gBAAgB;oBACnD,MAAM,aAAa,CAAC;gBACtB;YACF;QACF;oBAEF;0BACA;2BACA;QACA,GAAG,UAAU;IACf;AACF","sources":["packages/@react-aria/textfield/src/useFormattedTextField.ts"],"sourcesContent":["/*\n * Copyright 2021 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 {AriaTextFieldProps} from '@react-types/textfield';\nimport {mergeProps, useEffectEvent} from '@react-aria/utils';\nimport {RefObject} from '@react-types/shared';\nimport {TextFieldAria, useTextField} from './useTextField';\nimport {useEffect, useRef} from 'react';\n\ninterface FormattedTextFieldState {\n validate: (val: string) => boolean,\n setInputValue: (val: string) => void\n}\n\n\nfunction supportsNativeBeforeInputEvent() {\n return typeof window !== 'undefined' &&\n window.InputEvent &&\n // @ts-ignore\n typeof InputEvent.prototype.getTargetRanges === 'function';\n}\n\nexport function useFormattedTextField(props: AriaTextFieldProps, state: FormattedTextFieldState, inputRef: RefObject<HTMLInputElement | null>): TextFieldAria {\n // All browsers implement the 'beforeinput' event natively except Firefox\n // (currently behind a flag as of Firefox 84). React's polyfill does not\n // run in all cases that the native event fires, e.g. when deleting text.\n // Use the native event if available so that we can prevent invalid deletions.\n // We do not attempt to polyfill this in Firefox since it would be very complicated,\n // the benefit of doing so is fairly minor, and it's going to be natively supported soon.\n let onBeforeInputFallback = useEffectEvent((e: InputEvent) => {\n let input = inputRef.current;\n\n // Compute the next value of the input if the event is allowed to proceed.\n // See https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes for a full list of input types.\n let nextValue: string;\n switch (e.inputType) {\n case 'historyUndo':\n case 'historyRedo':\n // Explicitly allow undo/redo. e.data is null in this case, but there's no need to validate,\n // because presumably the input would have already been validated previously.\n return;\n case 'insertLineBreak':\n // Explicitly allow \"insertLineBreak\" event, to allow onSubmit for \"enter\" key. e.data is null in this case.\n return;\n case 'deleteContent':\n case 'deleteByCut':\n case 'deleteByDrag':\n nextValue = input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);\n break;\n case 'deleteContentForward':\n // This is potentially incorrect, since the browser may actually delete more than a single UTF-16\n // character. In reality, a full Unicode grapheme cluster consisting of multiple UTF-16 characters\n // or code points may be deleted. However, in our currently supported locales, there are no such cases.\n // If we support additional locales in the future, this may need to change.\n nextValue = input.selectionEnd === input.selectionStart\n ? input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd + 1)\n : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);\n break;\n case 'deleteContentBackward':\n nextValue = input.selectionEnd === input.selectionStart\n ? input.value.slice(0, input.selectionStart - 1) + input.value.slice(input.selectionStart)\n : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);\n break;\n case 'deleteSoftLineBackward':\n case 'deleteHardLineBackward':\n nextValue = input.value.slice(input.selectionStart);\n break;\n default:\n if (e.data != null) {\n nextValue =\n input.value.slice(0, input.selectionStart) +\n e.data +\n input.value.slice(input.selectionEnd);\n }\n break;\n }\n\n // If we did not compute a value, or the new value is invalid, prevent the event\n // so that the browser does not update the input text, move the selection, or add to\n // the undo/redo stack.\n if (nextValue == null || !state.validate(nextValue)) {\n e.preventDefault();\n }\n });\n\n useEffect(() => {\n if (!supportsNativeBeforeInputEvent()) {\n return;\n }\n\n let input = inputRef.current;\n input.addEventListener('beforeinput', onBeforeInputFallback, false);\n return () => {\n input.removeEventListener('beforeinput', onBeforeInputFallback, false);\n };\n }, [inputRef, onBeforeInputFallback]);\n\n let onBeforeInput = !supportsNativeBeforeInputEvent()\n ? e => {\n let nextValue =\n e.target.value.slice(0, e.target.selectionStart) +\n e.data +\n e.target.value.slice(e.target.selectionEnd);\n\n if (!state.validate(nextValue)) {\n e.preventDefault();\n }\n }\n : null;\n\n let {labelProps, inputProps: textFieldProps, descriptionProps, errorMessageProps, ...validation} = useTextField(props, inputRef);\n\n let compositionStartState = useRef(null);\n return {\n inputProps: mergeProps(\n textFieldProps,\n {\n onBeforeInput,\n onCompositionStart() {\n // Chrome does not implement Input Events Level 2, which specifies the insertFromComposition\n // and deleteByComposition inputType values for the beforeinput event. These are meant to occur\n // at the end of a composition (e.g. Pinyin IME, Android auto correct, etc.), and crucially, are\n // cancelable. The insertCompositionText and deleteCompositionText input types are not cancelable,\n // nor would we want to cancel them because the input from the user is incomplete at that point.\n // In Safari, insertFromComposition/deleteFromComposition will fire, however, allowing us to cancel\n // the final composition result if it is invalid. As a fallback for Chrome and Firefox, which either\n // don't support Input Events Level 2, or beforeinput at all, we store the state of the input when\n // the compositionstart event fires, and undo the changes in compositionend (below) if it is invalid.\n // Unfortunately, this messes up the undo/redo stack, but until insertFromComposition/deleteByComposition\n // are implemented, there is no other way to prevent composed input.\n // See https://bugs.chromium.org/p/chromium/issues/detail?id=1022204\n let {value, selectionStart, selectionEnd} = inputRef.current;\n compositionStartState.current = {value, selectionStart, selectionEnd};\n },\n onCompositionEnd() {\n if (!state.validate(inputRef.current.value)) {\n // Restore the input value in the DOM immediately so we can synchronously update the selection position.\n // But also update the value in React state as well so it is correct for future updates.\n let {value, selectionStart, selectionEnd} = compositionStartState.current;\n inputRef.current.value = value;\n inputRef.current.setSelectionRange(selectionStart, selectionEnd);\n state.setInputValue(value);\n }\n }\n }\n ),\n labelProps,\n descriptionProps,\n errorMessageProps,\n ...validation\n };\n}\n"],"names":[],"version":3,"file":"useFormattedTextField.main.js.map"}
@@ -0,0 +1,131 @@
1
+ import {useTextField as $2d73ec29415bd339$export$712718f7aec83d5} from "./useTextField.mjs";
2
+ import {useEffectEvent as $jyGKS$useEffectEvent, mergeProps as $jyGKS$mergeProps} from "@react-aria/utils";
3
+ import {useEffect as $jyGKS$useEffect, useRef as $jyGKS$useRef} from "react";
4
+
5
+ /*
6
+ * Copyright 2021 Adobe. All rights reserved.
7
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License. You may obtain a copy
9
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software distributed under
12
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
13
+ * OF ANY KIND, either express or implied. See the License for the specific language
14
+ * governing permissions and limitations under the License.
15
+ */
16
+
17
+
18
+ function $d841c8010a73d545$var$supportsNativeBeforeInputEvent() {
19
+ return typeof window !== 'undefined' && window.InputEvent && // @ts-ignore
20
+ typeof InputEvent.prototype.getTargetRanges === 'function';
21
+ }
22
+ function $d841c8010a73d545$export$4f384c9210e583c3(props, state, inputRef) {
23
+ // All browsers implement the 'beforeinput' event natively except Firefox
24
+ // (currently behind a flag as of Firefox 84). React's polyfill does not
25
+ // run in all cases that the native event fires, e.g. when deleting text.
26
+ // Use the native event if available so that we can prevent invalid deletions.
27
+ // We do not attempt to polyfill this in Firefox since it would be very complicated,
28
+ // the benefit of doing so is fairly minor, and it's going to be natively supported soon.
29
+ let onBeforeInputFallback = (0, $jyGKS$useEffectEvent)((e)=>{
30
+ let input = inputRef.current;
31
+ // Compute the next value of the input if the event is allowed to proceed.
32
+ // See https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes for a full list of input types.
33
+ let nextValue;
34
+ switch(e.inputType){
35
+ case 'historyUndo':
36
+ case 'historyRedo':
37
+ // Explicitly allow undo/redo. e.data is null in this case, but there's no need to validate,
38
+ // because presumably the input would have already been validated previously.
39
+ return;
40
+ case 'insertLineBreak':
41
+ // Explicitly allow "insertLineBreak" event, to allow onSubmit for "enter" key. e.data is null in this case.
42
+ return;
43
+ case 'deleteContent':
44
+ case 'deleteByCut':
45
+ case 'deleteByDrag':
46
+ nextValue = input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
47
+ break;
48
+ case 'deleteContentForward':
49
+ // This is potentially incorrect, since the browser may actually delete more than a single UTF-16
50
+ // character. In reality, a full Unicode grapheme cluster consisting of multiple UTF-16 characters
51
+ // or code points may be deleted. However, in our currently supported locales, there are no such cases.
52
+ // If we support additional locales in the future, this may need to change.
53
+ nextValue = input.selectionEnd === input.selectionStart ? input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd + 1) : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
54
+ break;
55
+ case 'deleteContentBackward':
56
+ nextValue = input.selectionEnd === input.selectionStart ? input.value.slice(0, input.selectionStart - 1) + input.value.slice(input.selectionStart) : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
57
+ break;
58
+ case 'deleteSoftLineBackward':
59
+ case 'deleteHardLineBackward':
60
+ nextValue = input.value.slice(input.selectionStart);
61
+ break;
62
+ default:
63
+ if (e.data != null) nextValue = input.value.slice(0, input.selectionStart) + e.data + input.value.slice(input.selectionEnd);
64
+ break;
65
+ }
66
+ // If we did not compute a value, or the new value is invalid, prevent the event
67
+ // so that the browser does not update the input text, move the selection, or add to
68
+ // the undo/redo stack.
69
+ if (nextValue == null || !state.validate(nextValue)) e.preventDefault();
70
+ });
71
+ (0, $jyGKS$useEffect)(()=>{
72
+ if (!$d841c8010a73d545$var$supportsNativeBeforeInputEvent()) return;
73
+ let input = inputRef.current;
74
+ input.addEventListener('beforeinput', onBeforeInputFallback, false);
75
+ return ()=>{
76
+ input.removeEventListener('beforeinput', onBeforeInputFallback, false);
77
+ };
78
+ }, [
79
+ inputRef,
80
+ onBeforeInputFallback
81
+ ]);
82
+ let onBeforeInput = !$d841c8010a73d545$var$supportsNativeBeforeInputEvent() ? (e)=>{
83
+ let nextValue = e.target.value.slice(0, e.target.selectionStart) + e.data + e.target.value.slice(e.target.selectionEnd);
84
+ if (!state.validate(nextValue)) e.preventDefault();
85
+ } : null;
86
+ let { labelProps: labelProps, inputProps: textFieldProps, descriptionProps: descriptionProps, errorMessageProps: errorMessageProps, ...validation } = (0, $2d73ec29415bd339$export$712718f7aec83d5)(props, inputRef);
87
+ let compositionStartState = (0, $jyGKS$useRef)(null);
88
+ return {
89
+ inputProps: (0, $jyGKS$mergeProps)(textFieldProps, {
90
+ onBeforeInput: onBeforeInput,
91
+ onCompositionStart () {
92
+ // Chrome does not implement Input Events Level 2, which specifies the insertFromComposition
93
+ // and deleteByComposition inputType values for the beforeinput event. These are meant to occur
94
+ // at the end of a composition (e.g. Pinyin IME, Android auto correct, etc.), and crucially, are
95
+ // cancelable. The insertCompositionText and deleteCompositionText input types are not cancelable,
96
+ // nor would we want to cancel them because the input from the user is incomplete at that point.
97
+ // In Safari, insertFromComposition/deleteFromComposition will fire, however, allowing us to cancel
98
+ // the final composition result if it is invalid. As a fallback for Chrome and Firefox, which either
99
+ // don't support Input Events Level 2, or beforeinput at all, we store the state of the input when
100
+ // the compositionstart event fires, and undo the changes in compositionend (below) if it is invalid.
101
+ // Unfortunately, this messes up the undo/redo stack, but until insertFromComposition/deleteByComposition
102
+ // are implemented, there is no other way to prevent composed input.
103
+ // See https://bugs.chromium.org/p/chromium/issues/detail?id=1022204
104
+ let { value: value, selectionStart: selectionStart, selectionEnd: selectionEnd } = inputRef.current;
105
+ compositionStartState.current = {
106
+ value: value,
107
+ selectionStart: selectionStart,
108
+ selectionEnd: selectionEnd
109
+ };
110
+ },
111
+ onCompositionEnd () {
112
+ if (!state.validate(inputRef.current.value)) {
113
+ // Restore the input value in the DOM immediately so we can synchronously update the selection position.
114
+ // But also update the value in React state as well so it is correct for future updates.
115
+ let { value: value, selectionStart: selectionStart, selectionEnd: selectionEnd } = compositionStartState.current;
116
+ inputRef.current.value = value;
117
+ inputRef.current.setSelectionRange(selectionStart, selectionEnd);
118
+ state.setInputValue(value);
119
+ }
120
+ }
121
+ }),
122
+ labelProps: labelProps,
123
+ descriptionProps: descriptionProps,
124
+ errorMessageProps: errorMessageProps,
125
+ ...validation
126
+ };
127
+ }
128
+
129
+
130
+ export {$d841c8010a73d545$export$4f384c9210e583c3 as useFormattedTextField};
131
+ //# sourceMappingURL=useFormattedTextField.module.js.map
@@ -0,0 +1,131 @@
1
+ import {useTextField as $2d73ec29415bd339$export$712718f7aec83d5} from "./useTextField.module.js";
2
+ import {useEffectEvent as $jyGKS$useEffectEvent, mergeProps as $jyGKS$mergeProps} from "@react-aria/utils";
3
+ import {useEffect as $jyGKS$useEffect, useRef as $jyGKS$useRef} from "react";
4
+
5
+ /*
6
+ * Copyright 2021 Adobe. All rights reserved.
7
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License. You may obtain a copy
9
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software distributed under
12
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
13
+ * OF ANY KIND, either express or implied. See the License for the specific language
14
+ * governing permissions and limitations under the License.
15
+ */
16
+
17
+
18
+ function $d841c8010a73d545$var$supportsNativeBeforeInputEvent() {
19
+ return typeof window !== 'undefined' && window.InputEvent && // @ts-ignore
20
+ typeof InputEvent.prototype.getTargetRanges === 'function';
21
+ }
22
+ function $d841c8010a73d545$export$4f384c9210e583c3(props, state, inputRef) {
23
+ // All browsers implement the 'beforeinput' event natively except Firefox
24
+ // (currently behind a flag as of Firefox 84). React's polyfill does not
25
+ // run in all cases that the native event fires, e.g. when deleting text.
26
+ // Use the native event if available so that we can prevent invalid deletions.
27
+ // We do not attempt to polyfill this in Firefox since it would be very complicated,
28
+ // the benefit of doing so is fairly minor, and it's going to be natively supported soon.
29
+ let onBeforeInputFallback = (0, $jyGKS$useEffectEvent)((e)=>{
30
+ let input = inputRef.current;
31
+ // Compute the next value of the input if the event is allowed to proceed.
32
+ // See https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes for a full list of input types.
33
+ let nextValue;
34
+ switch(e.inputType){
35
+ case 'historyUndo':
36
+ case 'historyRedo':
37
+ // Explicitly allow undo/redo. e.data is null in this case, but there's no need to validate,
38
+ // because presumably the input would have already been validated previously.
39
+ return;
40
+ case 'insertLineBreak':
41
+ // Explicitly allow "insertLineBreak" event, to allow onSubmit for "enter" key. e.data is null in this case.
42
+ return;
43
+ case 'deleteContent':
44
+ case 'deleteByCut':
45
+ case 'deleteByDrag':
46
+ nextValue = input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
47
+ break;
48
+ case 'deleteContentForward':
49
+ // This is potentially incorrect, since the browser may actually delete more than a single UTF-16
50
+ // character. In reality, a full Unicode grapheme cluster consisting of multiple UTF-16 characters
51
+ // or code points may be deleted. However, in our currently supported locales, there are no such cases.
52
+ // If we support additional locales in the future, this may need to change.
53
+ nextValue = input.selectionEnd === input.selectionStart ? input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd + 1) : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
54
+ break;
55
+ case 'deleteContentBackward':
56
+ nextValue = input.selectionEnd === input.selectionStart ? input.value.slice(0, input.selectionStart - 1) + input.value.slice(input.selectionStart) : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
57
+ break;
58
+ case 'deleteSoftLineBackward':
59
+ case 'deleteHardLineBackward':
60
+ nextValue = input.value.slice(input.selectionStart);
61
+ break;
62
+ default:
63
+ if (e.data != null) nextValue = input.value.slice(0, input.selectionStart) + e.data + input.value.slice(input.selectionEnd);
64
+ break;
65
+ }
66
+ // If we did not compute a value, or the new value is invalid, prevent the event
67
+ // so that the browser does not update the input text, move the selection, or add to
68
+ // the undo/redo stack.
69
+ if (nextValue == null || !state.validate(nextValue)) e.preventDefault();
70
+ });
71
+ (0, $jyGKS$useEffect)(()=>{
72
+ if (!$d841c8010a73d545$var$supportsNativeBeforeInputEvent()) return;
73
+ let input = inputRef.current;
74
+ input.addEventListener('beforeinput', onBeforeInputFallback, false);
75
+ return ()=>{
76
+ input.removeEventListener('beforeinput', onBeforeInputFallback, false);
77
+ };
78
+ }, [
79
+ inputRef,
80
+ onBeforeInputFallback
81
+ ]);
82
+ let onBeforeInput = !$d841c8010a73d545$var$supportsNativeBeforeInputEvent() ? (e)=>{
83
+ let nextValue = e.target.value.slice(0, e.target.selectionStart) + e.data + e.target.value.slice(e.target.selectionEnd);
84
+ if (!state.validate(nextValue)) e.preventDefault();
85
+ } : null;
86
+ let { labelProps: labelProps, inputProps: textFieldProps, descriptionProps: descriptionProps, errorMessageProps: errorMessageProps, ...validation } = (0, $2d73ec29415bd339$export$712718f7aec83d5)(props, inputRef);
87
+ let compositionStartState = (0, $jyGKS$useRef)(null);
88
+ return {
89
+ inputProps: (0, $jyGKS$mergeProps)(textFieldProps, {
90
+ onBeforeInput: onBeforeInput,
91
+ onCompositionStart () {
92
+ // Chrome does not implement Input Events Level 2, which specifies the insertFromComposition
93
+ // and deleteByComposition inputType values for the beforeinput event. These are meant to occur
94
+ // at the end of a composition (e.g. Pinyin IME, Android auto correct, etc.), and crucially, are
95
+ // cancelable. The insertCompositionText and deleteCompositionText input types are not cancelable,
96
+ // nor would we want to cancel them because the input from the user is incomplete at that point.
97
+ // In Safari, insertFromComposition/deleteFromComposition will fire, however, allowing us to cancel
98
+ // the final composition result if it is invalid. As a fallback for Chrome and Firefox, which either
99
+ // don't support Input Events Level 2, or beforeinput at all, we store the state of the input when
100
+ // the compositionstart event fires, and undo the changes in compositionend (below) if it is invalid.
101
+ // Unfortunately, this messes up the undo/redo stack, but until insertFromComposition/deleteByComposition
102
+ // are implemented, there is no other way to prevent composed input.
103
+ // See https://bugs.chromium.org/p/chromium/issues/detail?id=1022204
104
+ let { value: value, selectionStart: selectionStart, selectionEnd: selectionEnd } = inputRef.current;
105
+ compositionStartState.current = {
106
+ value: value,
107
+ selectionStart: selectionStart,
108
+ selectionEnd: selectionEnd
109
+ };
110
+ },
111
+ onCompositionEnd () {
112
+ if (!state.validate(inputRef.current.value)) {
113
+ // Restore the input value in the DOM immediately so we can synchronously update the selection position.
114
+ // But also update the value in React state as well so it is correct for future updates.
115
+ let { value: value, selectionStart: selectionStart, selectionEnd: selectionEnd } = compositionStartState.current;
116
+ inputRef.current.value = value;
117
+ inputRef.current.setSelectionRange(selectionStart, selectionEnd);
118
+ state.setInputValue(value);
119
+ }
120
+ }
121
+ }),
122
+ labelProps: labelProps,
123
+ descriptionProps: descriptionProps,
124
+ errorMessageProps: errorMessageProps,
125
+ ...validation
126
+ };
127
+ }
128
+
129
+
130
+ export {$d841c8010a73d545$export$4f384c9210e583c3 as useFormattedTextField};
131
+ //# sourceMappingURL=useFormattedTextField.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AAcD,SAAS;IACP,OAAO,OAAO,WAAW,eACvB,OAAO,UAAU,IACjB,aAAa;IACb,OAAO,WAAW,SAAS,CAAC,eAAe,KAAK;AACpD;AAEO,SAAS,0CAAsB,KAAyB,EAAE,KAA8B,EAAE,QAA4C;IAC3I,yEAAyE;IACzE,wEAAwE;IACxE,yEAAyE;IACzE,8EAA8E;IAC9E,oFAAoF;IACpF,yFAAyF;IACzF,IAAI,wBAAwB,CAAA,GAAA,qBAAa,EAAE,CAAC;QAC1C,IAAI,QAAQ,SAAS,OAAO;QAE5B,0EAA0E;QAC1E,4GAA4G;QAC5G,IAAI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cAAc,gBAAE,YAAY,EAAC,GAAG,sBAAsB,OAAO;oBACzE,SAAS,OAAO,CAAC,KAAK,GAAG;oBACzB,SAAS,OAAO,CAAC,iBAAiB,CAAC,gBAAgB;oBACnD,MAAM,aAAa,CAAC;gBACtB;YACF;QACF;oBAEF;0BACA;2BACA;QACA,GAAG,UAAU;IACf;AACF","sources":["packages/@react-aria/textfield/src/useFormattedTextField.ts"],"sourcesContent":["/*\n * Copyright 2021 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 {AriaTextFieldProps} from '@react-types/textfield';\nimport {mergeProps, useEffectEvent} from '@react-aria/utils';\nimport {RefObject} from '@react-types/shared';\nimport {TextFieldAria, useTextField} from './useTextField';\nimport {useEffect, useRef} from 'react';\n\ninterface FormattedTextFieldState {\n validate: (val: string) => boolean,\n setInputValue: (val: string) => void\n}\n\n\nfunction supportsNativeBeforeInputEvent() {\n return typeof window !== 'undefined' &&\n window.InputEvent &&\n // @ts-ignore\n typeof InputEvent.prototype.getTargetRanges === 'function';\n}\n\nexport function useFormattedTextField(props: AriaTextFieldProps, state: FormattedTextFieldState, inputRef: RefObject<HTMLInputElement | null>): TextFieldAria {\n // All browsers implement the 'beforeinput' event natively except Firefox\n // (currently behind a flag as of Firefox 84). React's polyfill does not\n // run in all cases that the native event fires, e.g. when deleting text.\n // Use the native event if available so that we can prevent invalid deletions.\n // We do not attempt to polyfill this in Firefox since it would be very complicated,\n // the benefit of doing so is fairly minor, and it's going to be natively supported soon.\n let onBeforeInputFallback = useEffectEvent((e: InputEvent) => {\n let input = inputRef.current;\n\n // Compute the next value of the input if the event is allowed to proceed.\n // See https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes for a full list of input types.\n let nextValue: string;\n switch (e.inputType) {\n case 'historyUndo':\n case 'historyRedo':\n // Explicitly allow undo/redo. e.data is null in this case, but there's no need to validate,\n // because presumably the input would have already been validated previously.\n return;\n case 'insertLineBreak':\n // Explicitly allow \"insertLineBreak\" event, to allow onSubmit for \"enter\" key. e.data is null in this case.\n return;\n case 'deleteContent':\n case 'deleteByCut':\n case 'deleteByDrag':\n nextValue = input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);\n break;\n case 'deleteContentForward':\n // This is potentially incorrect, since the browser may actually delete more than a single UTF-16\n // character. In reality, a full Unicode grapheme cluster consisting of multiple UTF-16 characters\n // or code points may be deleted. However, in our currently supported locales, there are no such cases.\n // If we support additional locales in the future, this may need to change.\n nextValue = input.selectionEnd === input.selectionStart\n ? input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd + 1)\n : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);\n break;\n case 'deleteContentBackward':\n nextValue = input.selectionEnd === input.selectionStart\n ? input.value.slice(0, input.selectionStart - 1) + input.value.slice(input.selectionStart)\n : input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);\n break;\n case 'deleteSoftLineBackward':\n case 'deleteHardLineBackward':\n nextValue = input.value.slice(input.selectionStart);\n break;\n default:\n if (e.data != null) {\n nextValue =\n input.value.slice(0, input.selectionStart) +\n e.data +\n input.value.slice(input.selectionEnd);\n }\n break;\n }\n\n // If we did not compute a value, or the new value is invalid, prevent the event\n // so that the browser does not update the input text, move the selection, or add to\n // the undo/redo stack.\n if (nextValue == null || !state.validate(nextValue)) {\n e.preventDefault();\n }\n });\n\n useEffect(() => {\n if (!supportsNativeBeforeInputEvent()) {\n return;\n }\n\n let input = inputRef.current;\n input.addEventListener('beforeinput', onBeforeInputFallback, false);\n return () => {\n input.removeEventListener('beforeinput', onBeforeInputFallback, false);\n };\n }, [inputRef, onBeforeInputFallback]);\n\n let onBeforeInput = !supportsNativeBeforeInputEvent()\n ? e => {\n let nextValue =\n e.target.value.slice(0, e.target.selectionStart) +\n e.data +\n e.target.value.slice(e.target.selectionEnd);\n\n if (!state.validate(nextValue)) {\n e.preventDefault();\n }\n }\n : null;\n\n let {labelProps, inputProps: textFieldProps, descriptionProps, errorMessageProps, ...validation} = useTextField(props, inputRef);\n\n let compositionStartState = useRef(null);\n return {\n inputProps: mergeProps(\n textFieldProps,\n {\n onBeforeInput,\n onCompositionStart() {\n // Chrome does not implement Input Events Level 2, which specifies the insertFromComposition\n // and deleteByComposition inputType values for the beforeinput event. These are meant to occur\n // at the end of a composition (e.g. Pinyin IME, Android auto correct, etc.), and crucially, are\n // cancelable. The insertCompositionText and deleteCompositionText input types are not cancelable,\n // nor would we want to cancel them because the input from the user is incomplete at that point.\n // In Safari, insertFromComposition/deleteFromComposition will fire, however, allowing us to cancel\n // the final composition result if it is invalid. As a fallback for Chrome and Firefox, which either\n // don't support Input Events Level 2, or beforeinput at all, we store the state of the input when\n // the compositionstart event fires, and undo the changes in compositionend (below) if it is invalid.\n // Unfortunately, this messes up the undo/redo stack, but until insertFromComposition/deleteByComposition\n // are implemented, there is no other way to prevent composed input.\n // See https://bugs.chromium.org/p/chromium/issues/detail?id=1022204\n let {value, selectionStart, selectionEnd} = inputRef.current;\n compositionStartState.current = {value, selectionStart, selectionEnd};\n },\n onCompositionEnd() {\n if (!state.validate(inputRef.current.value)) {\n // Restore the input value in the DOM immediately so we can synchronously update the selection position.\n // But also update the value in React state as well so it is correct for future updates.\n let {value, selectionStart, selectionEnd} = compositionStartState.current;\n inputRef.current.value = value;\n inputRef.current.setSelectionRange(selectionStart, selectionEnd);\n state.setInputValue(value);\n }\n }\n }\n ),\n labelProps,\n descriptionProps,\n errorMessageProps,\n ...validation\n };\n}\n"],"names":[],"version":3,"file":"useFormattedTextField.module.js.map"}