@react-aria/textfield 3.3.1 → 3.5.2

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.
@@ -11,27 +11,88 @@
11
11
  */
12
12
 
13
13
  import {AriaTextFieldProps} from '@react-types/textfield';
14
- import {ChangeEvent, InputHTMLAttributes, LabelHTMLAttributes, RefObject, TextareaHTMLAttributes} from 'react';
15
- import {ElementType} from 'react';
14
+ import {
15
+ ChangeEvent,
16
+ DOMFactory,
17
+ HTMLAttributes,
18
+ LabelHTMLAttributes,
19
+ ReactDOM,
20
+ RefObject
21
+ } from 'react';
16
22
  import {filterDOMProps, mergeProps} from '@react-aria/utils';
23
+ import {useField} from '@react-aria/label';
17
24
  import {useFocusable} from '@react-aria/focus';
18
- import {useLabel} from '@react-aria/label';
19
25
 
20
- export interface TextFieldAria {
21
- /** Props for the input element. */
22
- inputProps: InputHTMLAttributes<HTMLInputElement> | TextareaHTMLAttributes<HTMLTextAreaElement>,
23
- /** Props for the text field's visible label element (if any). */
24
- labelProps: LabelHTMLAttributes<HTMLLabelElement>
25
- }
26
+ /**
27
+ * A map of HTML element names and their interface types.
28
+ * For example `'a'` -> `HTMLAnchorElement`.
29
+ */
30
+ type IntrinsicHTMLElements = {
31
+ [K in keyof IntrinsicHTMLAttributes]: IntrinsicHTMLAttributes[K] extends HTMLAttributes<infer T> ? T : never
32
+ };
33
+
34
+ /**
35
+ * A map of HTML element names and their attribute interface types.
36
+ * For example `'a'` -> `AnchorHTMLAttributes<HTMLAnchorElement>`.
37
+ */
38
+ type IntrinsicHTMLAttributes = {
39
+ [K in keyof ReactDOM]: ReactDOM[K] extends DOMFactory<infer T, any> ? T : never
40
+ };
41
+
42
+ type DefaultElementType = 'input';
43
+
44
+ /**
45
+ * The intrinsic HTML element names that `useTextField` supports; e.g. `input`,
46
+ * `textarea`.
47
+ */
48
+ type TextFieldIntrinsicElements = keyof Pick<IntrinsicHTMLElements, 'input' | 'textarea'>;
26
49
 
27
- interface AriaTextFieldOptions extends AriaTextFieldProps {
50
+ /**
51
+ * The HTML element interfaces that `useTextField` supports based on what is
52
+ * defined for `TextFieldIntrinsicElements`; e.g. `HTMLInputElement`,
53
+ * `HTMLTextAreaElement`.
54
+ */
55
+ type TextFieldHTMLElementType = Pick<IntrinsicHTMLElements, TextFieldIntrinsicElements>;
56
+
57
+ /**
58
+ * The HTML attributes interfaces that `useTextField` supports based on what
59
+ * is defined for `TextFieldIntrinsicElements`; e.g. `InputHTMLAttributes`,
60
+ * `TextareaHTMLAttributes`.
61
+ */
62
+ type TextFieldHTMLAttributesType = Pick<IntrinsicHTMLAttributes, TextFieldIntrinsicElements>;
63
+
64
+ /**
65
+ * The type of `inputProps` returned by `useTextField`; e.g. `InputHTMLAttributes`,
66
+ * `TextareaHTMLAttributes`.
67
+ */
68
+ type TextFieldInputProps<T extends TextFieldIntrinsicElements> = TextFieldHTMLAttributesType[T];
69
+
70
+ interface AriaTextFieldOptions<T extends TextFieldIntrinsicElements> extends AriaTextFieldProps {
28
71
  /**
29
72
  * The HTML element used to render the input, e.g. 'input', or 'textarea'.
30
73
  * It determines whether certain HTML attributes will be included in `inputProps`.
31
74
  * For example, [`type`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-type).
32
75
  * @default 'input'
33
76
  */
34
- inputElementType?: ElementType
77
+ inputElementType?: T
78
+ }
79
+
80
+ /**
81
+ * The type of `ref` object that can be passed to `useTextField` based on the given
82
+ * intrinsic HTML element name; e.g.`RefObject<HTMLInputElement>`,
83
+ * `RefObject<HTMLTextAreaElement>`.
84
+ */
85
+ type TextFieldRefObject<T extends TextFieldIntrinsicElements> = RefObject<TextFieldHTMLElementType[T]>;
86
+
87
+ export interface TextFieldAria<T extends TextFieldIntrinsicElements = DefaultElementType> {
88
+ /** Props for the input element. */
89
+ inputProps: TextFieldInputProps<T>,
90
+ /** Props for the text field's visible label element, if any. */
91
+ labelProps: LabelHTMLAttributes<HTMLLabelElement>,
92
+ /** Props for the text field's description element, if any. */
93
+ descriptionProps: HTMLAttributes<HTMLElement>,
94
+ /** Props for the text field's error message element, if any. */
95
+ errorMessageProps: HTMLAttributes<HTMLElement>
35
96
  }
36
97
 
37
98
  /**
@@ -39,10 +100,10 @@ interface AriaTextFieldOptions extends AriaTextFieldProps {
39
100
  * @param props - Props for the text field.
40
101
  * @param ref - Ref to the HTML input or textarea element.
41
102
  */
42
- export function useTextField(
43
- props: AriaTextFieldOptions,
44
- ref: RefObject<HTMLInputElement | HTMLTextAreaElement>
45
- ): TextFieldAria {
103
+ export function useTextField<T extends TextFieldIntrinsicElements = DefaultElementType>(
104
+ props: AriaTextFieldOptions<T>,
105
+ ref: TextFieldRefObject<T>
106
+ ): TextFieldAria<T> {
46
107
  let {
47
108
  inputElementType = 'input',
48
109
  isDisabled = false,
@@ -51,9 +112,9 @@ export function useTextField(
51
112
  validationState,
52
113
  type = 'text',
53
114
  onChange = () => {}
54
- } = props;
115
+ }: AriaTextFieldOptions<TextFieldIntrinsicElements> = props;
55
116
  let {focusableProps} = useFocusable(props, ref);
56
- let {labelProps, fieldProps} = useLabel(props);
117
+ let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField(props);
57
118
  let domProps = filterDOMProps(props, {labelable: true});
58
119
 
59
120
  const inputOnlyProps = {
@@ -104,6 +165,8 @@ export function useTextField(
104
165
  ...focusableProps,
105
166
  ...fieldProps
106
167
  }
107
- )
168
+ ),
169
+ descriptionProps,
170
+ errorMessageProps
108
171
  };
109
172
  }