@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.
@@ -0,0 +1,213 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {AriaTextFieldProps} from '@react-types/textfield';
14
+ import {
15
+ ChangeEvent,
16
+ HTMLAttributes,
17
+ type JSX,
18
+ LabelHTMLAttributes,
19
+ RefObject,
20
+ useEffect
21
+ } from 'react';
22
+ import {DOMAttributes, ValidationResult} from '@react-types/shared';
23
+ import {filterDOMProps, getOwnerWindow, mergeProps, useFormReset} from '@react-aria/utils';
24
+ import {useControlledState} from '@react-stately/utils';
25
+ import {useField} from '@react-aria/label';
26
+ import {useFocusable} from '@react-aria/focus';
27
+ import {useFormValidation} from '@react-aria/form';
28
+ import {useFormValidationState} from '@react-stately/form';
29
+
30
+ /**
31
+ * A map of HTML element names and their interface types.
32
+ * For example `'a'` -> `HTMLAnchorElement`.
33
+ */
34
+ type IntrinsicHTMLElements = {
35
+ [K in keyof IntrinsicHTMLAttributes]: IntrinsicHTMLAttributes[K] extends HTMLAttributes<infer T> ? T : never
36
+ };
37
+
38
+ /**
39
+ * A map of HTML element names and their attribute interface types.
40
+ * For example `'a'` -> `AnchorHTMLAttributes<HTMLAnchorElement>`.
41
+ */
42
+ type IntrinsicHTMLAttributes = JSX.IntrinsicElements;
43
+
44
+ type DefaultElementType = 'input';
45
+
46
+ /**
47
+ * The intrinsic HTML element names that `useTextField` supports; e.g. `input`,
48
+ * `textarea`.
49
+ */
50
+ type TextFieldIntrinsicElements = keyof Pick<IntrinsicHTMLElements, 'input' | 'textarea'>;
51
+
52
+ /**
53
+ * The HTML element interfaces that `useTextField` supports based on what is
54
+ * defined for `TextFieldIntrinsicElements`; e.g. `HTMLInputElement`,
55
+ * `HTMLTextAreaElement`.
56
+ */
57
+ type TextFieldHTMLElementType = Pick<IntrinsicHTMLElements, TextFieldIntrinsicElements>;
58
+
59
+ /**
60
+ * The HTML attributes interfaces that `useTextField` supports based on what
61
+ * is defined for `TextFieldIntrinsicElements`; e.g. `InputHTMLAttributes`,
62
+ * `TextareaHTMLAttributes`.
63
+ */
64
+ type TextFieldHTMLAttributesType = Pick<IntrinsicHTMLAttributes, TextFieldIntrinsicElements>;
65
+
66
+ /**
67
+ * The type of `inputProps` returned by `useTextField`; e.g. `InputHTMLAttributes`,
68
+ * `TextareaHTMLAttributes`.
69
+ */
70
+ type TextFieldInputProps<T extends TextFieldIntrinsicElements> = TextFieldHTMLAttributesType[T];
71
+
72
+ export interface AriaTextFieldOptions<T extends TextFieldIntrinsicElements> extends AriaTextFieldProps {
73
+ /**
74
+ * The HTML element used to render the input, e.g. 'input', or 'textarea'.
75
+ * It determines whether certain HTML attributes will be included in `inputProps`.
76
+ * For example, [`type`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-type).
77
+ * @default 'input'
78
+ */
79
+ inputElementType?: T,
80
+ /**
81
+ * Controls whether inputted text is automatically capitalized and, if so, in what manner.
82
+ * See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autocapitalize).
83
+ */
84
+ autoCapitalize?: 'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters'
85
+ }
86
+
87
+ /**
88
+ * The type of `ref` object that can be passed to `useTextField` based on the given
89
+ * intrinsic HTML element name; e.g.`RefObject<HTMLInputElement>`,
90
+ * `RefObject<HTMLTextAreaElement>`.
91
+ */
92
+ type TextFieldRefObject<T extends TextFieldIntrinsicElements> = RefObject<TextFieldHTMLElementType[T] | null>;
93
+
94
+ export interface TextFieldAria<T extends TextFieldIntrinsicElements = DefaultElementType> extends ValidationResult {
95
+ /** Props for the input element. */
96
+ inputProps: TextFieldInputProps<T>,
97
+ /** Props for the text field's visible label element, if any. */
98
+ labelProps: DOMAttributes | LabelHTMLAttributes<HTMLLabelElement>,
99
+ /** Props for the text field's description element, if any. */
100
+ descriptionProps: DOMAttributes,
101
+ /** Props for the text field's error message element, if any. */
102
+ errorMessageProps: DOMAttributes
103
+ }
104
+
105
+ /**
106
+ * Provides the behavior and accessibility implementation for a text field.
107
+ * @param props - Props for the text field.
108
+ * @param ref - Ref to the HTML input or textarea element.
109
+ */
110
+ export function useTextField<T extends TextFieldIntrinsicElements = DefaultElementType>(
111
+ props: AriaTextFieldOptions<T>,
112
+ ref: TextFieldRefObject<T>
113
+ ): TextFieldAria<T> {
114
+ let {
115
+ inputElementType = 'input',
116
+ isDisabled = false,
117
+ isRequired = false,
118
+ isReadOnly = false,
119
+ type = 'text',
120
+ validationBehavior = 'aria'
121
+ }: AriaTextFieldOptions<TextFieldIntrinsicElements> = props;
122
+ let [value, setValue] = useControlledState<string>(props.value, props.defaultValue || '', props.onChange);
123
+ let {focusableProps} = useFocusable(props, ref);
124
+ let validationState = useFormValidationState({
125
+ ...props,
126
+ value
127
+ });
128
+ let {isInvalid, validationErrors, validationDetails} = validationState.displayValidation;
129
+ let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
130
+ ...props,
131
+ isInvalid,
132
+ errorMessage: props.errorMessage || validationErrors
133
+ });
134
+ let domProps = filterDOMProps(props, {labelable: true});
135
+
136
+ const inputOnlyProps = {
137
+ type,
138
+ pattern: props.pattern
139
+ };
140
+
141
+ useFormReset(ref, value, setValue);
142
+ useFormValidation(props, validationState, ref);
143
+
144
+ useEffect(() => {
145
+ // This works around a React/Chrome bug that prevents textarea elements from validating when controlled.
146
+ // We prevent React from updating defaultValue (i.e. children) of textarea when `value` changes,
147
+ // which causes Chrome to skip validation. Only updating `value` is ok in our case since our
148
+ // textareas are always controlled. React is planning on removing this synchronization in a
149
+ // future major version.
150
+ // https://github.com/facebook/react/issues/19474
151
+ // https://github.com/facebook/react/issues/11896
152
+ if (ref.current instanceof getOwnerWindow(ref.current).HTMLTextAreaElement) {
153
+ let input = ref.current;
154
+ Object.defineProperty(input, 'defaultValue', {
155
+ get: () => input.value,
156
+ set: () => {},
157
+ configurable: true
158
+ });
159
+ }
160
+ }, [ref]);
161
+
162
+ return {
163
+ labelProps,
164
+ inputProps: mergeProps(
165
+ domProps,
166
+ inputElementType === 'input' ? inputOnlyProps : undefined,
167
+ {
168
+ disabled: isDisabled,
169
+ readOnly: isReadOnly,
170
+ required: isRequired && validationBehavior === 'native',
171
+ 'aria-required': (isRequired && validationBehavior === 'aria') || undefined,
172
+ 'aria-invalid': isInvalid || undefined,
173
+ 'aria-errormessage': props['aria-errormessage'],
174
+ 'aria-activedescendant': props['aria-activedescendant'],
175
+ 'aria-autocomplete': props['aria-autocomplete'],
176
+ 'aria-haspopup': props['aria-haspopup'],
177
+ value,
178
+ onChange: (e: ChangeEvent<HTMLInputElement>) => setValue(e.target.value),
179
+ autoComplete: props.autoComplete,
180
+ autoCapitalize: props.autoCapitalize,
181
+ maxLength: props.maxLength,
182
+ minLength: props.minLength,
183
+ name: props.name,
184
+ placeholder: props.placeholder,
185
+ inputMode: props.inputMode,
186
+
187
+ // Clipboard events
188
+ onCopy: props.onCopy,
189
+ onCut: props.onCut,
190
+ onPaste: props.onPaste,
191
+
192
+ // Composition events
193
+ onCompositionEnd: props.onCompositionEnd,
194
+ onCompositionStart: props.onCompositionStart,
195
+ onCompositionUpdate: props.onCompositionUpdate,
196
+
197
+ // Selection events
198
+ onSelect: props.onSelect,
199
+
200
+ // Input events
201
+ onBeforeInput: props.onBeforeInput,
202
+ onInput: props.onInput,
203
+ ...focusableProps,
204
+ ...fieldProps
205
+ }
206
+ ),
207
+ descriptionProps,
208
+ errorMessageProps,
209
+ isInvalid,
210
+ validationErrors,
211
+ validationDetails
212
+ };
213
+ }