@khanacademy/wonder-blocks-form 3.1.11 → 3.1.13

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 (66) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/components/checkbox-core.d.ts +16 -0
  3. package/dist/components/checkbox-core.js.flow +26 -0
  4. package/dist/components/checkbox-group.d.ts +84 -0
  5. package/dist/components/checkbox-group.js.flow +103 -0
  6. package/dist/components/checkbox.d.ts +83 -0
  7. package/dist/components/checkbox.js.flow +106 -0
  8. package/dist/components/choice-internal.d.ts +63 -0
  9. package/dist/components/choice-internal.js.flow +100 -0
  10. package/dist/components/choice.d.ts +127 -0
  11. package/dist/components/choice.js.flow +161 -0
  12. package/dist/components/field-heading.d.ts +50 -0
  13. package/dist/components/field-heading.js.flow +64 -0
  14. package/dist/components/group-styles.d.ts +3 -0
  15. package/dist/components/group-styles.js.flow +10 -0
  16. package/dist/components/labeled-text-field.d.ts +169 -0
  17. package/dist/components/labeled-text-field.js.flow +211 -0
  18. package/dist/components/radio-core.d.ts +15 -0
  19. package/dist/components/radio-core.js.flow +26 -0
  20. package/dist/components/radio-group.d.ts +85 -0
  21. package/dist/components/radio-group.js.flow +104 -0
  22. package/dist/components/radio.d.ts +68 -0
  23. package/dist/components/radio.js.flow +92 -0
  24. package/dist/components/text-field.d.ts +146 -0
  25. package/dist/components/text-field.js.flow +186 -0
  26. package/dist/es/index.js +258 -224
  27. package/dist/index.d.ts +7 -0
  28. package/dist/index.js +281 -249
  29. package/dist/index.js.flow +21 -2
  30. package/dist/util/types.d.ts +62 -0
  31. package/dist/util/types.js.flow +138 -0
  32. package/package.json +10 -10
  33. package/src/__tests__/{custom-snapshot.test.js → custom-snapshot.test.tsx} +8 -9
  34. package/src/components/__tests__/{checkbox-group.test.js → checkbox-group.test.tsx} +5 -5
  35. package/src/components/__tests__/{field-heading.test.js → field-heading.test.tsx} +0 -1
  36. package/src/components/__tests__/{labeled-text-field.test.js → labeled-text-field.test.tsx} +4 -5
  37. package/src/components/__tests__/{radio-group.test.js → radio-group.test.tsx} +8 -8
  38. package/src/components/__tests__/{text-field.test.js → text-field.test.tsx} +22 -18
  39. package/src/components/{checkbox-core.js → checkbox-core.tsx} +12 -15
  40. package/src/components/{checkbox-group.js → checkbox-group.tsx} +20 -23
  41. package/src/components/{checkbox.js → checkbox.tsx} +18 -32
  42. package/src/components/{choice-internal.js → choice-internal.tsx} +25 -39
  43. package/src/components/{choice.js → choice.tsx} +24 -37
  44. package/src/components/{field-heading.js → field-heading.tsx} +16 -23
  45. package/src/components/{group-styles.js → group-styles.ts} +0 -1
  46. package/src/components/{labeled-text-field.js → labeled-text-field.tsx} +54 -69
  47. package/src/components/{radio-core.js → radio-core.tsx} +13 -16
  48. package/src/components/{radio-group.js → radio-group.tsx} +20 -23
  49. package/src/components/{radio.js → radio.tsx} +18 -32
  50. package/src/components/{text-field.js → text-field.tsx} +53 -64
  51. package/src/{index.js → index.ts} +0 -1
  52. package/src/util/{types.js → types.ts} +32 -35
  53. package/tsconfig.json +19 -0
  54. package/tsconfig.tsbuildinfo +1 -0
  55. package/src/__docs__/_overview_.stories.mdx +0 -15
  56. package/src/components/__docs__/checkbox-accessibility.stories.mdx +0 -147
  57. package/src/components/__docs__/checkbox-group.stories.js +0 -300
  58. package/src/components/__docs__/checkbox.stories.js +0 -167
  59. package/src/components/__docs__/choice.stories.js +0 -86
  60. package/src/components/__docs__/labeled-text-field.argtypes.js +0 -248
  61. package/src/components/__docs__/labeled-text-field.stories.js +0 -709
  62. package/src/components/__docs__/radio-group.stories.js +0 -217
  63. package/src/components/__docs__/radio.stories.js +0 -161
  64. package/src/components/__docs__/text-field.argtypes.js +0 -206
  65. package/src/components/__docs__/text-field.stories.js +0 -780
  66. /package/src/__tests__/__snapshots__/{custom-snapshot.test.js.snap → custom-snapshot.test.tsx.snap} +0 -0
@@ -1,45 +1,40 @@
1
- // @flow
2
1
  import * as React from "react";
3
2
 
4
- import {IDProvider, type StyleType} from "@khanacademy/wonder-blocks-core";
3
+ import {IDProvider, StyleType} from "@khanacademy/wonder-blocks-core";
5
4
 
6
5
  import FieldHeading from "./field-heading";
7
- import TextField, {type TextFieldType} from "./text-field";
6
+ import TextField, {TextFieldType} from "./text-field";
8
7
 
9
- type WithForwardRef = {|forwardedRef: React.Ref<"input">|};
8
+ type WithForwardRef = {
9
+ forwardedRef: React.ForwardedRef<HTMLInputElement>;
10
+ };
10
11
 
11
- type Props = {|
12
+ type Props = {
12
13
  /**
13
14
  * An optional unique identifier for the TextField.
14
15
  * If no id is specified, a unique id will be auto-generated.
15
16
  */
16
- id?: string,
17
-
17
+ id?: string;
18
18
  /**
19
19
  * Determines the type of input. Defaults to text.
20
20
  */
21
- type: TextFieldType,
22
-
21
+ type: TextFieldType;
23
22
  /**
24
23
  * Provide a label for the TextField.
25
24
  */
26
- label: React.Node,
27
-
25
+ label: React.ReactNode;
28
26
  /**
29
27
  * Provide a description for the TextField.
30
28
  */
31
- description?: React.Node,
32
-
29
+ description?: React.ReactNode;
33
30
  /**
34
31
  * The input value.
35
32
  */
36
- value: string,
37
-
33
+ value: string;
38
34
  /**
39
35
  * Makes a read-only input field that cannot be focused. Defaults to false.
40
36
  */
41
- disabled: boolean,
42
-
37
+ disabled: boolean;
43
38
  /**
44
39
  * Whether this field is required to to continue, or the error message to
45
40
  * render if this field is left blank.
@@ -62,54 +57,44 @@ type Props = {|
62
57
  * there is no corresponding message and the default untranlsated message
63
58
  * will be used.
64
59
  */
65
- required?: boolean | string,
66
-
60
+ required?: boolean | string;
67
61
  /**
68
62
  * Identifies the element or elements that describes this text field.
69
63
  */
70
- ariaDescribedby?: string | Array<string>,
71
-
64
+ ariaDescribedby?: string | undefined;
72
65
  /**
73
66
  * Provide a validation for the input value.
74
67
  * Return a string error message or null | void for a valid input.
75
68
  */
76
- validate?: (value: string) => ?string,
77
-
69
+ validate?: (value: string) => string | null | undefined;
78
70
  /**
79
71
  * Called when the TextField input is validated.
80
72
  */
81
- onValidate?: (errorMessage: ?string) => mixed,
82
-
73
+ onValidate?: (errorMessage?: string | null | undefined) => unknown;
83
74
  /**
84
75
  * Called when the value has changed.
85
76
  */
86
- onChange: (newValue: string) => mixed,
87
-
77
+ onChange: (newValue: string) => unknown;
88
78
  /**
89
79
  * Called when a key is pressed.
90
80
  */
91
- onKeyDown?: (event: SyntheticKeyboardEvent<HTMLInputElement>) => mixed,
92
-
81
+ onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => unknown;
93
82
  /**
94
83
  * Called when the element has been focused.
95
84
  */
96
- onFocus?: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed,
97
-
85
+ onFocus?: (event: React.FocusEvent<HTMLInputElement>) => unknown;
98
86
  /**
99
87
  * Called when the element has been blurred.
100
88
  */
101
- onBlur?: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed,
102
-
89
+ onBlur?: (event: React.FocusEvent<HTMLInputElement>) => unknown;
103
90
  /**
104
91
  * Provide hints or examples of what to enter.
105
92
  */
106
- placeholder?: string,
107
-
93
+ placeholder?: string;
108
94
  /**
109
95
  * Change the field’s sub-components to fit a dark background.
110
96
  */
111
- light: boolean,
112
-
97
+ light: boolean;
113
98
  /**
114
99
  * Custom styles for the container.
115
100
  *
@@ -122,46 +107,39 @@ type Props = {|
122
107
  * In this case, the container would maintain its width ocuppying
123
108
  * unnecessary space when the text field is smaller.
124
109
  */
125
- style?: StyleType,
126
-
110
+ style?: StyleType;
127
111
  /**
128
112
  * Optional test ID for e2e testing.
129
113
  */
130
- testId?: string,
131
-
114
+ testId?: string;
132
115
  /**
133
116
  * Specifies if the TextField is read-only.
134
117
  */
135
- readOnly?: boolean,
136
-
118
+ readOnly?: boolean;
137
119
  /**
138
120
  * Specifies if the TextField allows autocomplete.
139
121
  */
140
- autoComplete?: string,
141
- |};
122
+ autoComplete?: string;
123
+ };
142
124
 
143
- type PropsWithForwardRef = {|
144
- ...Props,
145
- ...WithForwardRef,
146
- |};
125
+ type PropsWithForwardRef = Props & WithForwardRef;
147
126
 
148
- type DefaultProps = {|
149
- type: $PropertyType<PropsWithForwardRef, "type">,
150
- disabled: $PropertyType<PropsWithForwardRef, "disabled">,
151
- light: $PropertyType<PropsWithForwardRef, "light">,
152
- |};
127
+ type DefaultProps = {
128
+ type: PropsWithForwardRef["type"];
129
+ disabled: PropsWithForwardRef["disabled"];
130
+ light: PropsWithForwardRef["light"];
131
+ };
153
132
 
154
- type State = {|
133
+ type State = {
155
134
  /**
156
135
  * Displayed when the validation fails.
157
136
  */
158
- error: ?string,
159
-
137
+ error: string | null | undefined;
160
138
  /**
161
139
  * The user focuses on the textfield.
162
140
  */
163
- focused: boolean,
164
- |};
141
+ focused: boolean;
142
+ };
165
143
 
166
144
  /**
167
145
  * A LabeledTextField is an element used to accept a single line of text
@@ -182,7 +160,9 @@ class LabeledTextField extends React.Component<PropsWithForwardRef, State> {
182
160
  };
183
161
  }
184
162
 
185
- handleValidate: (errorMessage: ?string) => mixed = (errorMessage) => {
163
+ handleValidate: (errorMessage?: string | null | undefined) => unknown = (
164
+ errorMessage,
165
+ ) => {
186
166
  const {onValidate} = this.props;
187
167
  this.setState({error: errorMessage}, () => {
188
168
  if (onValidate) {
@@ -191,7 +171,7 @@ class LabeledTextField extends React.Component<PropsWithForwardRef, State> {
191
171
  });
192
172
  };
193
173
 
194
- handleFocus: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed = (
174
+ handleFocus: (event: React.FocusEvent<HTMLInputElement>) => unknown = (
195
175
  event,
196
176
  ) => {
197
177
  const {onFocus} = this.props;
@@ -202,7 +182,7 @@ class LabeledTextField extends React.Component<PropsWithForwardRef, State> {
202
182
  });
203
183
  };
204
184
 
205
- handleBlur: (event: SyntheticFocusEvent<HTMLInputElement>) => mixed = (
185
+ handleBlur: (event: React.FocusEvent<HTMLInputElement>) => unknown = (
206
186
  event,
207
187
  ) => {
208
188
  const {onBlur} = this.props;
@@ -213,7 +193,7 @@ class LabeledTextField extends React.Component<PropsWithForwardRef, State> {
213
193
  });
214
194
  };
215
195
 
216
- render(): React.Node {
196
+ render(): React.ReactElement {
217
197
  const {
218
198
  id,
219
199
  type,
@@ -283,9 +263,12 @@ class LabeledTextField extends React.Component<PropsWithForwardRef, State> {
283
263
  }
284
264
  }
285
265
 
286
- type ExportProps = $Diff<
287
- React.ElementConfig<typeof LabeledTextField>,
288
- WithForwardRef,
266
+ type ExportProps = Omit<
267
+ JSX.LibraryManagedAttributes<
268
+ typeof LabeledTextField,
269
+ React.ComponentProps<typeof LabeledTextField>
270
+ >,
271
+ "forwardedRef"
289
272
  >;
290
273
 
291
274
  /**
@@ -309,6 +292,8 @@ type ExportProps = $Diff<
309
292
  * ```
310
293
  */
311
294
 
312
- export default (React.forwardRef<ExportProps, HTMLInputElement>(
313
- (props, ref) => <LabeledTextField {...props} forwardedRef={ref} />,
314
- ): React.AbstractComponent<ExportProps, HTMLInputElement>);
295
+ export default React.forwardRef<HTMLInputElement, ExportProps>((props, ref) => (
296
+ <LabeledTextField {...props} forwardedRef={ref} />
297
+ )) as React.ForwardRefExoticComponent<
298
+ ExportProps & React.RefAttributes<HTMLInputElement>
299
+ >;
@@ -1,5 +1,3 @@
1
- // @flow
2
-
3
1
  import * as React from "react";
4
2
  import {StyleSheet} from "aphrodite";
5
3
 
@@ -8,13 +6,12 @@ import {addStyle} from "@khanacademy/wonder-blocks-core";
8
6
 
9
7
  import type {ChoiceCoreProps} from "../util/types";
10
8
 
11
- type Props = {|
12
- ...ChoiceCoreProps,
13
- hovered: boolean,
14
- focused: boolean,
15
- pressed: boolean,
16
- waiting: boolean,
17
- |};
9
+ type Props = ChoiceCoreProps & {
10
+ hovered: boolean;
11
+ focused: boolean;
12
+ pressed: boolean;
13
+ waiting: boolean;
14
+ };
18
15
 
19
16
  const {blue, red, white, offWhite, offBlack16, offBlack32, offBlack50} = Color;
20
17
 
@@ -28,7 +25,7 @@ const StyledInput = addStyle("input");
28
25
  return;
29
26
  };
30
27
 
31
- render(): React.Node {
28
+ render(): React.ReactElement {
32
29
  const {
33
30
  checked,
34
31
  disabled,
@@ -55,7 +52,7 @@ const StyledInput = addStyle("input");
55
52
  ];
56
53
  const props = {
57
54
  "data-test-id": testId,
58
- };
55
+ } as const;
59
56
  return (
60
57
  <React.Fragment>
61
58
  <StyledInput
@@ -86,7 +83,7 @@ const disabledChecked = {
86
83
  width: size / 2,
87
84
  borderRadius: "50%",
88
85
  backgroundColor: offBlack32,
89
- };
86
+ } as const;
90
87
  const sharedStyles = StyleSheet.create({
91
88
  // Reset the default styled input element
92
89
  inputReset: {
@@ -128,16 +125,16 @@ const colors = {
128
125
  base: red,
129
126
  active: activeRed,
130
127
  },
131
- };
132
- const styles = {};
133
- const _generateStyles = (checked, error) => {
128
+ } as const;
129
+ const styles: Record<string, any> = {};
130
+ const _generateStyles = (checked: boolean, error: boolean) => {
134
131
  // "hash" the parameters
135
132
  const styleKey = `${String(checked)}-${String(error)}`;
136
133
  if (styles[styleKey]) {
137
134
  return styles[styleKey];
138
135
  }
139
136
  const palette = error ? colors.error : colors.default;
140
- let newStyles = {};
137
+ let newStyles: Record<string, any> = {};
141
138
  if (checked) {
142
139
  newStyles = {
143
140
  default: {
@@ -1,5 +1,3 @@
1
- // @flow
2
-
3
1
  import * as React from "react";
4
2
 
5
3
  import {View, addStyle} from "@khanacademy/wonder-blocks-core";
@@ -9,59 +7,56 @@ import {LabelMedium, LabelSmall} from "@khanacademy/wonder-blocks-typography";
9
7
  import type {StyleType} from "@khanacademy/wonder-blocks-core";
10
8
 
11
9
  import styles from "./group-styles";
12
- import typeof Choice from "./choice";
10
+ import Choice from "./choice";
13
11
 
14
12
  // Keep synced with RadioGroupProps in ../util/types.js
15
- type RadioGroupProps = {|
13
+ type RadioGroupProps = {
16
14
  /**
17
15
  * Children should be Choice components.
18
16
  */
19
- children: Array<?(React.Element<Choice> | false)>,
20
-
17
+ children: Array<
18
+ | React.ReactElement<React.ComponentProps<typeof Choice>>
19
+ | false
20
+ | null
21
+ | undefined
22
+ >;
21
23
  /**
22
24
  * Group name for this checkbox or radio group. Should be unique for all
23
25
  * such groups displayed on a page.
24
26
  */
25
- groupName: string,
26
-
27
+ groupName: string;
27
28
  /**
28
29
  * Optional label for the group. This label is optional to allow for
29
30
  * greater flexibility in implementing checkbox and radio groups.
30
31
  */
31
- label?: React.Node,
32
-
32
+ label?: React.ReactNode;
33
33
  /**
34
34
  * Optional description for the group.
35
35
  */
36
- description?: React.Node,
37
-
36
+ description?: React.ReactNode;
38
37
  /**
39
38
  * Optional error message. If supplied, the group will be displayed in an
40
39
  * error state, along with this error message. If no error state is desired,
41
40
  * simply do not supply this prop, or pass along null.
42
41
  */
43
- errorMessage?: string,
44
-
42
+ errorMessage?: string;
45
43
  /**
46
44
  * Custom styling for this group of checkboxes.
47
45
  */
48
- style?: StyleType,
49
-
46
+ style?: StyleType;
50
47
  /**
51
48
  * Callback for when the selected value of the radio group has changed.
52
49
  */
53
- onChange: (selectedValue: string) => mixed,
54
-
50
+ onChange: (selectedValue: string) => unknown;
55
51
  /**
56
52
  * Value of the selected radio item.
57
53
  */
58
- selectedValue: string,
59
-
54
+ selectedValue: string;
60
55
  /**
61
56
  * Test ID used for e2e testing.
62
57
  */
63
- testId?: string,
64
- |};
58
+ testId?: string;
59
+ };
65
60
 
66
61
  const StyledFieldset = addStyle<"fieldset">("fieldset");
67
62
  const StyledLegend = addStyle<"legend">("legend");
@@ -106,7 +101,7 @@ export default class RadioGroup extends React.Component<RadioGroupProps> {
106
101
  this.props.onChange(changedValue);
107
102
  }
108
103
 
109
- render(): React.Node {
104
+ render(): React.ReactElement {
110
105
  const {
111
106
  children,
112
107
  label,
@@ -144,8 +139,10 @@ export default class RadioGroup extends React.Component<RadioGroupProps> {
144
139
  )}
145
140
 
146
141
  {allChildren.map((child, index) => {
142
+ // @ts-expect-error [FEI-5019] - TS2339 - Property 'props' does not exist on type 'ReactChild | ReactFragment | ReactPortal'.
147
143
  const {style, value} = child.props;
148
144
  const checked = selectedValue === value;
145
+ // @ts-expect-error [FEI-5019] - TS2769 - No overload matches this call.
149
146
  return React.cloneElement(child, {
150
147
  checked: checked,
151
148
  error: !!errorMessage,
@@ -1,79 +1,65 @@
1
- // @flow
2
-
3
1
  import * as React from "react";
4
2
 
5
3
  import type {AriaProps, StyleType} from "@khanacademy/wonder-blocks-core";
6
4
  import ChoiceInternal from "./choice-internal";
7
5
 
8
6
  // Keep synced with ChoiceComponentProps in ../util/types.js
9
- type ChoiceComponentProps = {|
10
- ...AriaProps,
11
-
7
+ type ChoiceComponentProps = AriaProps & {
12
8
  /**
13
9
  * Whether this component is checked
14
10
  */
15
- checked: boolean,
16
-
11
+ checked: boolean;
17
12
  /**
18
13
  * Whether this component is disabled
19
14
  */
20
- disabled: boolean,
21
-
15
+ disabled: boolean;
22
16
  /**
23
17
  * Whether this component should show an error state
24
18
  */
25
- error: boolean,
26
-
19
+ error: boolean;
27
20
  /**
28
21
  * Callback when this component is selected. The newCheckedState is the
29
22
  * new checked state of the component.
30
23
  */
31
- onChange: (newCheckedState: boolean) => mixed,
32
-
24
+ onChange: (newCheckedState: boolean) => unknown;
33
25
  /**
34
26
  * Optional label for the field.
35
27
  */
36
- label?: React.Node,
37
-
28
+ label?: React.ReactNode;
38
29
  /**
39
30
  * Optional description for the field.
40
31
  */
41
- description?: React.Node,
42
-
32
+ description?: React.ReactNode;
43
33
  /**
44
34
  * Unique identifier attached to the HTML input element. If used, need to
45
35
  * guarantee that the ID is unique within everything rendered on a page.
46
36
  * Used to match `<label>` with `<input>` elements for screenreaders.
47
37
  */
48
- id?: string,
49
-
38
+ id?: string;
50
39
  /**
51
40
  * Optional styling for the container. Does not style the component.
52
41
  */
53
- style?: StyleType,
54
-
42
+ style?: StyleType;
55
43
  /**
56
44
  * Adds CSS classes to the Checkbox.
57
45
  */
58
- className?: string,
59
-
46
+ className?: string;
60
47
  /**
61
48
  * Optional test ID for e2e testing
62
49
  */
63
- testId?: string,
64
-
50
+ testId?: string;
65
51
  /**
66
52
  * Name for the checkbox or radio button group. Only applicable for group
67
53
  * contexts, auto-populated by group components via Choice.
68
54
  * @ignore
69
55
  */
70
- groupName?: string,
71
- |};
56
+ groupName?: string;
57
+ };
72
58
 
73
- type DefaultProps = {|
74
- disabled: $PropertyType<ChoiceComponentProps, "disabled">,
75
- error: $PropertyType<ChoiceComponentProps, "error">,
76
- |};
59
+ type DefaultProps = {
60
+ disabled: ChoiceComponentProps["disabled"];
61
+ error: ChoiceComponentProps["error"];
62
+ };
77
63
 
78
64
  /**
79
65
  * 🔘 A nicely styled radio button for all your non-AMFM radio button needs. Can
@@ -87,7 +73,7 @@ type DefaultProps = {|
87
73
  error: false,
88
74
  };
89
75
 
90
- render(): React.Node {
76
+ render(): React.ReactElement {
91
77
  return <ChoiceInternal variant="radio" {...this.props} />;
92
78
  }
93
79
  }