@khanacademy/wonder-blocks-form 4.9.2 → 4.9.3

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 (35) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/components/checkbox-core.d.ts +2 -2
  3. package/dist/components/checkbox.d.ts +2 -2
  4. package/dist/components/choice-internal.d.ts +2 -2
  5. package/dist/components/choice.d.ts +2 -2
  6. package/dist/components/radio-core.d.ts +2 -2
  7. package/dist/components/radio.d.ts +2 -2
  8. package/dist/components/text-area.d.ts +2 -2
  9. package/package.json +7 -7
  10. package/src/__tests__/__snapshots__/custom-snapshot.test.tsx.snap +0 -247
  11. package/src/__tests__/custom-snapshot.test.tsx +0 -48
  12. package/src/components/__tests__/checkbox-group.test.tsx +0 -162
  13. package/src/components/__tests__/checkbox.test.tsx +0 -138
  14. package/src/components/__tests__/field-heading.test.tsx +0 -225
  15. package/src/components/__tests__/labeled-text-field.test.tsx +0 -750
  16. package/src/components/__tests__/radio-group.test.tsx +0 -182
  17. package/src/components/__tests__/text-area.test.tsx +0 -1286
  18. package/src/components/__tests__/text-field.test.tsx +0 -562
  19. package/src/components/checkbox-core.tsx +0 -239
  20. package/src/components/checkbox-group.tsx +0 -174
  21. package/src/components/checkbox.tsx +0 -99
  22. package/src/components/choice-internal.tsx +0 -184
  23. package/src/components/choice.tsx +0 -157
  24. package/src/components/field-heading.tsx +0 -169
  25. package/src/components/group-styles.ts +0 -33
  26. package/src/components/labeled-text-field.tsx +0 -317
  27. package/src/components/radio-core.tsx +0 -171
  28. package/src/components/radio-group.tsx +0 -159
  29. package/src/components/radio.tsx +0 -82
  30. package/src/components/text-area.tsx +0 -430
  31. package/src/components/text-field.tsx +0 -399
  32. package/src/index.ts +0 -17
  33. package/src/util/types.ts +0 -85
  34. package/tsconfig-build.json +0 -19
  35. package/tsconfig-build.tsbuildinfo +0 -1
@@ -1,399 +0,0 @@
1
- import * as React from "react";
2
- import {StyleSheet} from "aphrodite";
3
-
4
- import {IDProvider, addStyle} from "@khanacademy/wonder-blocks-core";
5
- import {color, spacing} from "@khanacademy/wonder-blocks-tokens";
6
- import {styles as typographyStyles} from "@khanacademy/wonder-blocks-typography";
7
-
8
- import type {StyleType, AriaProps} from "@khanacademy/wonder-blocks-core";
9
- import {OmitConstrained} from "../util/types";
10
-
11
- export type TextFieldType = "text" | "password" | "email" | "number" | "tel";
12
-
13
- type WithForwardRef = {
14
- forwardedRef: React.ForwardedRef<HTMLInputElement>;
15
- };
16
-
17
- const defaultErrorMessage = "This field is required.";
18
-
19
- const StyledInput = addStyle("input");
20
-
21
- type CommonProps = AriaProps & {
22
- /**
23
- * An optional unique identifier for the TextField.
24
- * If no id is specified, a unique id will be auto-generated.
25
- */
26
- id?: string;
27
- /**
28
- * The input value.
29
- */
30
- value: string;
31
- /**
32
- * The name for the input control. This is submitted along with
33
- * the form data.
34
- */
35
- name?: string;
36
- /**
37
- * Makes a read-only input field that cannot be focused. Defaults to false.
38
- */
39
- disabled: boolean;
40
- /**
41
- * Provide a validation for the input value.
42
- * Return a string error message or null | void for a valid input.
43
- */
44
- validate?: (value: string) => string | null | void;
45
- /**
46
- * Called right after the TextField input is validated.
47
- */
48
- onValidate?: (errorMessage?: string | null | undefined) => unknown;
49
- /**
50
- * Called when the value has changed.
51
- */
52
- onChange: (newValue: string) => unknown;
53
- /**
54
- * Called when a key is pressed.
55
- */
56
- onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
57
- /**
58
- * Called when the element has been focused.
59
- */
60
- onFocus?: (event: React.FocusEvent<HTMLInputElement>) => unknown;
61
- /**
62
- * Called when the element has been blurred.
63
- */
64
- onBlur?: (event: React.FocusEvent<HTMLInputElement>) => unknown;
65
- /**
66
- * Provide hints or examples of what to enter.
67
- */
68
- placeholder?: string;
69
- /**
70
- * Whether this field is required to to continue, or the error message to
71
- * render if this field is left blank.
72
- *
73
- * This can be a boolean or a string.
74
- *
75
- * String:
76
- * Please pass in a translated string to use as the error message that will
77
- * render if the user leaves this field blank. If this field is required,
78
- * and a string is not passed in, a default untranslated string will render
79
- * upon error.
80
- * Note: The string will not be used if a `validate` prop is passed in.
81
- *
82
- * Example message: i18n._("A password is required to log in.")
83
- *
84
- * Boolean:
85
- * True/false indicating whether this field is required. Please do not pass
86
- * in `true` if possible - pass in the error string instead.
87
- * If `true` is passed, and a `validate` prop is not passed, that means
88
- * there is no corresponding message and the default untranlsated message
89
- * will be used.
90
- */
91
- required?: boolean | string;
92
- /**
93
- * Change the default focus ring color to fit a dark background.
94
- */
95
- light: boolean;
96
- /**
97
- * Custom styles for the input.
98
- */
99
- style?: StyleType;
100
- /**
101
- * Optional test ID for e2e testing.
102
- */
103
- testId?: string;
104
- /**
105
- * Specifies if the input field is read-only.
106
- */
107
- readOnly?: boolean;
108
- /**
109
- * Whether this field should autofocus on page load.
110
- */
111
- autoFocus?: boolean;
112
- /**
113
- * Specifies if the input field allows autocomplete.
114
- */
115
- autoComplete?: string;
116
- };
117
-
118
- type OtherInputProps = CommonProps & {
119
- type: "text" | "password" | "email" | "tel";
120
- };
121
-
122
- // Props that are only available for inputs of type "number".
123
- export type NumericInputProps = {
124
- type: "number";
125
- /**
126
- * The minimum numeric value for the input.
127
- */
128
- min?: number;
129
- /**
130
- * The maximum numeric value for the input.
131
- */
132
- max?: number;
133
- /**
134
- * The numeric value to increment or decrement by.
135
- * Requires the input to be multiples of this value.
136
- */
137
- step?: number;
138
- };
139
-
140
- type FullNumericInputProps = CommonProps & NumericInputProps;
141
- type Props = OtherInputProps | FullNumericInputProps;
142
- type PropsWithForwardRef = Props & WithForwardRef;
143
-
144
- type DefaultProps = {
145
- type: PropsWithForwardRef["type"];
146
- disabled: PropsWithForwardRef["disabled"];
147
- light: PropsWithForwardRef["light"];
148
- };
149
-
150
- type State = {
151
- /**
152
- * Displayed when the validation fails.
153
- */
154
- error: string | null | undefined;
155
- /**
156
- * The user focuses on this field.
157
- */
158
- focused: boolean;
159
- };
160
-
161
- /**
162
- * A TextField is an element used to accept a single line of text from the user.
163
- */
164
- class TextField extends React.Component<PropsWithForwardRef, State> {
165
- static defaultProps: DefaultProps = {
166
- type: "text",
167
- disabled: false,
168
- light: false,
169
- };
170
-
171
- constructor(props: PropsWithForwardRef) {
172
- super(props);
173
- if (props.validate && props.value !== "") {
174
- // Ensures error is updated on unmounted server-side renders
175
- this.state.error = props.validate(props.value) || null;
176
- }
177
- }
178
-
179
- state: State = {
180
- error: null,
181
- focused: false,
182
- };
183
-
184
- componentDidMount() {
185
- if (this.props.value !== "") {
186
- this.maybeValidate(this.props.value);
187
- }
188
- }
189
-
190
- maybeValidate: (newValue: string) => void = (newValue) => {
191
- const {validate, onValidate, required} = this.props;
192
-
193
- if (validate) {
194
- const maybeError = validate(newValue) || null;
195
- this.setState({error: maybeError}, () => {
196
- if (onValidate) {
197
- onValidate(maybeError);
198
- }
199
- });
200
- } else if (required) {
201
- const requiredString =
202
- typeof required === "string" ? required : defaultErrorMessage;
203
- const maybeError = newValue ? null : requiredString;
204
- this.setState({error: maybeError}, () => {
205
- if (onValidate) {
206
- onValidate(maybeError);
207
- }
208
- });
209
- }
210
- };
211
-
212
- handleChange: (event: React.ChangeEvent<HTMLInputElement>) => unknown = (
213
- event,
214
- ) => {
215
- const {onChange} = this.props;
216
- const newValue = event.target.value;
217
- this.maybeValidate(newValue);
218
- onChange(newValue);
219
- };
220
-
221
- handleFocus: (event: React.FocusEvent<HTMLInputElement>) => unknown = (
222
- event,
223
- ) => {
224
- const {onFocus} = this.props;
225
- this.setState({focused: true}, () => {
226
- if (onFocus) {
227
- onFocus(event);
228
- }
229
- });
230
- };
231
-
232
- handleBlur: (event: React.FocusEvent<HTMLInputElement>) => unknown = (
233
- event,
234
- ) => {
235
- const {onBlur} = this.props;
236
- this.setState({focused: false}, () => {
237
- if (onBlur) {
238
- onBlur(event);
239
- }
240
- });
241
- };
242
-
243
- render(): React.ReactNode {
244
- const {
245
- id,
246
- type,
247
- value,
248
- name,
249
- disabled,
250
- onKeyDown,
251
- placeholder,
252
- light,
253
- style,
254
- testId,
255
- readOnly,
256
- autoFocus,
257
- autoComplete,
258
- forwardedRef,
259
- // The following props are being included here to avoid
260
- // passing them down to the otherProps spread
261
- /* eslint-disable @typescript-eslint/no-unused-vars */
262
- onFocus,
263
- onBlur,
264
- onValidate,
265
- validate,
266
- onChange,
267
- required,
268
- /* eslint-enable @typescript-eslint/no-unused-vars */
269
- // Should only include Aria related props
270
- ...otherProps
271
- } = this.props;
272
-
273
- return (
274
- <IDProvider id={id} scope="text-field">
275
- {(uniqueId) => (
276
- <StyledInput
277
- style={[
278
- styles.input,
279
- typographyStyles.LabelMedium,
280
- styles.default,
281
- // Prioritizes disabled, then focused, then error (if any)
282
- disabled
283
- ? styles.disabled
284
- : this.state.focused
285
- ? [styles.focused, light && styles.defaultLight]
286
- : !!this.state.error && [
287
- styles.error,
288
- light && styles.errorLight,
289
- ],
290
- // Cast `this.state.error` into boolean since it's being
291
- // used as a conditional
292
- !!this.state.error && styles.error,
293
- style && style,
294
- ]}
295
- id={uniqueId}
296
- type={type}
297
- placeholder={placeholder}
298
- value={value}
299
- name={name}
300
- disabled={disabled}
301
- onChange={this.handleChange}
302
- onKeyDown={onKeyDown}
303
- onFocus={this.handleFocus}
304
- onBlur={this.handleBlur}
305
- data-testid={testId}
306
- readOnly={readOnly}
307
- autoFocus={autoFocus}
308
- autoComplete={autoComplete}
309
- ref={forwardedRef}
310
- {...otherProps}
311
- aria-invalid={this.state.error ? "true" : "false"}
312
- />
313
- )}
314
- </IDProvider>
315
- );
316
- }
317
- }
318
-
319
- const styles = StyleSheet.create({
320
- input: {
321
- width: "100%",
322
- height: 40,
323
- borderRadius: 4,
324
- boxSizing: "border-box",
325
- paddingLeft: spacing.medium_16,
326
- margin: 0,
327
- outline: "none",
328
- boxShadow: "none",
329
- },
330
- default: {
331
- background: color.white,
332
- border: `1px solid ${color.offBlack50}`,
333
- color: color.offBlack,
334
- "::placeholder": {
335
- color: color.offBlack64,
336
- },
337
- },
338
- error: {
339
- background: color.fadedRed8,
340
- border: `1px solid ${color.red}`,
341
- color: color.offBlack,
342
- "::placeholder": {
343
- color: color.offBlack64,
344
- },
345
- },
346
- disabled: {
347
- background: color.offWhite,
348
- border: `1px solid ${color.offBlack16}`,
349
- color: color.offBlack64,
350
- "::placeholder": {
351
- color: color.offBlack32,
352
- },
353
- },
354
- focused: {
355
- background: color.white,
356
- border: `1px solid ${color.blue}`,
357
- color: color.offBlack,
358
- "::placeholder": {
359
- color: color.offBlack64,
360
- },
361
- },
362
- defaultLight: {
363
- boxShadow: `0px 0px 0px 1px ${color.blue}, 0px 0px 0px 2px ${color.white}`,
364
- },
365
- errorLight: {
366
- boxShadow: `0px 0px 0px 1px ${color.red}, 0px 0px 0px 2px ${color.white}`,
367
- },
368
- });
369
-
370
- type ExportProps = OmitConstrained<
371
- JSX.LibraryManagedAttributes<
372
- typeof TextField,
373
- React.ComponentProps<typeof TextField>
374
- >,
375
- "forwardedRef"
376
- >;
377
-
378
- /**
379
- * A TextField is an element used to accept a single line of text from the user.
380
- *
381
- * ### Usage
382
- *
383
- * ```jsx
384
- * import {TextField} from "@khanacademy/wonder-blocks-form";
385
- *
386
- * const [value, setValue] = React.useState("");
387
- *
388
- * <TextField
389
- * id="some-unique-text-field-id"
390
- * value={value}
391
- * onChange={setValue}
392
- * />
393
- * ```
394
- */
395
- export default React.forwardRef<HTMLInputElement, ExportProps>((props, ref) => (
396
- <TextField {...props} forwardedRef={ref} />
397
- )) as React.ForwardRefExoticComponent<
398
- ExportProps & React.RefAttributes<HTMLInputElement>
399
- >;
package/src/index.ts DELETED
@@ -1,17 +0,0 @@
1
- import Checkbox from "./components/checkbox";
2
- import Choice from "./components/choice";
3
- import CheckboxGroup from "./components/checkbox-group";
4
- import RadioGroup from "./components/radio-group";
5
- import TextField from "./components/text-field";
6
- import LabeledTextField from "./components/labeled-text-field";
7
- import TextArea from "./components/text-area";
8
-
9
- export {
10
- Checkbox,
11
- Choice,
12
- CheckboxGroup,
13
- RadioGroup,
14
- TextField,
15
- LabeledTextField,
16
- TextArea,
17
- };
package/src/util/types.ts DELETED
@@ -1,85 +0,0 @@
1
- // NOTE(sophie): Unfortunately, styleguidist does not pull prop definitions
2
- // from imported types. We've duplicated the shared props for each component
3
- // they apply to, so that the prop definitions will show up on the generated
4
- // guide.
5
- import type {AriaProps, StyleType} from "@khanacademy/wonder-blocks-core";
6
-
7
- import Choice from "../components/choice";
8
-
9
- // Checkbox is in indeterminate state when `checked` is `null` | `undefined`
10
- export type Checked = boolean | null | undefined;
11
-
12
- // Shared props for radio-core and checkbox-core
13
- export type ChoiceCoreProps = AriaProps & {
14
- /** Whether this component is checked */
15
- checked: Checked;
16
- /** Whether this component is disabled */
17
- disabled: boolean;
18
- /** Whether this component should show an error state */
19
- error: boolean;
20
- /** Name for the checkbox or radio button group */
21
- groupName?: string;
22
- /** Unique identifier attached to the HTML input element. If used, need to
23
- * guarantee that the ID is unique within everything rendered on a page.
24
- * Used to match <label> with <input> elements for screenreaders. */
25
- id?: string;
26
- /** Optional test ID for e2e testing */
27
- testId?: string;
28
- /** Function that executes when the choice is clicked. */
29
- onClick: () => void;
30
- };
31
-
32
- // Props for checkbox and radio button
33
- export type ChoiceComponentProps = ChoiceCoreProps & {
34
- /** Callback when this component is selected. The newCheckedState is the
35
- * new checked state of the component. */
36
- onChange: (newCheckedState: boolean) => unknown;
37
- /** Optional label for the field. */
38
- label?: string;
39
- /** Optional description for the field. */
40
- description?: string;
41
- /** Ignored because only applicable to Choice components in a group. */
42
- value?: string;
43
- /** Optional styling for the container. Does not style the component. */
44
- style?: StyleType;
45
- };
46
-
47
- export type SharedGroupProps = {
48
- /** Children should be Choice components. */
49
- children: typeof Choice;
50
- /** Group name for this checkbox or radio group. Should be unique for all
51
- * such groups displayed on a page. */
52
- groupName: string;
53
- /** Optional label for the group. This label is optional to allow for
54
- * greater flexibility in implementing checkbox and radio groups. */
55
- label?: string;
56
- /** Optional description for the group. */
57
- description?: string;
58
- /** Optional error message. If supplied, the group will be displayed in an
59
- * error state, along with this error message. If no error state is desired,
60
- * simply do not supply this prop, or pass along null. */
61
- errorMessage?: string;
62
- /** Custom styling for this group of checkboxes. */
63
- style?: StyleType;
64
- };
65
-
66
- export type CheckboxGroupProps = {
67
- /** Callback for when selection of the group has changed. Passes the newly
68
- * selected values. */
69
- onChange: (selectedValues: Array<string>) => unknown;
70
- /** An array of the values of the selected values in this checkbox group. */
71
- selectedValues: Array<string>;
72
- };
73
-
74
- export type RadioGroupProps = {
75
- /** Callback for when the selected value of the radio group has changed. */
76
- onChange: (selectedValue: string) => unknown;
77
- /** Value of the selected radio item. */
78
- selectedValue: string;
79
- };
80
-
81
- // For more information, see:
82
- // https://github.com/microsoft/TypeScript/wiki/FAQ#add-a-key-constraint-to-omit
83
- export type OmitConstrained<T, K> = {
84
- [P in keyof T as Exclude<P, K & keyof any>]: T[P];
85
- };
@@ -1,19 +0,0 @@
1
- {
2
- "exclude": ["dist"],
3
- "extends": "../tsconfig-shared.json",
4
- "compilerOptions": {
5
- "outDir": "./dist",
6
- "rootDir": "src",
7
- },
8
- "references": [
9
- {"path": "../wonder-blocks-button/tsconfig-build.json"},
10
- {"path": "../wonder-blocks-clickable/tsconfig-build.json"},
11
- {"path": "../wonder-blocks-core/tsconfig-build.json"},
12
- {"path": "../wonder-blocks-i18n/tsconfig-build.json"},
13
- {"path": "../wonder-blocks-icon/tsconfig-build.json"},
14
- {"path": "../wonder-blocks-layout/tsconfig-build.json"},
15
- {"path": "../wonder-blocks-link/tsconfig-build.json"},
16
- {"path": "../wonder-blocks-tokens/tsconfig-build.json"},
17
- {"path": "../wonder-blocks-typography/tsconfig-build.json"},
18
- ]
19
- }