|
1
|
--- |
|
2
|
sidebar_label: "Keybound Form" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList, Kbd, getOsShortcut } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `KeyboundForm - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This component is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The `KeyboundForm` component is a `form` element that submits only when the user presses <Kbd>{getOsShortcut()}</Kbd>+<Kbd>Enter</Kbd>. The Medusa Admin uses it in its modals and drawers so that pressing <Kbd>Enter</Kbd> in a field doesn't submit the form by accident. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { KeyboundForm } from "@medusajs/dashboard/components" |
|
23
|
``` |
|
24
|
|
|
25
|
--- |
|
26
|
|
|
27
|
## Example |
|
28
|
|
|
29
|
Use `KeyboundForm` in place of a native `form` element inside a `Form` component, as shown in the [Forms guide](../../guides/forms/page.mdx): |
|
30
|
|
|
31
|
```tsx |
|
32
|
import { |
|
33
|
Form, |
|
34
|
KeyboundForm, |
|
35
|
} from "@medusajs/dashboard/components" |
|
36
|
import { Button, Input } from "@medusajs/ui" |
|
37
|
import { useForm } from "react-hook-form" |
|
38
|
|
|
39
|
const BrandForm = () => { |
|
40
|
const form = useForm({ |
|
41
|
defaultValues: { |
|
42
|
name: "", |
|
43
|
}, |
|
44
|
}) |
|
45
|
|
|
46
|
const handleSubmit = form.handleSubmit(async (data) => { |
|
47
|
console.log(data) |
|
48
|
}) |
|
49
|
|
|
50
|
return ( |
|
51
|
<Form {...form}> |
|
52
|
<KeyboundForm onSubmit={handleSubmit}> |
|
53
|
<Form.Field |
|
54
|
control={form.control} |
|
55
|
name="name" |
|
56
|
render={({ field }) => ( |
|
57
|
<Form.Item> |
|
58
|
<Form.Label>Name</Form.Label> |
|
59
|
<Form.Control> |
|
60
|
<Input {...field} /> |
|
61
|
</Form.Control> |
|
62
|
<Form.ErrorMessage /> |
|
63
|
</Form.Item> |
|
64
|
)} |
|
65
|
/> |
|
66
|
<Button type="submit">Save</Button> |
|
67
|
</KeyboundForm> |
|
68
|
</Form> |
|
69
|
) |
|
70
|
} |
|
71
|
``` |
|
72
|
|
|
73
|
The user can still submit the form by clicking the submit button. When the user presses <Kbd>Enter</Kbd> in a field, `KeyboundForm` prevents the default submission. In a textarea, pressing <Kbd>Enter</Kbd> inserts a new line instead. |
|
74
|
|
|
75
|
--- |
|
76
|
|
|
77
|
## Props |
|
78
|
|
|
79
|
`KeyboundForm` accepts the same props as a native `form` element, including the following: |
|
80
|
|
|
81
|
<TypeList |
|
82
|
types={[ |
|
83
|
{ |
|
84
|
name: "onSubmit", |
|
85
|
type: "`(event: React.FormEvent<HTMLFormElement>) => void`", |
|
86
|
optional: true, |
|
87
|
description: `The function to execute when the user submits the form. \`KeyboundForm\` prevents the event's default behavior before calling it.`, |
|
88
|
}, |
|
89
|
{ |
|
90
|
name: "onKeyDown", |
|
91
|
type: "`(event: React.KeyboardEvent<HTMLFormElement>) => void`", |
|
92
|
optional: true, |
|
93
|
description: `A custom key down handler. If you pass this prop, it replaces the default handler, so the keyboard shortcut no longer submits the form.`, |
|
94
|
}, |
|
95
|
{ |
|
96
|
name: "children", |
|
97
|
type: "`React.ReactNode`", |
|
98
|
optional: true, |
|
99
|
description: `The form's content, such as its fields and submit button.`, |
|
100
|
}, |
|
101
|
]} |
|
102
|
/> |
|
103
|
|
|
104
|
`KeyboundForm` also accepts every other attribute of a native [form element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form), such as `id`, `className`, and `noValidate`. |