nextui/apps/docs/content/components/input/server-validation.raw.jsx
Peterl561 a66476d60c
fix(form): use native as default validation behavior (#4425)
* fix(form): use native as default validation behavior

* docs(form): delete explicit validationBehavior=native

* test(form): adjusted form test validation behaviors

* chore(form): adjusted stories with forms

* chore(changeset): changed form default validation behavior to native

* chore(changeset): removed packages with only test changes

* chore(changeset): change to patch

* chore(changeset): update package name

* refactor(docs): update package name

* refactor(docs): update to heroui

---------

Co-authored-by: աӄա <wingkwong.code@gmail.com>
2025-01-30 10:54:56 -03:00

45 lines
1.1 KiB
JavaScript

import {Button, Form, Input} from "@heroui/react";
export default function App() {
const [isLoading, setIsLoading] = React.useState(false);
const [errors, setErrors] = React.useState({});
const onSubmit = async (e) => {
e.preventDefault();
setIsLoading(true);
const data = Object.fromEntries(new FormData(e.currentTarget));
const result = await callServer(data);
setErrors(result.errors);
setIsLoading(false);
};
return (
<Form className="w-full max-w-xs" validationErrors={errors} onSubmit={onSubmit}>
<Input
isRequired
isDisabled={isLoading}
label="Username"
labelPlacement="outside"
name="username"
placeholder="Enter your username"
/>
<Button color="primary" isLoading={isLoading} type="submit">
Submit
</Button>
</Form>
);
}
// Fake server used in this example.
async function callServer(_) {
await new Promise((resolve) => setTimeout(resolve, 500));
return {
errors: {
username: "Sorry, this username is taken.",
},
};
}