@availity/mui-controlled-form 0.2.5 → 0.3.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +2 -2
  3. package/dist/index.d.mts +122 -34
  4. package/dist/index.d.ts +122 -34
  5. package/dist/index.js +263 -225
  6. package/dist/index.mjs +279 -240
  7. package/package.json +2 -2
  8. package/src/index.ts +47 -0
  9. package/src/lib/AsyncAutocomplete.stories.tsx +21 -36
  10. package/src/lib/AsyncAutocomplete.test.tsx +17 -53
  11. package/src/lib/AsyncAutocomplete.tsx +23 -20
  12. package/src/lib/Autocomplete.stories.tsx +20 -33
  13. package/src/lib/Autocomplete.test.tsx +7 -37
  14. package/src/lib/Autocomplete.tsx +16 -15
  15. package/src/lib/Checkbox.stories.tsx +50 -43
  16. package/src/lib/Checkbox.test.tsx +14 -46
  17. package/src/lib/Checkbox.tsx +30 -15
  18. package/src/lib/CodesAutocomplete.stories.tsx +21 -35
  19. package/src/lib/CodesAutocomplete.test.tsx +20 -54
  20. package/src/lib/CodesAutocomplete.tsx +23 -20
  21. package/src/lib/ControlledForm.stories.tsx +1 -0
  22. package/src/lib/ControlledForm.tsx +8 -4
  23. package/src/lib/Datepicker.stories.tsx +19 -32
  24. package/src/lib/Datepicker.test.tsx +3 -35
  25. package/src/lib/Datepicker.tsx +18 -10
  26. package/src/lib/Input.stories.tsx +32 -33
  27. package/src/lib/Input.test.tsx +71 -7
  28. package/src/lib/Input.tsx +44 -24
  29. package/src/lib/OrganizationAutocomplete.stories.tsx +30 -35
  30. package/src/lib/OrganizationAutocomplete.test.tsx +23 -57
  31. package/src/lib/OrganizationAutocomplete.tsx +24 -23
  32. package/src/lib/ProviderAutocomplete.stories.tsx +20 -35
  33. package/src/lib/ProviderAutocomplete.test.tsx +29 -63
  34. package/src/lib/ProviderAutocomplete.tsx +22 -20
  35. package/src/lib/RadioGroup.stories.tsx +41 -36
  36. package/src/lib/RadioGroup.test.tsx +3 -35
  37. package/src/lib/RadioGroup.tsx +33 -25
  38. package/src/lib/Select.stories.tsx +78 -45
  39. package/src/lib/Select.test.tsx +8 -36
  40. package/src/lib/Select.tsx +44 -25
  41. package/src/lib/TextField.stories.tsx +26 -34
  42. package/src/lib/TextField.test.tsx +71 -5
  43. package/src/lib/TextField.tsx +55 -37
  44. package/src/lib/Types.tsx +2489 -0
  45. package/src/lib/UtilComponents.tsx +52 -0
  46. package/docs/propDefinitions.tsx +0 -31
@@ -1,38 +1,8 @@
1
1
  import { render, fireEvent, waitFor } from '@testing-library/react';
2
- import { useFormContext } from 'react-hook-form';
3
- import { Paper } from '@availity/mui-paper';
4
- import { Typography } from '@availity/mui-typography';
5
- import { Grid } from '@availity/mui-layout';
6
- import { Button } from '@availity/mui-button';
7
2
  import { ThemeProvider } from '@availity/theme-provider';
8
3
  import dayjs from 'dayjs';
9
- import { ControlledForm } from './ControlledForm';
10
4
  import { ControlledDatepicker } from './Datepicker';
11
-
12
- const SubmittedValues = () => {
13
- const {
14
- getValues,
15
- formState: { isSubmitSuccessful },
16
- } = useFormContext();
17
-
18
- return isSubmitSuccessful ? (
19
- <Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
20
- <Typography variant="h2">Submitted Values</Typography>
21
- <pre data-testid="result">{JSON.stringify(getValues(), null, 2)}</pre>
22
- </Paper>
23
- ) : null;
24
- };
25
-
26
- const Actions = () => {
27
- const {
28
- formState: { isSubmitSuccessful },
29
- } = useFormContext();
30
- return (
31
- <Grid container direction="row" justifyContent="space-between">
32
- <Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
33
- </Grid>
34
- );
35
- };
5
+ import { TestForm } from './UtilComponents';
36
6
 
37
7
  const onSubmit = jest.fn();
38
8
 
@@ -40,7 +10,7 @@ describe('Datepicker', () => {
40
10
  test('should render successfully and submit selection', async () => {
41
11
  const screen = render(
42
12
  <ThemeProvider>
43
- <ControlledForm values={{ controlledDatepicker: undefined }} onSubmit={onSubmit}>
13
+ <TestForm UseFormOptions={{values: { controlledDatepicker: null}}} onSubmit={onSubmit}>
44
14
  <ControlledDatepicker
45
15
  name="controlledDatepicker"
46
16
  FieldProps={{
@@ -50,9 +20,7 @@ describe('Datepicker', () => {
50
20
  label: 'Date',
51
21
  }}
52
22
  />
53
- <Actions />
54
- <SubmittedValues />
55
- </ControlledForm>
23
+ </TestForm>
56
24
  </ThemeProvider>
57
25
  );
58
26
  expect(screen.getAllByText('Date')).toBeTruthy();
@@ -1,9 +1,14 @@
1
1
  import { Datepicker, DatepickerProps } from '@availity/mui-datepicker';
2
- import { useFormContext, RegisterOptions, FieldValues, Controller, ControllerProps } from 'react-hook-form';
2
+ import { RegisterOptions, FieldValues, Controller } from 'react-hook-form';
3
+ import { ControllerProps, DeprecatedRulesProps } from './Types';
3
4
 
4
- export type ControlledDatepickerProps = DatepickerProps &
5
- Omit<RegisterOptions<FieldValues, string>, 'disabled' | 'valueAsNumber' | 'valueAsDate' | 'setValueAs'> &
6
- Pick<ControllerProps, 'defaultValue' | 'shouldUnregister' | 'name'>;
5
+ export type ControlledDatepickerProps = Omit<DatepickerProps,
6
+ 'onBlur' | 'onChange' | 'value' | 'name'
7
+ > & Pick<RegisterOptions<FieldValues, string>,
8
+ 'onBlur' | 'onChange' | 'value'
9
+ > & ControllerProps
10
+ //TODO v1 - remove deprecated props
11
+ & DeprecatedRulesProps;
7
12
 
8
13
  export const ControlledDatepicker = ({
9
14
  name,
@@ -17,17 +22,16 @@ export const ControlledDatepicker = ({
17
22
  onChange,
18
23
  pattern,
19
24
  required,
25
+ rules = {},
20
26
  shouldUnregister,
21
27
  validate,
22
28
  value,
23
29
  FieldProps,
24
30
  ...rest
25
31
  }: ControlledDatepickerProps) => {
26
- const { control } = useFormContext();
27
32
  return (
28
33
  <Controller
29
34
  name={name}
30
- control={control}
31
35
  defaultValue={defaultValue}
32
36
  rules={{
33
37
  deps,
@@ -42,24 +46,28 @@ export const ControlledDatepicker = ({
42
46
  shouldUnregister,
43
47
  validate,
44
48
  value,
49
+ ...rules,
45
50
  }}
46
51
  shouldUnregister={shouldUnregister}
47
- render={({ field: { onChange, value }, fieldState: { error } }) => (
52
+ render={({ field: { onChange, value, onBlur, ref }, fieldState: { error } }) => (
48
53
  <Datepicker
49
54
  {...rest}
50
55
  FieldProps={{
51
56
  ...FieldProps,
52
- required: typeof required === 'object' ? required.value : !!required,
53
57
  error: !!error,
54
- helperText: error?.message ? (
58
+ helperText: error ? (
55
59
  <>
56
- {error?.message}
60
+ {error.message}
57
61
  <br />
58
62
  {FieldProps?.helperText}
59
63
  </>
60
64
  ) : (
61
65
  FieldProps?.helperText
62
66
  ),
67
+ inputRef: ref,
68
+ inputProps: {
69
+ onBlur: onBlur
70
+ },
63
71
  }}
64
72
  onChange={onChange}
65
73
  value={value || null}
@@ -1,60 +1,59 @@
1
1
  import type { Meta, StoryObj } from '@storybook/react';
2
2
  import { ControlledInput, ControlledInputProps } from './Input';
3
- import { ControlledForm } from './ControlledForm';
4
3
  import { Button } from '@availity/mui-button';
5
- import { useFormContext } from 'react-hook-form';
6
4
  import { Paper } from '@availity/mui-paper';
7
5
  import { Typography } from '@availity/mui-typography';
8
6
  import { Grid } from '@availity/mui-layout';
7
+ import { AllControllerPropertiesCategorized, InputPropsCategorized } from './Types';
8
+ import { FormProvider, useForm } from '..';
9
9
 
10
10
  const meta: Meta<typeof ControlledInput> = {
11
11
  title: 'Form Components/Controlled Form/ControlledInput',
12
12
  component: ControlledInput,
13
13
  tags: ['autodocs'],
14
+ argTypes: {...AllControllerPropertiesCategorized, ...InputPropsCategorized},
15
+ parameters: {
16
+ controls: {
17
+ exclude: [
18
+ 'className',
19
+ 'defaultChecked',
20
+ 'onError',
21
+ 'ref',
22
+ 'style',
23
+ 'tabIndex'
24
+ ]
25
+ }
26
+ }
14
27
  };
15
28
 
16
29
  export default meta;
17
30
 
18
31
  export const _ControlledInput: StoryObj<typeof ControlledInput> = {
19
32
  render: (args: ControlledInputProps) => {
20
- const SubmittedValues = () => {
21
- const {
22
- getValues,
23
- formState: { isSubmitSuccessful },
24
- } = useFormContext();
33
+ const methods = useForm();
25
34
 
26
- return isSubmitSuccessful ? (
27
- <Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
28
- <Typography variant="h2">Submitted Values</Typography>
29
- <pre>{JSON.stringify(getValues(), null, 2)}</pre>
30
- </Paper>
31
- ) : null;
32
- };
33
-
34
- const Actions = () => {
35
- const {
36
- reset,
37
- formState: { isSubmitSuccessful },
38
- } = useFormContext();
39
- return (
40
- <Grid container direction="row" justifyContent="space-between" marginTop={1}>
41
- <Button disabled={!isSubmitSuccessful} children="Reset" color="secondary" onClick={() => reset()} />
42
- <Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
43
- </Grid>
44
- );
45
- };
46
35
  return (
47
- <ControlledForm values={{ controlledInput: undefined }} onSubmit={(data) => data}>
48
- <ControlledInput {...args} />
49
- <Actions />
50
- <SubmittedValues />
51
- </ControlledForm>
36
+ <FormProvider {...methods}>
37
+ <form onSubmit={methods.handleSubmit((data) => data)}>
38
+ <ControlledInput {...args} />
39
+ <Grid container direction="row" justifyContent="space-between" marginTop={1}>
40
+ <Button disabled={!methods?.formState?.isSubmitSuccessful} children="Reset" color="secondary" onClick={() => methods.reset()} />
41
+ <Button type="submit" disabled={methods?.formState?.isSubmitSuccessful} children="Submit" />
42
+ </Grid>
43
+ { methods?.formState?.isSubmitSuccessful ? (
44
+ <Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
45
+ <Typography variant="h2">Submitted Values</Typography>
46
+ <pre data-testid="result">{JSON.stringify(methods.getValues(), null, 2)}</pre>
47
+ </Paper>
48
+ ) : null }
49
+ </form>
50
+ </FormProvider>
52
51
  );
53
52
  },
54
53
  args: {
55
54
  name: 'controlledInput',
56
55
  required: true,
57
- maxLength: { value: 10, message: 'Too long' },
56
+ rules: { required:'This is required.', maxLength: { value: 10, message: 'Too long' }},
58
57
  inputProps: { 'aria-label': 'Input Label' },
59
58
  },
60
59
  };
@@ -1,11 +1,12 @@
1
1
  import { render, fireEvent, waitFor } from '@testing-library/react';
2
- import { useFormContext } from 'react-hook-form';
3
2
  import { Paper } from '@availity/mui-paper';
4
3
  import { Typography } from '@availity/mui-typography';
5
4
  import { Grid } from '@availity/mui-layout';
6
5
  import { Button } from '@availity/mui-button';
7
- import { ControlledForm } from './ControlledForm';
8
6
  import { ControlledInput } from './Input';
7
+ import { useFormContext } from '..';
8
+ import { ControlledForm } from './ControlledForm';
9
+ import { TestForm } from './UtilComponents';
9
10
 
10
11
  const SubmittedValues = () => {
11
12
  const {
@@ -33,11 +34,12 @@ const Actions = () => {
33
34
  };
34
35
 
35
36
  const onSubmit = jest.fn();
37
+ const onSubmitDeprecated = jest.fn();
36
38
 
37
39
  describe('ControlledInput', () => {
38
- test('should render the error styling if an error is returned', async () => {
40
+ test('Deprecation Check: should render the error styling if an error is returned', async () => {
39
41
  const screen = render(
40
- <ControlledForm values={{ controlledInput: undefined }} onSubmit={(data) => data}>
42
+ <ControlledForm values={{ controlledInput: undefined }} onSubmit={onSubmitDeprecated}>
41
43
  <ControlledInput
42
44
  data-testid="controlledInputWrapper"
43
45
  name="controlledInput"
@@ -58,16 +60,16 @@ describe('ControlledInput', () => {
58
60
 
59
61
  fireEvent.click(screen.getByText('Submit'));
60
62
 
61
- await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(0));
63
+ await waitFor(() => expect(onSubmitDeprecated).toHaveBeenCalledTimes(0));
62
64
 
63
65
  const muiInputBase = screen.getByTestId('controlledInputWrapper');
64
66
 
65
67
  await waitFor(() => expect(muiInputBase.classList).toContain('Mui-error'));
66
68
  });
67
69
 
68
- test('should render the error styling if an error is returned', async () => {
70
+ test('DeprecatedCheck: should not render the error styling if no error is returned', async () => {
69
71
  const screen = render(
70
- <ControlledForm values={{ controlledInput: undefined }} onSubmit={onSubmit}>
72
+ <ControlledForm values={{ controlledInput: undefined }} onSubmit={onSubmitDeprecated}>
71
73
  <ControlledInput
72
74
  name="controlledInput"
73
75
  required="This field is required."
@@ -87,6 +89,68 @@ describe('ControlledInput', () => {
87
89
 
88
90
  fireEvent.click(screen.getByText('Submit'));
89
91
 
92
+ await waitFor(() => expect(onSubmitDeprecated).toHaveBeenCalledTimes(1));
93
+
94
+ const result = screen.getByTestId('result');
95
+ await waitFor(() => {
96
+ const formValues = JSON.parse(result.innerHTML).controlledInput;
97
+ expect(formValues).toBe('Input Text');
98
+ });
99
+ });
100
+
101
+ test('should render the error styling if an error is returned', async () => {
102
+ const screen = render(
103
+ <TestForm UseFormOptions={{values: { controlledInput: undefined }}} onSubmit={onSubmit}>
104
+ <ControlledInput
105
+ data-testid="controlledInputWrapper"
106
+ name="controlledInput"
107
+ rules={{
108
+ required: "This field is required.",
109
+ maxLength:{ value: 10, message: 'Too long' }
110
+ }}
111
+ inputProps={{
112
+ 'data-testid': 'testInput',
113
+ }}
114
+ />
115
+ </TestForm>
116
+ );
117
+
118
+ const input = screen.getByTestId('testInput');
119
+
120
+ fireEvent.change(input, { target: { value: 'This is way too much text' } });
121
+
122
+ fireEvent.click(screen.getByText('Submit'));
123
+
124
+ await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(0));
125
+
126
+ const muiInputBase = screen.getByTestId('controlledInputWrapper');
127
+
128
+ await waitFor(() => expect(muiInputBase.classList).toContain('Mui-error'));
129
+ });
130
+
131
+ test('should not render the error styling if no error is returned', async () => {
132
+ const screen = render(
133
+ <TestForm UseFormOptions={{values: { controlledInput: undefined }}} onSubmit={onSubmit}>
134
+ <ControlledInput
135
+ data-testid="controlledInputWrapper"
136
+ name="controlledInput"
137
+ rules={{
138
+ required: "This field is required.",
139
+ maxLength:{ value: 10, message: 'Too long' }
140
+ }}
141
+ inputProps={{
142
+ 'data-testid': 'testInput',
143
+ }}
144
+ />
145
+ </TestForm>
146
+ );
147
+
148
+ const input = screen.getByTestId('testInput');
149
+
150
+ fireEvent.change(input, { target: { value: 'Input Text' } });
151
+
152
+ fireEvent.click(screen.getByText('Submit'));
153
+
90
154
  await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
91
155
 
92
156
  const result = screen.getByTestId('result');
package/src/lib/Input.tsx CHANGED
@@ -1,51 +1,71 @@
1
1
  import { Input, InputProps } from '@availity/mui-form-utils';
2
- import { useFormContext, RegisterOptions, FieldValues } from 'react-hook-form';
2
+ import { RegisterOptions, FieldValues, Controller } from 'react-hook-form';
3
+ import { ControllerProps, DeprecatedRulesProps } from './Types';
3
4
 
4
- export type ControlledInputProps = Omit<InputProps, 'error' | 'name' | 'required'> & { name: string } & RegisterOptions<
5
- FieldValues,
6
- string
7
- >;
5
+ export type ControlledInputProps = Omit<InputProps,
6
+ 'onBlur' | 'onChange' | 'value' | 'name' | 'required'
7
+ > & Pick<RegisterOptions<FieldValues, string>,
8
+ 'onBlur' | 'onChange' | 'value'
9
+ > & ControllerProps
10
+ //TODO v1 - remove deprecated props
11
+ & Omit<DeprecatedRulesProps, 'required'> & {
12
+ /** If `true`, will add `aria-required` to `input`.
13
+ *
14
+ * @deprecated There has been a collision of properties. The boolean value
15
+ * to mark the input as required will remain in future versions, but the
16
+ * required object for `react-hook-form` has been moved to the `rules` prop.
17
+ */
18
+ required?: boolean | RegisterOptions['required'];
19
+ };
8
20
 
9
21
  export const ControlledInput = ({
10
22
  name,
11
- required,
12
- maxLength,
13
- minLength,
23
+ defaultValue,
24
+ deps,
25
+ disabled,
14
26
  max,
27
+ maxLength,
15
28
  min,
29
+ minLength,
30
+ onBlur,
31
+ onChange,
16
32
  pattern,
33
+ required,
34
+ rules = {},
35
+ shouldUnregister,
17
36
  validate,
18
- setValueAs,
19
- disabled,
20
- onChange,
21
- onBlur,
22
37
  value,
23
- shouldUnregister,
24
- deps,
25
38
  ...rest
26
39
  }: ControlledInputProps) => {
27
- const { register, getFieldState } = useFormContext();
28
40
  return (
29
- <Input
30
- {...rest}
31
- error={!!getFieldState(name).error}
32
- required={typeof required === 'object' ? required.value : !!required}
33
- {...register(name, {
34
- required,
41
+ <Controller
42
+ name={name}
43
+ defaultValue={defaultValue}
44
+ disabled={disabled}
45
+ rules={{
46
+ required: typeof required === 'boolean' ? undefined : required,
35
47
  maxLength,
36
48
  minLength,
37
49
  max,
38
50
  min,
39
51
  pattern,
40
52
  validate,
41
- setValueAs,
42
- disabled,
43
53
  onChange,
44
54
  onBlur,
45
55
  value,
46
56
  shouldUnregister,
47
57
  deps,
48
- })}
58
+ ...rules,
59
+ }}
60
+ shouldUnregister={shouldUnregister}
61
+ render={({ field, fieldState: { error } }) => (
62
+ <Input
63
+ {...rest}
64
+ {...field}
65
+ error={!!error}
66
+ required={!!required}
67
+ />
68
+ )}
49
69
  />
50
70
  );
51
71
  };
@@ -1,19 +1,28 @@
1
1
  import type { Meta, StoryObj } from '@storybook/react';
2
2
  import { ControlledOrganizationAutocomplete } from './OrganizationAutocomplete';
3
- import { ControlledForm } from './ControlledForm';
4
3
  import { Button } from '@availity/mui-button';
5
- import { useFormContext } from 'react-hook-form';
6
4
  import { Paper } from '@availity/mui-paper';
7
5
  import { Typography } from '@availity/mui-typography';
8
6
  import { Grid } from '@availity/mui-layout';
9
7
  import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
10
- import { missingRHFprops } from '../../docs/propDefinitions';
8
+ import { AllControllerPropertiesCategorized, OrganizationAutocompletePropsCategorized } from './Types';
9
+ import { FormProvider, useForm } from '..';
11
10
 
12
11
  const meta: Meta<typeof ControlledOrganizationAutocomplete> = {
13
12
  title: 'Form Components/Controlled Form/Autocomplete/ControlledOrganizationAutocomplete',
14
13
  component: ControlledOrganizationAutocomplete,
15
14
  tags: ['autodocs'],
16
- argTypes: missingRHFprops,
15
+ argTypes: {...AllControllerPropertiesCategorized, ...OrganizationAutocompletePropsCategorized},
16
+ parameters: {
17
+ controls: {
18
+ exclude: [
19
+ 'max',
20
+ 'maxLength',
21
+ 'min',
22
+ 'minLength'
23
+ ]
24
+ }
25
+ }
17
26
  };
18
27
 
19
28
  export default meta;
@@ -28,45 +37,31 @@ const client = new QueryClient({
28
37
 
29
38
  export const _ControlledOrganizationAutoComplete: StoryObj<typeof ControlledOrganizationAutocomplete> = {
30
39
  render: (args) => {
31
- const SubmittedValues = () => {
32
- const {
33
- getValues,
34
- formState: { isSubmitSuccessful },
35
- } = useFormContext();
40
+ const methods = useForm();
36
41
 
37
- return isSubmitSuccessful ? (
38
- <Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
39
- <Typography variant="h2">Submitted Values</Typography>
40
- <pre>{JSON.stringify(getValues(), null, 2)}</pre>
41
- </Paper>
42
- ) : null;
43
- };
44
-
45
- const Actions = () => {
46
- const {
47
- reset,
48
- formState: { isSubmitSuccessful },
49
- } = useFormContext();
50
- return (
51
- <Grid container direction="row" justifyContent="space-between" marginTop={1}>
52
- <Button disabled={!isSubmitSuccessful} children="Reset" color="secondary" onClick={() => reset()} />
53
- <Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
54
- </Grid>
55
- );
56
- };
57
42
  return (
58
43
  <QueryClientProvider client={client}>
59
- <ControlledForm values={{}} onSubmit={(data) => data}>
60
- <ControlledOrganizationAutocomplete {...args} />
61
- <Actions />
62
- <SubmittedValues />
63
- </ControlledForm>
44
+ <FormProvider {...methods}>
45
+ <form onSubmit={methods.handleSubmit((data) => data)}>
46
+ <ControlledOrganizationAutocomplete {...args} />
47
+ <Grid container direction="row" justifyContent="space-between" marginTop={1}>
48
+ <Button disabled={!methods?.formState?.isSubmitSuccessful} children="Reset" color="secondary" onClick={() => methods.reset()} />
49
+ <Button type="submit" disabled={methods?.formState?.isSubmitSuccessful} children="Submit" />
50
+ </Grid>
51
+ { methods?.formState?.isSubmitSuccessful ? (
52
+ <Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
53
+ <Typography variant="h2">Submitted Values</Typography>
54
+ <pre data-testid="result">{JSON.stringify(methods.getValues(), null, 2)}</pre>
55
+ </Paper>
56
+ ) : null }
57
+ </form>
58
+ </FormProvider>
64
59
  </QueryClientProvider>
65
60
  );
66
61
  },
67
62
  args: {
68
63
  name: 'controlledOrganizationAutocomplete',
69
- required: 'This is required.',
64
+ rules: { required:'This is required.' },
70
65
  FieldProps: {
71
66
  label: 'Organization Select',
72
67
  helperText: 'Select an Organization from the list',
@@ -1,39 +1,9 @@
1
1
  import { fireEvent, render, waitFor } from '@testing-library/react';
2
2
  import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
3
- import { Paper } from '@availity/mui-paper';
4
- import { Typography } from '@availity/mui-typography';
5
- import { useFormContext } from 'react-hook-form';
6
- import { Grid } from '@availity/mui-layout';
7
- import { Button } from '@availity/mui-button';
8
3
  // eslint-disable-next-line @nx/enforce-module-boundaries
9
4
  import { server } from '@availity/mock/src/lib/server';
10
- import { ControlledForm } from './ControlledForm';
11
5
  import { ControlledOrganizationAutocomplete } from './OrganizationAutocomplete';
12
-
13
- const SubmittedValues = () => {
14
- const {
15
- getValues,
16
- formState: { isSubmitSuccessful },
17
- } = useFormContext();
18
-
19
- return isSubmitSuccessful ? (
20
- <Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
21
- <Typography variant="h2">Submitted Values</Typography>
22
- <pre data-testid="result">{JSON.stringify(getValues(), null, 2)}</pre>
23
- </Paper>
24
- ) : null;
25
- };
26
-
27
- const Actions = () => {
28
- const {
29
- formState: { isSubmitSuccessful },
30
- } = useFormContext();
31
- return (
32
- <Grid container direction="row" justifyContent="space-between">
33
- <Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
34
- </Grid>
35
- );
36
- };
6
+ import { TestForm } from './UtilComponents';
37
7
 
38
8
  const onSubmit = jest.fn();
39
9
 
@@ -61,19 +31,17 @@ describe('ControlledOrganizationAutocomplete', () => {
61
31
  test('should loadOptions successfully', async () => {
62
32
  const screen = render(
63
33
  <QueryClientProvider client={client}>
64
- <ControlledForm values={{}} onSubmit={onSubmit}>
65
- <ControlledOrganizationAutocomplete
66
- name="controlledOrganizationAutocomplete"
67
- FieldProps={{
68
- label: 'Organization Select',
69
- helperText: 'Select an Organization from the list',
70
- placeholder: 'Select...',
71
- fullWidth: false,
72
- }}
73
- />
74
- <Actions />
75
- <SubmittedValues />
76
- </ControlledForm>
34
+ <TestForm onSubmit={onSubmit}>
35
+ <ControlledOrganizationAutocomplete
36
+ name="controlledOrganizationAutocomplete"
37
+ FieldProps={{
38
+ label: 'Organization Select',
39
+ helperText: 'Select an Organization from the list',
40
+ placeholder: 'Select...',
41
+ fullWidth: false,
42
+ }}
43
+ />
44
+ </TestForm>
77
45
  </QueryClientProvider>
78
46
  );
79
47
 
@@ -87,19 +55,17 @@ describe('ControlledOrganizationAutocomplete', () => {
87
55
  test('should set the value and submit the form data', async () => {
88
56
  const screen = render(
89
57
  <QueryClientProvider client={client}>
90
- <ControlledForm values={{}} onSubmit={onSubmit}>
91
- <ControlledOrganizationAutocomplete
92
- name="controlledOrganizationAutocomplete"
93
- FieldProps={{
94
- label: 'Organization Select',
95
- helperText: 'Select an Organization from the list',
96
- placeholder: 'Select...',
97
- fullWidth: false,
98
- }}
99
- />
100
- <Actions />
101
- <SubmittedValues />
102
- </ControlledForm>
58
+ <TestForm onSubmit={onSubmit}>
59
+ <ControlledOrganizationAutocomplete
60
+ name="controlledOrganizationAutocomplete"
61
+ FieldProps={{
62
+ label: 'Organization Select',
63
+ helperText: 'Select an Organization from the list',
64
+ placeholder: 'Select...',
65
+ fullWidth: false,
66
+ }}
67
+ />
68
+ </TestForm>
103
69
  </QueryClientProvider>
104
70
  );
105
71