@seed-design/react-field 1.0.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.
@@ -0,0 +1,183 @@
1
+ import { ariaAttr, dataAttr, elementProps, inputProps, labelProps } from "@seed-design/dom-utils";
2
+ import { useCallback, useId, useState } from "react";
3
+ import { getDescriptionId, getErrorMessageId, getInputId, getLabelId } from "./dom";
4
+
5
+ function useFieldState() {
6
+ const [isHovered, setIsHovered] = useState(false);
7
+ const [isActive, setIsActive] = useState(false);
8
+ const [isFocused, setIsFocused] = useState(false);
9
+ const [isFocusVisible, setIsFocusVisible] = useState(false);
10
+
11
+ const [isLabelRendered, setIsLabelRendered] = useState(false);
12
+ const labelRef = useCallback((node: HTMLLabelElement | null) => {
13
+ setIsLabelRendered(!!node);
14
+ }, []);
15
+ const [isDescriptionRendered, setIsDescriptionRendered] = useState(false);
16
+ const descriptionRef = useCallback((node: HTMLElement | null) => {
17
+ setIsDescriptionRendered(!!node);
18
+ }, []);
19
+ const [isErrorMessageRendered, setIsErrorMessageRendered] = useState(false);
20
+ const errorMessageRef = useCallback((node: HTMLElement | null) => {
21
+ setIsErrorMessageRendered(!!node);
22
+ }, []);
23
+
24
+ return {
25
+ refs: {
26
+ label: labelRef,
27
+ description: descriptionRef,
28
+ errorMessage: errorMessageRef,
29
+ },
30
+
31
+ isHovered,
32
+ isActive,
33
+ isFocused,
34
+ isFocusVisible,
35
+ renderedElements: {
36
+ label: isLabelRendered,
37
+ description: isDescriptionRendered,
38
+ errorMessage: isErrorMessageRendered,
39
+ },
40
+
41
+ setIsHovered,
42
+ setIsActive,
43
+ setIsFocused,
44
+ setIsFocusVisible,
45
+ };
46
+ }
47
+
48
+ export interface UseFieldProps {
49
+ /**
50
+ * @default false
51
+ */
52
+ required?: boolean;
53
+ /**
54
+ * @default false
55
+ */
56
+ disabled?: boolean;
57
+ /**
58
+ * @default false
59
+ */
60
+ readOnly?: boolean;
61
+ /**
62
+ * @default false
63
+ */
64
+ invalid?: boolean;
65
+
66
+ name?: string;
67
+ }
68
+
69
+ export type UseFieldReturn = ReturnType<typeof useField>;
70
+
71
+ export function useField(props: UseFieldProps) {
72
+ const id = useId();
73
+ const { disabled = false, invalid = false, readOnly = false, required = false } = props;
74
+
75
+ const {
76
+ refs,
77
+ renderedElements,
78
+ isHovered,
79
+ isActive,
80
+ isFocused,
81
+ isFocusVisible,
82
+ setIsHovered,
83
+ setIsActive,
84
+ setIsFocused,
85
+ setIsFocusVisible,
86
+ } = useFieldState();
87
+
88
+ const ariaDescribedBy =
89
+ [
90
+ renderedElements.description ? getDescriptionId(id) : false,
91
+ renderedElements.errorMessage ? getErrorMessageId(id) : false,
92
+ ]
93
+ .filter(Boolean)
94
+ .join(" ") || undefined;
95
+
96
+ const stateProps = elementProps({
97
+ "data-hover": dataAttr(isHovered),
98
+ "data-active": dataAttr(isActive),
99
+ "data-focus": dataAttr(isFocused),
100
+ "data-focus-visible": dataAttr(isFocusVisible),
101
+ "data-readonly": dataAttr(readOnly),
102
+ "data-disabled": dataAttr(disabled),
103
+ "data-invalid": dataAttr(invalid),
104
+ });
105
+
106
+ return {
107
+ refs,
108
+
109
+ active: isActive,
110
+ focused: isFocused,
111
+ invalid,
112
+ required,
113
+
114
+ setIsFocused,
115
+ setIsFocusVisible,
116
+
117
+ stateProps,
118
+
119
+ rootProps: elementProps({
120
+ ...stateProps,
121
+
122
+ onPointerMove() {
123
+ setIsHovered(true);
124
+ },
125
+ onPointerDown() {
126
+ setIsActive(true);
127
+ },
128
+ onPointerUp() {
129
+ setIsActive(false);
130
+ },
131
+ onPointerLeave() {
132
+ setIsHovered(false);
133
+ setIsActive(false);
134
+ },
135
+ }),
136
+
137
+ labelProps: labelProps({
138
+ ...stateProps,
139
+ id: getLabelId(id),
140
+ htmlFor: getInputId(id),
141
+ }),
142
+
143
+ inputProps: inputProps({
144
+ disabled,
145
+ readOnly,
146
+ name: props.name || id,
147
+ id: getInputId(id),
148
+ }),
149
+
150
+ inputAriaAttributes: elementProps({
151
+ ...(renderedElements.label && { "aria-labelledby": getLabelId(id) }),
152
+ "aria-describedby": ariaDescribedBy,
153
+ "aria-required": ariaAttr(required),
154
+ "aria-invalid": ariaAttr(invalid),
155
+ "aria-readonly": ariaAttr(readOnly),
156
+ "aria-disabled": ariaAttr(disabled),
157
+ }),
158
+ inputHandlers: inputProps({
159
+ onChange: (event) => {
160
+ setIsFocusVisible(event.target.matches(":focus-visible"));
161
+ },
162
+ onBlur() {
163
+ setIsFocused(false);
164
+ setIsFocusVisible(false);
165
+ },
166
+ onFocus(event) {
167
+ setIsFocused(true);
168
+ setIsFocusVisible(event.target.matches(":focus-visible"));
169
+ },
170
+ }),
171
+
172
+ descriptionProps: elementProps({
173
+ ...stateProps,
174
+ id: getDescriptionId(id),
175
+ }),
176
+
177
+ errorMessageProps: elementProps({
178
+ ...stateProps,
179
+ id: getErrorMessageId(id),
180
+ "aria-live": "polite",
181
+ }),
182
+ };
183
+ }
@@ -0,0 +1,21 @@
1
+ import { createContext, useContext } from "react";
2
+ import type { UseFieldReturn } from "./useField";
3
+
4
+ export interface UseFieldContext extends UseFieldReturn {}
5
+
6
+ const FieldContext = createContext<UseFieldContext | null>(null);
7
+
8
+ export const FieldProvider = FieldContext.Provider;
9
+
10
+ export function useFieldContext<T extends boolean | undefined = true>({
11
+ strict = true,
12
+ }: {
13
+ strict?: T;
14
+ } = {}): T extends false ? UseFieldContext | null : UseFieldContext {
15
+ const context = useContext(FieldContext);
16
+ if (!context && strict) {
17
+ throw new Error("useFieldContext must be used within a Field");
18
+ }
19
+
20
+ return context as UseFieldContext;
21
+ }