@availity/mui-controlled-form 0.1.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.
- package/CHANGELOG.md +26 -0
- package/README.md +65 -0
- package/dist/index.d.mts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +71 -0
- package/dist/index.mjs +47 -0
- package/docs/propDefinitions.tsx +31 -0
- package/introduction.stories.mdx +7 -0
- package/jest.config.js +7 -0
- package/package.json +66 -0
- package/project.json +41 -0
- package/src/index.ts +1 -0
- package/src/lib/AsyncAutocomplete.stories.tsx +113 -0
- package/src/lib/AsyncAutocomplete.test.tsx +162 -0
- package/src/lib/AsyncAutocomplete.tsx +92 -0
- package/src/lib/Autocomplete.stories.tsx +60 -0
- package/src/lib/Autocomplete.test.tsx +70 -0
- package/src/lib/Autocomplete.tsx +96 -0
- package/src/lib/Checkbox.stories.tsx +67 -0
- package/src/lib/Checkbox.test.tsx +73 -0
- package/src/lib/Checkbox.tsx +37 -0
- package/src/lib/CodesAutocomplete.stories.tsx +79 -0
- package/src/lib/CodesAutocomplete.test.tsx +128 -0
- package/src/lib/CodesAutocomplete.tsx +76 -0
- package/src/lib/ControlledForm.stories.tsx +74 -0
- package/src/lib/ControlledForm.test.tsx +77 -0
- package/src/lib/ControlledForm.tsx +35 -0
- package/src/lib/Datepicker.stories.tsx +63 -0
- package/src/lib/Datepicker.test.tsx +73 -0
- package/src/lib/Datepicker.tsx +49 -0
- package/src/lib/Input.stories.tsx +60 -0
- package/src/lib/Input.test.tsx +98 -0
- package/src/lib/Input.tsx +54 -0
- package/src/lib/OrganizationAutocomplete.stories.tsx +77 -0
- package/src/lib/OrganizationAutocomplete.test.tsx +125 -0
- package/src/lib/OrganizationAutocomplete.tsx +75 -0
- package/src/lib/ProviderAutocomplete.stories.tsx +79 -0
- package/src/lib/ProviderAutocomplete.test.tsx +128 -0
- package/src/lib/ProviderAutocomplete.tsx +80 -0
- package/src/lib/RadioGroup.stories.tsx +63 -0
- package/src/lib/RadioGroup.test.tsx +66 -0
- package/src/lib/RadioGroup.tsx +68 -0
- package/src/lib/Select.stories.tsx +74 -0
- package/src/lib/Select.test.tsx +68 -0
- package/src/lib/Select.tsx +55 -0
- package/src/lib/TextField.stories.tsx +67 -0
- package/src/lib/TextField.test.tsx +99 -0
- package/src/lib/TextField.tsx +67 -0
- package/tsconfig.json +5 -0
- package/tsconfig.spec.json +10 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
|
+
|
|
5
|
+
## 0.1.0 (2025-01-23)
|
|
6
|
+
|
|
7
|
+
### Dependency Updates
|
|
8
|
+
|
|
9
|
+
* `mui-button` updated to version `0.1.0`
|
|
10
|
+
* `mui-layout` updated to version `0.1.0`
|
|
11
|
+
* `mui-menu` updated to version `0.1.0`
|
|
12
|
+
* `mui-paper` updated to version `0.1.0`
|
|
13
|
+
* `mui-typography` updated to version `0.1.0`
|
|
14
|
+
* `mui-autocomplete` updated to version `0.1.0`
|
|
15
|
+
* `mui-checkbox` updated to version `0.1.0`
|
|
16
|
+
* `mui-datepicker` updated to version `0.1.0`
|
|
17
|
+
* `mui-form-utils` updated to version `0.1.0`
|
|
18
|
+
* `mui-textfield` updated to version `0.1.0`
|
|
19
|
+
* `theme-provider` updated to version `0.1.0`
|
|
20
|
+
|
|
21
|
+
### Features
|
|
22
|
+
|
|
23
|
+
* **mui-controlled-form:** add ControlledDatepicker ([db89204](https://github.com/Availity/element/commit/db89204ac8c92bebcf1dae3f65be84718d87474a))
|
|
24
|
+
* **mui-controlled-form:** add yup resolver support ([16c16b9](https://github.com/Availity/element/commit/16c16b98743f8bae504a02709936cc252f4a82e1))
|
|
25
|
+
* **mui-controlled-form:** allow user to pass any validation resolver ([fa7daf3](https://github.com/Availity/element/commit/fa7daf3db9da8d02065cda3b89421064ea98f3bc))
|
|
26
|
+
* **mui-controlled-form:** initial commit ([827e8af](https://github.com/Availity/element/commit/827e8af9b0b29a912c08e2632f33cd6ee6708416))
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# @availity/mui-controlled-form
|
|
2
|
+
|
|
3
|
+
> Availity MUI Form components integrated with react-hook-form to be used with @availity/element design system.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@availity/mui-controlled-form)
|
|
6
|
+
[](https://www.npmjs.com/package/@availity/mui-controlled-form)
|
|
7
|
+
[](https://github.com/Availity/element/blob/main/packages/mui-controlled-form/package.json)
|
|
8
|
+
|
|
9
|
+
## Documentation
|
|
10
|
+
|
|
11
|
+
This package extends the Element's Form components with [react-hook-form](https://react-hook-form.com/). The goal of this package is to decrease the amount of setup involved with building a form using Element components and react-hook-form. If you would like to use another form library, the raw Element form components are also available.
|
|
12
|
+
|
|
13
|
+
Live demo and documentation in our [Storybook](https://availity.github.io/element/?path=/docs/form-components-controlled-form-introduction--docs)
|
|
14
|
+
|
|
15
|
+
Availity standards for design and usage can be found in the [Availity Design Guide](https://zeroheight.com/2e36e50c7)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
### Import Through @availity/element (Recommended)
|
|
20
|
+
|
|
21
|
+
#### NPM
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @availity/element
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
#### Yarn
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
yarn add @availity/element
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Direct Import
|
|
34
|
+
|
|
35
|
+
#### NPM
|
|
36
|
+
|
|
37
|
+
_This package has a few peer dependencies. Add `@mui/material` & `@emotion/react` to your project if not already installed._
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @availity/mui-controlled-form
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### Yarn
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
yarn add @availity/mui-controlled-form
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Usage
|
|
50
|
+
|
|
51
|
+
#### Import through @availity/element
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
import { ControlledForm } from '@availity/element';
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Direct import
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
import { ControlledForm } from '@availity/mui-controlled-form';
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Form Validation
|
|
64
|
+
|
|
65
|
+
Form validation can be on a component-by-component basis, or by passing a valid schema and schema resolver. For more information on react-hook-form's schema resolvers see: https://github.com/react-hook-form/resolvers#quickstart
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { FormHTMLAttributes } from 'react';
|
|
3
|
+
import { SubmitHandler, Resolver } from 'react-hook-form';
|
|
4
|
+
|
|
5
|
+
type ControlledFormProps = {
|
|
6
|
+
/** This function will receive the form data if form validation is successful. */
|
|
7
|
+
onSubmit: SubmitHandler<any>;
|
|
8
|
+
/** Reactive values to update the form values. */
|
|
9
|
+
values: Record<string, any>;
|
|
10
|
+
/** The schema used by the validationResolver. */
|
|
11
|
+
schema?: unknown;
|
|
12
|
+
/** Integrates with your preferred schema validation library.
|
|
13
|
+
* More information on react-hook-form's resolvers can be
|
|
14
|
+
* found here: https://github.com/react-hook-form/resolvers#quickstart
|
|
15
|
+
*/
|
|
16
|
+
validationResolver?: (schema: unknown) => Resolver;
|
|
17
|
+
} & FormHTMLAttributes<HTMLFormElement>;
|
|
18
|
+
declare const ControlledForm: ({ onSubmit, values, schema, validationResolver, ...rest }: ControlledFormProps) => react_jsx_runtime.JSX.Element;
|
|
19
|
+
|
|
20
|
+
export { ControlledForm, type ControlledFormProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { FormHTMLAttributes } from 'react';
|
|
3
|
+
import { SubmitHandler, Resolver } from 'react-hook-form';
|
|
4
|
+
|
|
5
|
+
type ControlledFormProps = {
|
|
6
|
+
/** This function will receive the form data if form validation is successful. */
|
|
7
|
+
onSubmit: SubmitHandler<any>;
|
|
8
|
+
/** Reactive values to update the form values. */
|
|
9
|
+
values: Record<string, any>;
|
|
10
|
+
/** The schema used by the validationResolver. */
|
|
11
|
+
schema?: unknown;
|
|
12
|
+
/** Integrates with your preferred schema validation library.
|
|
13
|
+
* More information on react-hook-form's resolvers can be
|
|
14
|
+
* found here: https://github.com/react-hook-form/resolvers#quickstart
|
|
15
|
+
*/
|
|
16
|
+
validationResolver?: (schema: unknown) => Resolver;
|
|
17
|
+
} & FormHTMLAttributes<HTMLFormElement>;
|
|
18
|
+
declare const ControlledForm: ({ onSubmit, values, schema, validationResolver, ...rest }: ControlledFormProps) => react_jsx_runtime.JSX.Element;
|
|
19
|
+
|
|
20
|
+
export { ControlledForm, type ControlledFormProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __objRest = (source, exclude) => {
|
|
24
|
+
var target = {};
|
|
25
|
+
for (var prop in source)
|
|
26
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
27
|
+
target[prop] = source[prop];
|
|
28
|
+
if (source != null && __getOwnPropSymbols)
|
|
29
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
30
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
31
|
+
target[prop] = source[prop];
|
|
32
|
+
}
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
35
|
+
var __export = (target, all) => {
|
|
36
|
+
for (var name in all)
|
|
37
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
38
|
+
};
|
|
39
|
+
var __copyProps = (to, from, except, desc) => {
|
|
40
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
41
|
+
for (let key of __getOwnPropNames(from))
|
|
42
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
43
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
44
|
+
}
|
|
45
|
+
return to;
|
|
46
|
+
};
|
|
47
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
48
|
+
|
|
49
|
+
// src/index.ts
|
|
50
|
+
var src_exports = {};
|
|
51
|
+
__export(src_exports, {
|
|
52
|
+
ControlledForm: () => ControlledForm
|
|
53
|
+
});
|
|
54
|
+
module.exports = __toCommonJS(src_exports);
|
|
55
|
+
|
|
56
|
+
// src/lib/ControlledForm.tsx
|
|
57
|
+
var import_react_hook_form = require("react-hook-form");
|
|
58
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
59
|
+
var ControlledForm = (_a) => {
|
|
60
|
+
var _b = _a, { onSubmit, values, schema, validationResolver } = _b, rest = __objRest(_b, ["onSubmit", "values", "schema", "validationResolver"]);
|
|
61
|
+
const useFormOptions = { values };
|
|
62
|
+
if (schema && validationResolver) {
|
|
63
|
+
useFormOptions.resolver = validationResolver(schema);
|
|
64
|
+
}
|
|
65
|
+
const methods = (0, import_react_hook_form.useForm)(useFormOptions);
|
|
66
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_hook_form.FormProvider, __spreadProps(__spreadValues({}, methods), { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("form", __spreadValues({ onSubmit: methods.handleSubmit(onSubmit) }, rest)) }));
|
|
67
|
+
};
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
ControlledForm
|
|
71
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/lib/ControlledForm.tsx
|
|
34
|
+
import { useForm, FormProvider } from "react-hook-form";
|
|
35
|
+
import { jsx } from "react/jsx-runtime";
|
|
36
|
+
var ControlledForm = (_a) => {
|
|
37
|
+
var _b = _a, { onSubmit, values, schema, validationResolver } = _b, rest = __objRest(_b, ["onSubmit", "values", "schema", "validationResolver"]);
|
|
38
|
+
const useFormOptions = { values };
|
|
39
|
+
if (schema && validationResolver) {
|
|
40
|
+
useFormOptions.resolver = validationResolver(schema);
|
|
41
|
+
}
|
|
42
|
+
const methods = useForm(useFormOptions);
|
|
43
|
+
return /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, methods), { children: /* @__PURE__ */ jsx("form", __spreadValues({ onSubmit: methods.handleSubmit(onSubmit) }, rest)) }));
|
|
44
|
+
};
|
|
45
|
+
export {
|
|
46
|
+
ControlledForm
|
|
47
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const missingRHFprops = {
|
|
2
|
+
name: {
|
|
3
|
+
description: 'Unique name of your input.',
|
|
4
|
+
},
|
|
5
|
+
deps: {
|
|
6
|
+
description: 'Validation will be triggered for the dependent inputs, it only limited to register api not trigger.',
|
|
7
|
+
},
|
|
8
|
+
max: {
|
|
9
|
+
description: 'The maximum value to accept for this input.',
|
|
10
|
+
},
|
|
11
|
+
maxLength: {
|
|
12
|
+
description: 'The maximum length of the value to accept for this input.',
|
|
13
|
+
},
|
|
14
|
+
min: {
|
|
15
|
+
description: 'The minimum value to accept for this input.',
|
|
16
|
+
},
|
|
17
|
+
minLength: {
|
|
18
|
+
description: 'The minimum length of the value to accept for this input.',
|
|
19
|
+
},
|
|
20
|
+
pattern: {
|
|
21
|
+
description: 'The regex pattern for the input.',
|
|
22
|
+
},
|
|
23
|
+
shouldUnregister: {
|
|
24
|
+
description:
|
|
25
|
+
'Input will be unregistered after unmount and defaultValues will be removed as well. Note: this prop should be avoided when using with useFieldArray as unregister function gets called after input unmount/remount and reorder.',
|
|
26
|
+
},
|
|
27
|
+
required: {
|
|
28
|
+
description:
|
|
29
|
+
'A Boolean which, if true, indicates that the input must have a value before the form can be submitted. You can assign a string to return an error message in the errors object. Note: This config aligns with web constrained API for required input validation, for object or array type of input use validate function instead.',
|
|
30
|
+
},
|
|
31
|
+
};
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@availity/mui-controlled-form",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Availity MUI ControlledForm Component - part of the @availity/element design system",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"typescript",
|
|
8
|
+
"availity",
|
|
9
|
+
"mui"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://availity.github.io/element/?path=/docs/components-controlled-form-introduction--docs",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/Availity/element/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/Availity/element.git",
|
|
18
|
+
"directory": "packages/controlled-form"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Availity Developers <AVOSS@availity.com>",
|
|
22
|
+
"browser": "./dist/index.js",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
28
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
29
|
+
"clean": "rm -rf dist",
|
|
30
|
+
"clean:nm": "rm -rf node_modules",
|
|
31
|
+
"publish": "yarn npm publish --tolerate-republish --access public",
|
|
32
|
+
"publish:canary": "yarn npm publish --access public --tag canary"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@availity/mui-autocomplete": "^0.9.9",
|
|
36
|
+
"@availity/mui-checkbox": "^0.2.5",
|
|
37
|
+
"@availity/mui-datepicker": "^0.5.0",
|
|
38
|
+
"@availity/mui-form-utils": "^0.16.4",
|
|
39
|
+
"@availity/mui-textfield": "^0.6.18",
|
|
40
|
+
"react-hook-form": "^7.51.3"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@availity/api-axios": "^9.0.6",
|
|
44
|
+
"@availity/mui-button": "^0.6.15",
|
|
45
|
+
"@availity/mui-layout": "^0.2.0",
|
|
46
|
+
"@availity/mui-menu": "^0.2.15",
|
|
47
|
+
"@availity/mui-paper": "^0.1.9",
|
|
48
|
+
"@availity/mui-typography": "^0.2.1",
|
|
49
|
+
"@hookform/resolvers": "^3.10.0",
|
|
50
|
+
"@mui/material": "^5.15.15",
|
|
51
|
+
"@tanstack/react-query": "^4.36.1",
|
|
52
|
+
"dayjs": "^1.11.13",
|
|
53
|
+
"react": "18.2.0",
|
|
54
|
+
"react-dom": "18.2.0",
|
|
55
|
+
"tsup": "^8.0.2",
|
|
56
|
+
"typescript": "^5.4.5",
|
|
57
|
+
"yup": "^1.6.1"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"@mui/material": "^5.11.9",
|
|
61
|
+
"react": ">=16.3.0"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
}
|
|
66
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mui-controlled-form",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/controlled-form/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"tags": [],
|
|
7
|
+
"targets": {
|
|
8
|
+
"lint": {
|
|
9
|
+
"executor": "@nx/eslint:lint",
|
|
10
|
+
"options": {
|
|
11
|
+
"eslintConfig": ".eslintrc.json",
|
|
12
|
+
"silent": false,
|
|
13
|
+
"fix": false,
|
|
14
|
+
"cache": true,
|
|
15
|
+
"cacheLocation": "./node_modules/.cache/controlled-form/.eslintcache",
|
|
16
|
+
"maxWarnings": -1,
|
|
17
|
+
"quiet": false,
|
|
18
|
+
"noEslintrc": false,
|
|
19
|
+
"hasTypeAwareRules": true,
|
|
20
|
+
"cacheStrategy": "metadata"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"test": {
|
|
24
|
+
"executor": "@nx/jest:jest",
|
|
25
|
+
"outputs": ["{workspaceRoot}/coverage/controlled-form"],
|
|
26
|
+
"options": {
|
|
27
|
+
"jestConfig": "packages/controlled-form/jest.config.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"version": {
|
|
31
|
+
"executor": "@jscutlery/semver:version",
|
|
32
|
+
"options": {
|
|
33
|
+
"preset": "conventional",
|
|
34
|
+
"commitMessageFormat": "chore({projectName}): release version ${version} [skip ci]",
|
|
35
|
+
"tagPrefix": "@availity/{projectName}@",
|
|
36
|
+
"trackDeps": true,
|
|
37
|
+
"skipCommitTypes": ["docs"]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib/ControlledForm';
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { ControlledAsyncAutocomplete } from './AsyncAutocomplete';
|
|
3
|
+
import { ControlledForm } from './ControlledForm';
|
|
4
|
+
import { Button } from '@availity/mui-button';
|
|
5
|
+
import { useFormContext } from 'react-hook-form';
|
|
6
|
+
import { Paper } from '@availity/mui-paper';
|
|
7
|
+
import { Typography } from '@availity/mui-typography';
|
|
8
|
+
import { Grid } from '@availity/mui-layout';
|
|
9
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
10
|
+
import AvApi, { ApiConfig } from '@availity/api-axios';
|
|
11
|
+
import { missingRHFprops } from '../../docs/propDefinitions';
|
|
12
|
+
|
|
13
|
+
const meta: Meta<typeof ControlledAsyncAutocomplete> = {
|
|
14
|
+
title: 'Form Components/Controlled Form/Autocomplete/ControlledAsyncAutocomplete',
|
|
15
|
+
component: ControlledAsyncAutocomplete,
|
|
16
|
+
tags: ['autodocs'],
|
|
17
|
+
argTypes: missingRHFprops,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default meta;
|
|
21
|
+
|
|
22
|
+
const api = new AvApi({ name: 'example' } as ApiConfig);
|
|
23
|
+
|
|
24
|
+
type Option = {
|
|
25
|
+
label: string;
|
|
26
|
+
value: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type ExampleResponse = {
|
|
30
|
+
totalCount: number;
|
|
31
|
+
options: Option[];
|
|
32
|
+
count: number;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const getResults = async (offset: number, limit: number) => {
|
|
36
|
+
// const offset = page * limit;
|
|
37
|
+
const resp = await api.post<ExampleResponse>({ offset, limit }, { params: {} });
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
totalCount: resp.data.totalCount,
|
|
41
|
+
offset,
|
|
42
|
+
limit,
|
|
43
|
+
options: resp.data.options,
|
|
44
|
+
count: resp.data.count,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const loadOptions = async (offset: number, limit: number) => {
|
|
49
|
+
const { options, totalCount } = await getResults(offset, limit);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
options,
|
|
53
|
+
hasMore: offset + limit < totalCount,
|
|
54
|
+
offset,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const client = new QueryClient({
|
|
59
|
+
defaultOptions: {
|
|
60
|
+
queries: {
|
|
61
|
+
refetchOnWindowFocus: false,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
export const _ControlledAsyncAutoComplete: StoryObj<typeof ControlledAsyncAutocomplete> = {
|
|
67
|
+
render: (args) => {
|
|
68
|
+
const SubmittedValues = () => {
|
|
69
|
+
const {
|
|
70
|
+
getValues,
|
|
71
|
+
formState: { isSubmitSuccessful },
|
|
72
|
+
} = useFormContext();
|
|
73
|
+
|
|
74
|
+
return isSubmitSuccessful ? (
|
|
75
|
+
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
76
|
+
<Typography variant="h2">Submitted Values</Typography>
|
|
77
|
+
<pre>{JSON.stringify(getValues(), null, 2)}</pre>
|
|
78
|
+
</Paper>
|
|
79
|
+
) : null;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const Actions = () => {
|
|
83
|
+
const {
|
|
84
|
+
reset,
|
|
85
|
+
formState: { isSubmitSuccessful },
|
|
86
|
+
} = useFormContext();
|
|
87
|
+
return (
|
|
88
|
+
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
89
|
+
<Button disabled={!isSubmitSuccessful} children="Reset" color="secondary" onClick={() => reset()} />
|
|
90
|
+
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
91
|
+
</Grid>
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
return (
|
|
95
|
+
<QueryClientProvider client={client}>
|
|
96
|
+
<ControlledForm values={{ controlledAutocomplete: undefined }} onSubmit={(data) => data}>
|
|
97
|
+
<ControlledAsyncAutocomplete {...args} />
|
|
98
|
+
<Actions />
|
|
99
|
+
<SubmittedValues />
|
|
100
|
+
</ControlledForm>
|
|
101
|
+
</QueryClientProvider>
|
|
102
|
+
);
|
|
103
|
+
},
|
|
104
|
+
args: {
|
|
105
|
+
name: 'controlledAsyncAutocomplete',
|
|
106
|
+
FieldProps: { label: 'Async Select', helperText: 'Helper Text', fullWidth: false },
|
|
107
|
+
getOptionLabel: (val: Option) => val.label,
|
|
108
|
+
loadOptions,
|
|
109
|
+
limit: 10,
|
|
110
|
+
queryKey: 'example',
|
|
111
|
+
required: 'This is required.',
|
|
112
|
+
},
|
|
113
|
+
};
|