-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(input): form validation examples
- Loading branch information
Showing
10 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
apps/docs/content/components/input/built-in-validation.raw.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {Button, Form, Input} from "@nextui-org/react"; | ||
|
||
export default function App() { | ||
const [submitted, setSubmitted] = React.useState(null); | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); | ||
const data = Object.fromEntries(new FormData(e.currentTarget)); | ||
|
||
setSubmitted(data); | ||
}; | ||
|
||
return ( | ||
<Form className="w-full max-w-xs" validationBehavior="native" onSubmit={onSubmit}> | ||
<Input | ||
isRequired | ||
errorMessage={({validationDetails, validationErrors}) => { | ||
if (validationDetails.typeMismatch) { | ||
return "Please enter a valid email address"; | ||
} | ||
|
||
return validationErrors; | ||
}} | ||
label="Email" | ||
labelPlacement="outside" | ||
name="email" | ||
placeholder="Enter your email" | ||
type="email" | ||
/> | ||
<Button color="primary" type="submit"> | ||
Submit | ||
</Button> | ||
{submitted && ( | ||
<div className="text-small text-default-500"> | ||
You submitted: <code>{JSON.stringify(submitted)}</code> | ||
</div> | ||
)} | ||
</Form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import App from "./built-in-validation.raw.jsx?raw"; | ||
|
||
const react = { | ||
"/App.jsx": App, | ||
}; | ||
|
||
export default { | ||
...react, | ||
}; |
40 changes: 40 additions & 0 deletions
40
apps/docs/content/components/input/custom-validation.raw.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {Button, Form, Input} from "@nextui-org/react"; | ||
|
||
export default function App() { | ||
const [submitted, setSubmitted] = React.useState(null); | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); | ||
const data = Object.fromEntries(new FormData(e.currentTarget)); | ||
|
||
setSubmitted(data); | ||
}; | ||
|
||
return ( | ||
<Form className="w-full max-w-xs" validationBehavior="native" onSubmit={onSubmit}> | ||
<Input | ||
isRequired | ||
label="Username" | ||
labelPlacement="outside" | ||
name="username" | ||
placeholder="Enter your username" | ||
type="text" | ||
validate={(value) => { | ||
if (value.length < 3) { | ||
return "Username must be at least 3 characters long"; | ||
} | ||
|
||
return value === "admin" ? "Nice try!" : null; | ||
}} | ||
/> | ||
<Button color="primary" type="submit"> | ||
Submit | ||
</Button> | ||
{submitted && ( | ||
<div className="text-small text-default-500"> | ||
You submitted: <code>{JSON.stringify(submitted)}</code> | ||
</div> | ||
)} | ||
</Form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import App from "./custom-validation.raw.jsx?raw"; | ||
|
||
const react = { | ||
"/App.jsx": App, | ||
}; | ||
|
||
export default { | ||
...react, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
apps/docs/content/components/input/real-time-validation.raw.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {Button, Form, Input} from "@nextui-org/react"; | ||
|
||
export default function App() { | ||
const [submitted, setSubmitted] = React.useState(null); | ||
const [password, setPassword] = React.useState(""); | ||
const errors = []; | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); | ||
const data = Object.fromEntries(new FormData(e.currentTarget)); | ||
|
||
setSubmitted(data); | ||
}; | ||
|
||
if (password.length < 4) { | ||
errors.push("Password must be 4 characters or more."); | ||
} | ||
if ((password.match(/[A-Z]/g) || []).length < 1) { | ||
errors.push("Password must include at least 1 upper case letter"); | ||
} | ||
if ((password.match(/[^a-z0-9]/gi) || []).length < 1) { | ||
errors.push("Password must include at least 1 symbol."); | ||
} | ||
|
||
return ( | ||
<Form className="w-full max-w-xs" validationBehavior="native" onSubmit={onSubmit}> | ||
<Input | ||
errorMessage={() => ( | ||
<ul> | ||
{errors.map((error, i) => ( | ||
<li key={i}>{error}</li> | ||
))} | ||
</ul> | ||
)} | ||
isInvalid={errors.length > 0} | ||
label="Password" | ||
labelPlacement="outside" | ||
name="password" | ||
placeholder="Enter your password" | ||
value={password} | ||
onValueChange={setPassword} | ||
/> | ||
<Button color="primary" type="submit"> | ||
Submit | ||
</Button> | ||
{submitted && ( | ||
<div className="text-small text-default-500"> | ||
You submitted: <code>{JSON.stringify(submitted)}</code> | ||
</div> | ||
)} | ||
</Form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import App from "./real-time-validation.raw.jsx?raw"; | ||
|
||
const react = { | ||
"/App.jsx": App, | ||
}; | ||
|
||
export default { | ||
...react, | ||
}; |
49 changes: 49 additions & 0 deletions
49
apps/docs/content/components/input/server-validation.raw.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {Button, Form, Input} from "@nextui-org/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" | ||
validationBehavior="native" | ||
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.", | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import App from "./server-validation.raw.jsx?raw"; | ||
|
||
const react = { | ||
"/App.jsx": App, | ||
}; | ||
|
||
export default { | ||
...react, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters