This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
react-component-form/example/index.tsx

39 lines
953 B
TypeScript
Raw Normal View History

import { createRoot } from 'react-dom/client'
2022-08-26 00:30:54 +02:00
import React from 'react'
import { Form, useForm } from 'react-component-form'
import type { HandleUseFormCallback } from 'react-component-form'
const schema = {
inputName: {
type: 'string',
minLength: 3,
maxLength: 20
}
}
2020-08-04 16:22:31 +02:00
2022-08-26 00:30:54 +02:00
export const Example = () => {
const { errors, handleUseForm } = useForm(schema)
2020-08-04 16:22:31 +02:00
2022-08-26 00:30:54 +02:00
const onSubmit: HandleUseFormCallback<typeof schema> = (
formData,
formElement
) => {
console.log(formData) // { inputName: 'value of the input validated' }
2020-08-04 16:22:31 +02:00
formElement.reset()
2022-08-26 00:30:54 +02:00
return null
2020-08-04 16:22:31 +02:00
}
return (
2022-08-26 00:30:54 +02:00
<Form onSubmit={handleUseForm(onSubmit)}>
<input type='text' name='inputName' />
{errors.inputName != null && <p>{errors.inputName[0].message}</p>}
2020-08-04 16:22:31 +02:00
2022-08-26 00:30:54 +02:00
<button type='submit'>Submit</button>
</Form>
2020-08-04 16:22:31 +02:00
)
}
const container = document.getElementById('root') as HTMLElement
const root = createRoot(container)
2022-08-26 00:30:54 +02:00
root.render(<Example />)