-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
chore: Add form
example
#966
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,28 @@ | ||
import { | ||
Form, | ||
FormError, | ||
FormInput, | ||
FormLabel, | ||
FormSubmit, | ||
useFormState, | ||
} from "ariakit/form"; | ||
import "./style.css"; | ||
|
||
export default function Example() { | ||
const form = useFormState({ defaultValues: { name: "" } }); | ||
|
||
form.useSubmit(() => { | ||
alert(JSON.stringify(form.values)); | ||
}); | ||
|
||
return ( | ||
<Form state={form} className="form"> | ||
<div className="field"> | ||
<FormLabel name={form.names.name}>Name</FormLabel> | ||
<FormInput name={form.names.name} required placeholder="John Doe" /> | ||
<FormError name={form.names.name} className="error" /> | ||
</div> | ||
<FormSubmit className="button">Submit</FormSubmit> | ||
</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,41 @@ | ||
@import url("../../../button/__examples__/button/style.css"); | ||
|
||
.form { | ||
@apply flex flex-col gap-4; | ||
} | ||
|
||
.field { | ||
@apply flex flex-col gap-2; | ||
} | ||
|
||
.field input { | ||
width: 250px; | ||
@apply | ||
border-none | ||
px-4 | ||
h-10 | ||
rounded-lg | ||
text-base | ||
outline-none | ||
bg-canvas-1 | ||
text-canvas-1 | ||
hover:bg-canvas-1-hover | ||
dark:bg-canvas-1-dark | ||
dark:text-canvas-1-dark | ||
dark:hover:bg-canvas-1-dark-hover | ||
ariakit-focus-visible:ariakit-outline; | ||
} | ||
|
||
.error:not(:empty) { | ||
@apply | ||
py-2 | ||
px-4 | ||
border | ||
rounded-lg | ||
text-danger-1 | ||
border-danger-1 | ||
bg-danger-1 | ||
dark:border-danger-1-dark | ||
dark:bg-danger-1-dark | ||
dark:text-danger-1-dark; | ||
} |
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,130 @@ | ||
import { | ||
click, | ||
getByRole, | ||
press, | ||
queryByText, | ||
render, | ||
type, | ||
} from "ariakit-test-utils"; | ||
import { axe } from "jest-axe"; | ||
import Example from "."; | ||
|
||
const getInput = (name: string) => getByRole("textbox", { name }); | ||
const getError = () => queryByText("Constraints not satisfied"); | ||
|
||
const spyOnAlert = () => | ||
jest.spyOn(window, "alert").mockImplementation(() => {}); | ||
|
||
let alert = spyOnAlert(); | ||
|
||
beforeEach(() => { | ||
alert = spyOnAlert(); | ||
}); | ||
|
||
afterEach(() => { | ||
alert.mockClear(); | ||
}); | ||
|
||
test("a11y", async () => { | ||
const { container } = render(<Example />); | ||
expect(await axe(container)).toHaveNoViolations(); | ||
}); | ||
|
||
test("focus on the first input by tabbing", async () => { | ||
render(<Example />); | ||
expect(getInput("Name")).not.toHaveFocus(); | ||
await press.Tab(); | ||
expect(getInput("Name")).toHaveFocus(); | ||
}); | ||
|
||
test("show error on blur", async () => { | ||
render(<Example />); | ||
expect(getError()).not.toBeInTheDocument(); | ||
await press.Tab(); | ||
await press.Tab(); | ||
expect(getError()).toBeInTheDocument(); | ||
}); | ||
|
||
test("show error on submit", async () => { | ||
render(<Example />); | ||
await press.Tab(); | ||
expect(getError()).not.toBeInTheDocument(); | ||
await press.Enter(); | ||
expect(getError()).toBeInTheDocument(); | ||
}); | ||
|
||
test("focus on input with error on submit", async () => { | ||
render(<Example />); | ||
await click(getByRole("button")); | ||
expect(getInput("Name")).toHaveFocus(); | ||
}); | ||
|
||
test("fix error on change", async () => { | ||
render(<Example />); | ||
await press.Tab(); | ||
await press.Tab(); | ||
await press.ShiftTab(); | ||
expect(getError()).toBeInTheDocument(); | ||
await type("John"); | ||
expect(getError()).not.toBeInTheDocument(); | ||
}); | ||
|
||
test("submit form", async () => { | ||
render(<Example />); | ||
await press.Tab(); | ||
await type("John"); | ||
await press.Enter(); | ||
expect(alert).toHaveBeenCalledWith(JSON.stringify({ name: "John" })); | ||
}); | ||
|
||
test("reset form on submit", async () => { | ||
render(<Example />); | ||
await press.Tab(); | ||
await type("John"); | ||
await press.Enter(); | ||
expect(getInput("Name")).toHaveValue(""); | ||
}); | ||
|
||
test("markup", () => { | ||
const { container } = render(<Example />); | ||
expect(container).toMatchInlineSnapshot(` | ||
<div> | ||
<form | ||
class="form" | ||
novalidate="" | ||
> | ||
<div | ||
class="field" | ||
> | ||
<label | ||
for="r:p" | ||
id="r:o" | ||
> | ||
Name | ||
</label> | ||
<input | ||
aria-describedby="r:q" | ||
aria-labelledby="r:o" | ||
id="r:p" | ||
name="name" | ||
placeholder="John Doe" | ||
required="" | ||
value="" | ||
/> | ||
<div | ||
class="error" | ||
id="r:q" | ||
role="alert" | ||
/> | ||
</div> | ||
<button | ||
class="button" | ||
data-command="" | ||
type="submit" | ||
> | ||
Submit | ||
</button> | ||
</form> | ||
</div> | ||
`); | ||
}); |
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
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,3 @@ | ||
# Form | ||
|
||
<a href="./__examples__/form/index.tsx" data-playground>Example</a> |
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 |
---|---|---|
|
@@ -269,6 +269,7 @@ module.exports = { | |
}); | ||
|
||
addVariant("ariakit-focus-visible", "&[data-focus-visible]"); | ||
addVariant("aria-invalid", '&[aria-invalid="true"]'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ended up not needing this selector here, but might be useful in the future. |
||
addVariant("aria-disabled", '&[aria-disabled="true"]'); | ||
addVariant("aria-selected", '&[aria-selected="true"]'); | ||
}), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to add this because
FormInput
was missing the focus-visible features.