@react-aria/textfield 3.8.1 → 3.9.0

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.
Files changed (2) hide show
  1. package/dist/import.mjs +208 -0
  2. package/package.json +12 -7
@@ -0,0 +1,208 @@
1
+ import {filterDOMProps as $kOq5K$filterDOMProps, mergeProps as $kOq5K$mergeProps} from "@react-aria/utils";
2
+ import {useField as $kOq5K$useField} from "@react-aria/label";
3
+ import {useFocusable as $kOq5K$useFocusable} from "@react-aria/focus";
4
+ import {useRef as $kOq5K$useRef, useEffect as $kOq5K$useEffect} from "react";
5
+
6
+ /*
7
+ * Copyright 2020 Adobe. All rights reserved.
8
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License. You may obtain a copy
10
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software distributed under
13
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
14
+ * OF ANY KIND, either express or implied. See the License for the specific language
15
+ * governing permissions and limitations under the License.
16
+ */ /*
17
+ * Copyright 2020 Adobe. All rights reserved.
18
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
19
+ * you may not use this file except in compliance with the License. You may obtain a copy
20
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
21
+ *
22
+ * Unless required by applicable law or agreed to in writing, software distributed under
23
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
24
+ * OF ANY KIND, either express or implied. See the License for the specific language
25
+ * governing permissions and limitations under the License.
26
+ */
27
+
28
+
29
+ function $2d73ec29415bd339$export$712718f7aec83d5(props, ref) {
30
+ let { inputElementType: inputElementType = "input" , isDisabled: isDisabled = false , isRequired: isRequired = false , isReadOnly: isReadOnly = false , validationState: validationState , type: type = "text" , onChange: onChange = ()=>{} } = props;
31
+ let { focusableProps: focusableProps } = (0, $kOq5K$useFocusable)(props, ref);
32
+ let { labelProps: labelProps , fieldProps: fieldProps , descriptionProps: descriptionProps , errorMessageProps: errorMessageProps } = (0, $kOq5K$useField)(props);
33
+ let domProps = (0, $kOq5K$filterDOMProps)(props, {
34
+ labelable: true
35
+ });
36
+ const inputOnlyProps = {
37
+ type: type,
38
+ pattern: props.pattern
39
+ };
40
+ return {
41
+ labelProps: labelProps,
42
+ inputProps: (0, $kOq5K$mergeProps)(domProps, inputElementType === "input" && inputOnlyProps, {
43
+ disabled: isDisabled,
44
+ readOnly: isReadOnly,
45
+ "aria-required": isRequired || undefined,
46
+ "aria-invalid": validationState === "invalid" || undefined,
47
+ "aria-errormessage": props["aria-errormessage"],
48
+ "aria-activedescendant": props["aria-activedescendant"],
49
+ "aria-autocomplete": props["aria-autocomplete"],
50
+ "aria-haspopup": props["aria-haspopup"],
51
+ value: props.value,
52
+ defaultValue: props.value ? undefined : props.defaultValue,
53
+ onChange: (e)=>onChange(e.target.value),
54
+ autoComplete: props.autoComplete,
55
+ maxLength: props.maxLength,
56
+ minLength: props.minLength,
57
+ name: props.name,
58
+ placeholder: props.placeholder,
59
+ inputMode: props.inputMode,
60
+ // Clipboard events
61
+ onCopy: props.onCopy,
62
+ onCut: props.onCut,
63
+ onPaste: props.onPaste,
64
+ // Composition events
65
+ onCompositionEnd: props.onCompositionEnd,
66
+ onCompositionStart: props.onCompositionStart,
67
+ onCompositionUpdate: props.onCompositionUpdate,
68
+ // Selection events
69
+ onSelect: props.onSelect,
70
+ // Input events
71
+ onBeforeInput: props.onBeforeInput,
72
+ onInput: props.onInput,
73
+ ...focusableProps,
74
+ ...fieldProps
75
+ }),
76
+ descriptionProps: descriptionProps,
77
+ errorMessageProps: errorMessageProps
78
+ };
79
+ }
80
+
81
+
82
+ /*
83
+ * Copyright 2021 Adobe. All rights reserved.
84
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
85
+ * you may not use this file except in compliance with the License. You may obtain a copy
86
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
87
+ *
88
+ * Unless required by applicable law or agreed to in writing, software distributed under
89
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
90
+ * OF ANY KIND, either express or implied. See the License for the specific language
91
+ * governing permissions and limitations under the License.
92
+ */
93
+
94
+
95
+ function $d841c8010a73d545$var$supportsNativeBeforeInputEvent() {
96
+ return typeof window !== "undefined" && window.InputEvent && // @ts-ignore
97
+ typeof InputEvent.prototype.getTargetRanges === "function";
98
+ }
99
+ function $d841c8010a73d545$export$4f384c9210e583c3(props, state, inputRef) {
100
+ let stateRef = (0, $kOq5K$useRef)(state);
101
+ stateRef.current = state;
102
+ // All browsers implement the 'beforeinput' event natively except Firefox
103
+ // (currently behind a flag as of Firefox 84). React's polyfill does not
104
+ // run in all cases that the native event fires, e.g. when deleting text.
105
+ // Use the native event if available so that we can prevent invalid deletions.
106
+ // We do not attempt to polyfill this in Firefox since it would be very complicated,
107
+ // the benefit of doing so is fairly minor, and it's going to be natively supported soon.
108
+ (0, $kOq5K$useEffect)(()=>{
109
+ if (!$d841c8010a73d545$var$supportsNativeBeforeInputEvent()) return;
110
+ let input = inputRef.current;
111
+ let onBeforeInput = (e)=>{
112
+ let state = stateRef.current;
113
+ // Compute the next value of the input if the event is allowed to proceed.
114
+ // See https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes for a full list of input types.
115
+ let nextValue;
116
+ switch(e.inputType){
117
+ case "historyUndo":
118
+ case "historyRedo":
119
+ // Explicitly allow undo/redo. e.data is null in this case, but there's no need to validate,
120
+ // because presumably the input would have already been validated previously.
121
+ return;
122
+ case "deleteContent":
123
+ case "deleteByCut":
124
+ case "deleteByDrag":
125
+ nextValue = input.value.slice(0, input.selectionStart) + input.value.slice(input.selectionEnd);
126
+ break;
127
+ case "deleteContentForward":
128
+ // This is potentially incorrect, since the browser may actually delete more than a single UTF-16
129
+ // character. In reality, a full Unicode grapheme cluster consisting of multiple UTF-16 characters
130
+ // or code points may be deleted. However, in our currently supported locales, there are no such cases.
131
+ // If we support additional locales in the future, this may need to change.
132
+ 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);
133
+ break;
134
+ case "deleteContentBackward":
135
+ 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);
136
+ break;
137
+ case "deleteSoftLineBackward":
138
+ case "deleteHardLineBackward":
139
+ nextValue = input.value.slice(input.selectionStart);
140
+ break;
141
+ default:
142
+ if (e.data != null) nextValue = input.value.slice(0, input.selectionStart) + e.data + input.value.slice(input.selectionEnd);
143
+ break;
144
+ }
145
+ // If we did not compute a value, or the new value is invalid, prevent the event
146
+ // so that the browser does not update the input text, move the selection, or add to
147
+ // the undo/redo stack.
148
+ if (nextValue == null || !state.validate(nextValue)) e.preventDefault();
149
+ };
150
+ input.addEventListener("beforeinput", onBeforeInput, false);
151
+ return ()=>{
152
+ input.removeEventListener("beforeinput", onBeforeInput, false);
153
+ };
154
+ }, [
155
+ inputRef,
156
+ stateRef
157
+ ]);
158
+ let onBeforeInput = !$d841c8010a73d545$var$supportsNativeBeforeInputEvent() ? (e)=>{
159
+ let nextValue = e.target.value.slice(0, e.target.selectionStart) + e.data + e.target.value.slice(e.target.selectionEnd);
160
+ if (!state.validate(nextValue)) e.preventDefault();
161
+ } : null;
162
+ let { labelProps: labelProps , inputProps: textFieldProps , descriptionProps: descriptionProps , errorMessageProps: errorMessageProps } = (0, $2d73ec29415bd339$export$712718f7aec83d5)(props, inputRef);
163
+ let compositionStartState = (0, $kOq5K$useRef)(null);
164
+ return {
165
+ inputProps: (0, $kOq5K$mergeProps)(textFieldProps, {
166
+ onBeforeInput: onBeforeInput,
167
+ onCompositionStart () {
168
+ // Chrome does not implement Input Events Level 2, which specifies the insertFromComposition
169
+ // and deleteByComposition inputType values for the beforeinput event. These are meant to occur
170
+ // at the end of a composition (e.g. Pinyin IME, Android auto correct, etc.), and crucially, are
171
+ // cancelable. The insertCompositionText and deleteCompositionText input types are not cancelable,
172
+ // nor would we want to cancel them because the input from the user is incomplete at that point.
173
+ // In Safari, insertFromComposition/deleteFromComposition will fire, however, allowing us to cancel
174
+ // the final composition result if it is invalid. As a fallback for Chrome and Firefox, which either
175
+ // don't support Input Events Level 2, or beforeinput at all, we store the state of the input when
176
+ // the compositionstart event fires, and undo the changes in compositionend (below) if it is invalid.
177
+ // Unfortunately, this messes up the undo/redo stack, but until insertFromComposition/deleteByComposition
178
+ // are implemented, there is no other way to prevent composed input.
179
+ // See https://bugs.chromium.org/p/chromium/issues/detail?id=1022204
180
+ let { value: value , selectionStart: selectionStart , selectionEnd: selectionEnd } = inputRef.current;
181
+ compositionStartState.current = {
182
+ value: value,
183
+ selectionStart: selectionStart,
184
+ selectionEnd: selectionEnd
185
+ };
186
+ },
187
+ onCompositionEnd () {
188
+ if (!state.validate(inputRef.current.value)) {
189
+ // Restore the input value in the DOM immediately so we can synchronously update the selection position.
190
+ // But also update the value in React state as well so it is correct for future updates.
191
+ let { value: value , selectionStart: selectionStart , selectionEnd: selectionEnd } = compositionStartState.current;
192
+ inputRef.current.value = value;
193
+ inputRef.current.setSelectionRange(selectionStart, selectionEnd);
194
+ state.setInputValue(value);
195
+ }
196
+ }
197
+ }),
198
+ labelProps: labelProps,
199
+ descriptionProps: descriptionProps,
200
+ errorMessageProps: errorMessageProps
201
+ };
202
+ }
203
+
204
+
205
+
206
+
207
+ export {$2d73ec29415bd339$export$712718f7aec83d5 as useTextField, $d841c8010a73d545$export$4f384c9210e583c3 as useFormattedTextField};
208
+ //# sourceMappingURL=module.js.map
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "@react-aria/textfield",
3
- "version": "3.8.1",
3
+ "version": "3.9.0",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/module.js",
8
+ "exports": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/import.mjs",
11
+ "require": "./dist/main.js"
12
+ },
8
13
  "types": "dist/types.d.ts",
9
14
  "source": "src/index.ts",
10
15
  "files": [
@@ -17,11 +22,11 @@
17
22
  "url": "https://github.com/adobe/react-spectrum"
18
23
  },
19
24
  "dependencies": {
20
- "@react-aria/focus": "^3.10.1",
21
- "@react-aria/label": "^3.4.4",
22
- "@react-aria/utils": "^3.14.2",
23
- "@react-types/shared": "^3.16.0",
24
- "@react-types/textfield": "^3.6.2",
25
+ "@react-aria/focus": "^3.11.0",
26
+ "@react-aria/label": "^3.5.0",
27
+ "@react-aria/utils": "^3.15.0",
28
+ "@react-types/shared": "^3.17.0",
29
+ "@react-types/textfield": "^3.7.0",
25
30
  "@swc/helpers": "^0.4.14"
26
31
  },
27
32
  "peerDependencies": {
@@ -30,5 +35,5 @@
30
35
  "publishConfig": {
31
36
  "access": "public"
32
37
  },
33
- "gitHead": "5480d76bd815e239366f92852c76b6831ad2a4fd"
38
+ "gitHead": "a0efee84aa178cb1a202951dfd6d8de02b292307"
34
39
  }