@purpurds/text-field 3.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,184 @@
1
+ import React, {
2
+ ComponentPropsWithoutRef,
3
+ ForwardedRef,
4
+ forwardRef,
5
+ HTMLInputTypeAttribute,
6
+ ReactNode,
7
+ } from "react";
8
+ import { FieldErrorText } from "@purpurds/field-error-text";
9
+ import { FieldHelperText } from "@purpurds/field-helper-text";
10
+ import { checkCircleFilled, Icon } from "@purpurds/icon";
11
+ import { Label } from "@purpurds/label";
12
+ import { Spinner } from "@purpurds/spinner";
13
+ import c from "classnames";
14
+
15
+ import styles from "./text-field.module.scss";
16
+
17
+ export type TextFieldProps = ComponentPropsWithoutRef<"input"> & {
18
+ id: string;
19
+ ["data-testid"]?: string;
20
+ className?: string;
21
+ /**
22
+ * Use to display e.g. a button after the text field.
23
+ *
24
+ * _NOTE: Should ideally only be used by other purpur components!_
25
+ */
26
+ afterField?: ReactNode;
27
+ /**
28
+ * Use to display e.g. an icon at the end inside of the text field.
29
+ *
30
+ * _NOTE: Should ideally only be used by other purpur components!_
31
+ */
32
+ endAdornment?: ReactNode;
33
+ /**
34
+ * Use to render error message below the text field. The text field renders with error appearance.
35
+ * */
36
+ errorText?: string;
37
+ /**
38
+ * Use to give context about the field's input. Renders below the field.
39
+ * */
40
+ helperText?: string;
41
+ /**
42
+ * The label of the text field.
43
+ * */
44
+ label?: string;
45
+ /**
46
+ * Use to render a spinner at the end inside of the text field.
47
+ */
48
+ loading?: boolean;
49
+ /**
50
+ * Use to display e.g. an icon at the start inside of the text field.
51
+ *
52
+ * _NOTE: Should ideally only be used by other purpur components!_
53
+ */
54
+ startAdornment?: ReactNode;
55
+ /**
56
+ * Use to set the type of the field.
57
+ */
58
+ type?: Extract<
59
+ HTMLInputTypeAttribute,
60
+ "email" | "number" | "password" | "search" | "tel" | "text"
61
+ >;
62
+ /**
63
+ * Use to render text field with valid appearance. A check
64
+ * icon will render at the start inside of the input.
65
+ */
66
+ valid?: boolean;
67
+ };
68
+
69
+ const rootClassName = "purpur-text-field";
70
+
71
+ const TextFieldComponent = (
72
+ {
73
+ ["data-testid"]: dataTestId,
74
+ className,
75
+ afterField,
76
+ endAdornment,
77
+ errorText,
78
+ helperText,
79
+ label,
80
+ loading = false,
81
+ startAdornment,
82
+ valid = false,
83
+ ...inputProps
84
+ }: TextFieldProps,
85
+ ref: ForwardedRef<HTMLInputElement>
86
+ ) => {
87
+ const getTestId = (name: string) => (dataTestId ? `${dataTestId}-${name}` : undefined);
88
+ const isValid = valid && !errorText;
89
+ const helperTextId = helperText ? `${inputProps.id}-helper-text` : undefined;
90
+
91
+ const startAdornments: ReactNode[] = [startAdornment].filter((adornment) => !!adornment);
92
+
93
+ const endAdornments: ReactNode[] = [
94
+ loading && (
95
+ <Spinner
96
+ variant={inputProps.disabled ? "disabled" : "primary"}
97
+ size="xs"
98
+ data-testid={getTestId("spinner")}
99
+ />
100
+ ),
101
+ isValid && (
102
+ <Icon
103
+ data-testid={getTestId("valid-icon")}
104
+ className={styles[`${rootClassName}__valid-icon`]}
105
+ svg={checkCircleFilled}
106
+ size="md"
107
+ />
108
+ ),
109
+ endAdornment,
110
+ ].filter((adornment) => !!adornment);
111
+
112
+ const inputContainerClassnames = c([
113
+ styles[`${rootClassName}__input-container`],
114
+ {
115
+ [styles[`${rootClassName}__input-container--start-adornment`]]: startAdornments.length,
116
+ [styles[`${rootClassName}__input-container--end-adornment`]]: endAdornments.length,
117
+ [styles[`${rootClassName}__input-container--disabled`]]: inputProps.disabled,
118
+ [styles[`${rootClassName}__input-container--readonly`]]:
119
+ inputProps.readOnly && !inputProps.disabled,
120
+ },
121
+ ]);
122
+
123
+ return (
124
+ <div className={c(className, styles[rootClassName])}>
125
+ {label && (
126
+ <Label
127
+ htmlFor={inputProps.id}
128
+ className={styles[`${rootClassName}__label`]}
129
+ data-testid={getTestId("label")}
130
+ disabled={inputProps.disabled}
131
+ >
132
+ {`${inputProps.required ? "* " : ""}${label}`}
133
+ </Label>
134
+ )}
135
+ <div className={styles[`${rootClassName}__field-row`]}>
136
+ <div className={inputContainerClassnames}>
137
+ {!!startAdornments.length && (
138
+ <div
139
+ data-testid={getTestId("start-adornments")}
140
+ className={styles[`${rootClassName}__adornment-container`]}
141
+ >
142
+ {startAdornments}
143
+ </div>
144
+ )}
145
+ <input
146
+ {...inputProps}
147
+ ref={ref}
148
+ data-testid={getTestId("input")}
149
+ aria-describedby={inputProps["aria-describedby"] || helperTextId}
150
+ aria-invalid={inputProps["aria-invalid"] || !!errorText}
151
+ className={c([
152
+ styles[`${rootClassName}__input`],
153
+ {
154
+ [styles[`${rootClassName}__input--valid`]]: isValid,
155
+ [styles[`${rootClassName}__input--error`]]: !!errorText,
156
+ },
157
+ ])}
158
+ />
159
+ <div className={styles[`${rootClassName}__frame`]} />
160
+ {!!endAdornments.length && (
161
+ <div
162
+ data-testid={getTestId("end-adornments")}
163
+ className={styles[`${rootClassName}__adornment-container`]}
164
+ >
165
+ {endAdornments}
166
+ </div>
167
+ )}
168
+ </div>
169
+ {!!afterField && afterField}
170
+ </div>
171
+ {helperTextId && (
172
+ <FieldHelperText data-testid={getTestId("helper-text")} id={helperTextId}>
173
+ {helperText}
174
+ </FieldHelperText>
175
+ )}
176
+ {errorText && (
177
+ <FieldErrorText data-testid={getTestId("error-text")}>{errorText}</FieldErrorText>
178
+ )}
179
+ </div>
180
+ );
181
+ };
182
+
183
+ export const TextField = forwardRef(TextFieldComponent);
184
+ TextField.displayName = "TextField";