feat(form): add ref prop (#900)

Closes #877
This commit is contained in:
Eric Liu 2021-11-10 19:46:14 -08:00 committed by GitHub
commit 08402e54bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 4 deletions

View file

@ -1447,7 +1447,9 @@ None.
### Props ### Props
None. | Prop name | Kind | Reactive | Type | Default value | Description |
| :-------- | :--------------- | :------- | :--------------------------------------- | ----------------- | -------------------------------------- |
| ref | <code>let</code> | Yes | <code>null &#124; HTMLFormElement</code> | <code>null</code> | Obtain a reference to the form element |
### Slots ### Slots

View file

@ -3998,7 +3998,19 @@
{ {
"moduleName": "Form", "moduleName": "Form",
"filePath": "src/Form/Form.svelte", "filePath": "src/Form/Form.svelte",
"props": [], "props": [
{
"name": "ref",
"kind": "let",
"description": "Obtain a reference to the form element",
"type": "null | HTMLFormElement",
"value": "null",
"isFunction": false,
"isFunctionDeclaration": false,
"constant": false,
"reactive": true
}
],
"slots": [{ "name": "__default__", "default": true, "slot_props": "{}" }], "slots": [{ "name": "__default__", "default": true, "slot_props": "{}" }],
"events": [ "events": [
{ "type": "forwarded", "name": "click", "element": "form" }, { "type": "forwarded", "name": "click", "element": "form" },

View file

@ -1,6 +1,12 @@
<script>
/** Obtain a reference to the form element */
export let ref = null;
</script>
<!-- svelte-ignore a11y-mouse-events-have-key-events --> <!-- svelte-ignore a11y-mouse-events-have-key-events -->
<form <form
class:bx--form="{true}" class:bx--form="{true}"
bind:this="{ref}"
{...$$restProps} {...$$restProps}
on:click on:click
on:keydown on:keydown

View file

@ -9,9 +9,11 @@
SelectItem, SelectItem,
Button, Button,
} from "../types"; } from "../types";
let ref: HTMLFormElement;
</script> </script>
<Form on:submit> <Form on:submit bind:ref>
<FormGroup legendText="Checkboxes"> <FormGroup legendText="Checkboxes">
<Checkbox id="checkbox-0" labelText="Checkbox Label" checked /> <Checkbox id="checkbox-0" labelText="Checkbox Label" checked />
<Checkbox id="checkbox-1" labelText="Checkbox Label" /> <Checkbox id="checkbox-1" labelText="Checkbox Label" />

View file

@ -2,7 +2,13 @@
import { SvelteComponentTyped } from "svelte"; import { SvelteComponentTyped } from "svelte";
export interface FormProps export interface FormProps
extends svelte.JSX.HTMLAttributes<HTMLElementTagNameMap["form"]> {} extends svelte.JSX.HTMLAttributes<HTMLElementTagNameMap["form"]> {
/**
* Obtain a reference to the form element
* @default null
*/
ref?: null | HTMLFormElement;
}
export default class Form extends SvelteComponentTyped< export default class Form extends SvelteComponentTyped<
FormProps, FormProps,