Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/react hook form components #630

Merged
merged 29 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b9eba2c
fix(mui-autocomplete): export the CodesAutocomplete component
gregmartDOTin Jan 8, 2025
827e8af
feat(mui-controlled-form): initial commit
gregmartDOTin Jan 8, 2025
11f273d
docs(mui-controlled-form): add TextField and RadioGroup stories
gregmartDOTin Jan 9, 2025
bb4bde0
Merge branch 'main' of github.com:Availity/element into feat/react-ho…
gregmartDOTin Jan 9, 2025
ee9e14c
refactor(mui-controlled-form): change register to Controller in Autoc…
gregmartDOTin Jan 9, 2025
2cafb50
docs(mui-controlled-form): add story for ControlledCheckbox
gregmartDOTin Jan 9, 2025
eef7916
docs(mui-controlled-form): add story for ControlledInput
gregmartDOTin Jan 9, 2025
058ce2c
refactor(mui-controlled-form): change register to Controller in Async…
gregmartDOTin Jan 15, 2025
4b7f380
refactor(mui-controlled-form): change register to Controller in Codes…
gregmartDOTin Jan 15, 2025
75adf50
refactor(mui-controlled-form): change register to Controller in Organ…
gregmartDOTin Jan 15, 2025
299964e
refactor(mui-controlled-form): change register to Controller in Provi…
gregmartDOTin Jan 15, 2025
ccfde75
docs(mui-controlled-form): add story for Select
gregmartDOTin Jan 15, 2025
79cc4da
test(mui-controlled-form): add tests for AsyncAutocomplete
gregmartDOTin Jan 16, 2025
3ee9b8c
test(mui-controlled-form): add tests for Autocomplete
gregmartDOTin Jan 16, 2025
7ded2b6
refactor(mui-controlled-form): allow passing register options as top …
gregmartDOTin Jan 16, 2025
902f568
test(mui-controlled-form): add tests for CodesAutocomplete
gregmartDOTin Jan 16, 2025
db89204
feat(mui-controlled-form): add ControlledDatepicker
gregmartDOTin Jan 17, 2025
9c0841e
refactor(mui-controlled-form): update props on ControlledInput
gregmartDOTin Jan 17, 2025
aec3a7c
refactor(mui-controlled-form): update props on all Autocompletes
gregmartDOTin Jan 17, 2025
7216241
refactor(mui-controlled-form): update props on Select
gregmartDOTin Jan 17, 2025
c2515e6
refactor(mui-controlled-form): update props on TextField
gregmartDOTin Jan 17, 2025
e6581f8
refactor(mui-controlled-form): update props on RadioGroup
gregmartDOTin Jan 21, 2025
d70c450
docs(mui-controlled-form): clean up stories
gregmartDOTin Jan 21, 2025
16c16b9
feat(mui-controlled-form): add yup resolver support
gregmartDOTin Jan 21, 2025
21e07d6
test(mui-controlled-form): cleanup ControlledForm tests
gregmartDOTin Jan 21, 2025
7d254f7
docs(mui-controlled-form): move stories to Form Components section
gregmartDOTin Jan 22, 2025
4bfdc95
docs(mui-controlled-form): increase timeout for Datepicker test
gregmartDOTin Jan 22, 2025
fa7daf3
feat(mui-controlled-form): allow user to pass any validation resolver
gregmartDOTin Jan 23, 2025
1d65fa3
docs(mui-controlled-form): update README to reflect react-hook-form u…
gregmartDOTin Jan 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/autocomplete/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@mui/types": "^7.2.14"
},
"devDependencies": {
"@availity/api-axios": "^9.0.4",
"@availity/api-axios": "^9.0.6",
"@availity/mui-form-utils": "workspace:^",
"@availity/mui-textfield": "workspace:^",
"@mui/material": "^5.15.15",
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './lib/Autocomplete';
export * from './lib/AsyncAutocomplete';
export * from './lib/CodesAutocomplete';
export * from './lib/OrganizationAutocomplete';
export * from './lib/ProviderAutocomplete';
1 change: 1 addition & 0 deletions packages/autocomplete/src/lib/CodesAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface CodesAutocompleteProps<
Optional<AsyncAutocompleteProps<Option, Multiple, DisableClearable, FreeSolo, ChipComponent>, 'queryKey'>,
'loadOptions'
> {
/** The code list id. */
list: string;
/** Config passed to the AvCodesApi.query function */
apiConfig?: ApiConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface OrgAutocompleteProps<
Optional<AsyncAutocompleteProps<Option, Multiple, DisableClearable, FreeSolo, ChipComponent>, 'queryKey'>,
'loadOptions'
> {
/** Axios ApiConfig */
apiConfig?: ApiConfig;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/avatar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
"access": "public"
},
"dependencies": {
"@availity/api-axios": "^9.0.4"
"@availity/api-axios": "^9.0.6"
}
}
Empty file.
61 changes: 61 additions & 0 deletions packages/controlled-form/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# @availity/mui-controlled-form

> Availity MUI ControlledForm component to be used with @availity/element design system.

[![Version](https://img.shields.io/npm/v/@availity/mui-controlled-form.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/mui-controlled-form)
[![NPM Downloads](https://img.shields.io/npm/dt/@availity/mui-controlled-form.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/mui-controlled-form)
[![Dependency Status](https://img.shields.io/librariesio/release/npm/@availity/mui-controlled-form?style=for-the-badge)](https://github.com/Availity/element/blob/main/packages/mui-controlled-form/package.json)

## Documentation

This package extends the MUI ControlledForm component: [MUI ControlledForm Docs](https://mui.com/components/controlled-form/)

Live demo and documentation in our [Storybook](https://availity.github.io/element/?path=/docs/components-controlled-form-introduction--docs)

Availity standards for design and usage can be found in the [Availity Design Guide](https://zeroheight.com/2e36e50c7)

## Installation

### Import Through @availity/element (Recommended)

#### NPM

```bash
npm install @availity/element
```

#### Yarn

```bash
yarn add @availity/element
```

### Direct Import

#### NPM

_This package has a few peer dependencies. Add `@mui/material` & `@emotion/react` to your project if not already installed._

```bash
npm install @availity/mui-controlled-form
```

#### Yarn

```bash
yarn add @availity/mui-controlled-form
```

### Usage

#### Import through @availity/element

```tsx
import { ControlledForm } from '@availity/element';
```

#### Direct import

```tsx
import { ControlledForm } from '@availity/mui-controlled-form';
```
31 changes: 31 additions & 0 deletions packages/controlled-form/docs/propDefinitions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const missingRHFprops = {
name: {
description: 'Unique name of your input.',
},
deps: {
description: 'Validation will be triggered for the dependent inputs, it only limited to register api not trigger.',
},
max: {
description: 'The maximum value to accept for this input.',
},
maxLength: {
description: 'The maximum length of the value to accept for this input.',
},
min: {
description: 'The minimum value to accept for this input.',
},
minLength: {
description: 'The minimum length of the value to accept for this input.',
},
pattern: {
description: 'The regex pattern for the input.',
},
shouldUnregister: {
description:
'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.',
},
required: {
description:
'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.',
},
};
7 changes: 7 additions & 0 deletions packages/controlled-form/introduction.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Markdown } from '@storybook/blocks';
import { Meta } from '@storybook/addon-docs';
import ReadMe from './README.md?raw';

<Meta title="Form Components/Controlled Form/Introduction" />

<Markdown>{ReadMe}</Markdown>
7 changes: 7 additions & 0 deletions packages/controlled-form/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const global = require('../../jest.config.global');

module.exports = {
...global,
displayName: 'controlled-form',
coverageDirectory: '../../coverage/controlled-form',
};
66 changes: 66 additions & 0 deletions packages/controlled-form/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@availity/mui-controlled-form",
"version": "0.0.0",
"description": "Availity MUI ControlledForm Component - part of the @availity/element design system",
"keywords": [
"react",
"typescript",
"availity",
"mui"
],
"homepage": "https://availity.github.io/element/?path=/docs/components-controlled-form-introduction--docs",
"bugs": {
"url": "https://github.com/Availity/element/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/Availity/element.git",
"directory": "packages/controlled-form"
},
"license": "MIT",
"author": "Availity Developers <[email protected]>",
"browser": "./dist/index.js",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --format esm,cjs --dts",
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
"clean": "rm -rf dist",
"clean:nm": "rm -rf node_modules",
"publish": "yarn npm publish --tolerate-republish --access public",
"publish:canary": "yarn npm publish --access public --tag canary"
},
"dependencies": {
"@availity/mui-autocomplete": "workspace:^",
"@availity/mui-checkbox": "workspace:^",
"@availity/mui-datepicker": "workspace:^",
"@availity/mui-form-utils": "workspace:^",
"@availity/mui-textfield": "workspace:^",
"@hookform/resolvers": "^3.10.0",
"react-hook-form": "^7.51.3",
"yup": "^1.6.1"
},
"devDependencies": {
"@availity/api-axios": "^9.0.6",
"@availity/mui-button": "workspace:^",
"@availity/mui-layout": "workspace:^",
"@availity/mui-menu": "workspace:^",
"@availity/mui-paper": "workspace:^",
"@availity/mui-typography": "workspace:^",
"@mui/material": "^5.15.15",
"@tanstack/react-query": "^4.36.1",
"dayjs": "^1.11.13",
"react": "18.2.0",
"react-dom": "18.2.0",
"tsup": "^8.0.2",
"typescript": "^5.4.5"
},
"peerDependencies": {
"@mui/material": "^5.11.9",
"react": ">=16.3.0"
},
"publishConfig": {
"access": "public"
}
}
41 changes: 41 additions & 0 deletions packages/controlled-form/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "mui-controlled-form",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/controlled-form/src",
"projectType": "library",
"tags": [],
"targets": {
"lint": {
"executor": "@nx/eslint:lint",
"options": {
"eslintConfig": ".eslintrc.json",
"silent": false,
"fix": false,
"cache": true,
"cacheLocation": "./node_modules/.cache/controlled-form/.eslintcache",
"maxWarnings": -1,
"quiet": false,
"noEslintrc": false,
"hasTypeAwareRules": true,
"cacheStrategy": "metadata"
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/controlled-form"],
"options": {
"jestConfig": "packages/controlled-form/jest.config.js"
}
},
"version": {
"executor": "@jscutlery/semver:version",
"options": {
"preset": "conventional",
"commitMessageFormat": "chore({projectName}): release version ${version} [skip ci]",
"tagPrefix": "@availity/{projectName}@",
"trackDeps": true,
"skipCommitTypes": ["docs"]
}
}
}
}
1 change: 1 addition & 0 deletions packages/controlled-form/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/ControlledForm';
113 changes: 113 additions & 0 deletions packages/controlled-form/src/lib/AsyncAutocomplete.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import type { Meta, StoryObj } from '@storybook/react';
import { ControlledAsyncAutocomplete } from './AsyncAutocomplete';
import { ControlledForm } from './ControlledForm';
import { Button } from '@availity/mui-button';
import { useFormContext } from 'react-hook-form';
import { Paper } from '@availity/mui-paper';
import { Typography } from '@availity/mui-typography';
import { Grid } from '@availity/mui-layout';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import AvApi, { ApiConfig } from '@availity/api-axios';
import { missingRHFprops } from '../../docs/propDefinitions';

const meta: Meta<typeof ControlledAsyncAutocomplete> = {
title: 'Form Components/Controlled Form/Autocomplete/ControlledAsyncAutocomplete',
component: ControlledAsyncAutocomplete,
tags: ['autodocs'],
argTypes: missingRHFprops,
};

export default meta;

const api = new AvApi({ name: 'example' } as ApiConfig);

type Option = {
label: string;
value: number;
};

type ExampleResponse = {
totalCount: number;
options: Option[];
count: number;
};

const getResults = async (offset: number, limit: number) => {
// const offset = page * limit;
const resp = await api.post<ExampleResponse>({ offset, limit }, { params: {} });

return {
totalCount: resp.data.totalCount,
offset,
limit,
options: resp.data.options,
count: resp.data.count,
};
};

const loadOptions = async (offset: number, limit: number) => {
const { options, totalCount } = await getResults(offset, limit);

return {
options,
hasMore: offset + limit < totalCount,
offset,
};
};

const client = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
},
},
});

export const _ControlledAsyncAutoComplete: StoryObj<typeof ControlledAsyncAutocomplete> = {
render: (args) => {
const SubmittedValues = () => {
const {
getValues,
formState: { isSubmitSuccessful },
} = useFormContext();

return isSubmitSuccessful ? (
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
<Typography variant="h2">Submitted Values</Typography>
<pre>{JSON.stringify(getValues(), null, 2)}</pre>
</Paper>
) : null;
};

const Actions = () => {
const {
reset,
formState: { isSubmitSuccessful },
} = useFormContext();
return (
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
<Button disabled={!isSubmitSuccessful} children="Reset" color="secondary" onClick={() => reset()} />
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
</Grid>
);
};
return (
<QueryClientProvider client={client}>
<ControlledForm values={{ controlledAutocomplete: undefined }} onSubmit={(data) => data}>
<ControlledAsyncAutocomplete {...args} />
<Actions />
<SubmittedValues />
</ControlledForm>
</QueryClientProvider>
);
},
args: {
name: 'controlledAsyncAutocomplete',
FieldProps: { label: 'Async Select', helperText: 'Helper Text', fullWidth: false },
getOptionLabel: (val: Option) => val.label,
loadOptions,
limit: 10,
queryKey: 'example',
required: 'This is required.',
},
};
Loading
Loading