Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/arkemishub/form

Flexible and extensible form
https://github.com/arkemishub/form

arke form

Last synced: 19 days ago
JSON representation

Flexible and extensible form

Awesome Lists containing this project

README

        

# @arkejs/form

![Form](https://github.com/arkemishub/form/assets/81776297/7fb75374-a210-4c21-8337-b3504cc82f00)

[![License](https://img.shields.io/badge/license-Apache2.0-blue.svg)](https://github.com/arkemishub/arke-monorepo/blob/master/LICENSE.txt)

Form component to automate form generation process

## Usage

First of all, you need to install the library:

```shell
npm install @arkejs/form
pnpm install @arkejs/form
```

You can create a FormProvider to associate automatically a component from field type:

```tsx
import { FormConfigProvider } from '@arkejs/form'

function Application() {
return (
( field.onChange(e.target.value)} />),
string: ({field}) => ( field.onChange(e.target.value)} />)
}}
>
...

)
}
```

Then you're able to import the Form and the FormField components:

```tsx
import { FormConfigProvider, Form, FormField } from '@arkejs/form'

function Application() {
return (

setData(values)}
onChange={(values) => console.log(values)}
>




(

)}
/>



)
}
```

You can also use the Form without the general FormConfigProvider and use the `components` props to define the component
by field type

```tsx
import { Form, FormField } from '@arkejs/form'

function Application() {
return (
setData(values)}
onChange={(values) => console.log(values)}
// Define here the components
components={{
boolean: ({field}) => (),
string: ({field}) => ()
}}
>






)
}
```

## Manage the internal state

If you need to use the internal form state or useful functionalities, as looks the value of one field or reset the form
state, you can use the `useForm` hook.

The methods object is based on `react-hook-form` library, to understand all functionalities look the [React Hook Form Documentation](https://www.react-hook-form.com/)

```tsx
import { Form, FormField } from '@arkejs/form'

function Application() {
const { formProps, methods } = useForm();
const { watch, reset } = methods;
const nameValue = watch('name');
return (
setData(values)}
>



{nameValue.length > 0 &&}

reset()}>Reset fields

)
}
```

## Define the DefaultValues

If you need to update the default values after first render you can pass fields directly on `useForm`:

```tsx
import { Form, FormField } from '@arkejs/form'

async function Application() {
const responseFields = await axios.get('/fields')
const { formProps } = useForm({
fields: responseFields.data,
});
return(
setData(values)}
>
...

)
}
```