-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Radio): enable support for form-associated
- Loading branch information
1 parent
ac9aee7
commit 1e11069
Showing
7 changed files
with
240 additions
and
46 deletions.
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
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 |
---|---|---|
|
@@ -124,3 +124,86 @@ export const Fieldset = { | |
label: 'radio group', | ||
}, | ||
}; | ||
|
||
export const WithForm: Story = { | ||
render: () => { | ||
const handleFormSubmit = (ev: Event) => { | ||
ev.preventDefault(); | ||
const form = ev.target as HTMLFormElement; | ||
const formData = new FormData(form); | ||
const formValues = Object.fromEntries(formData.entries()); | ||
|
||
const codeElement = document.getElementById('form-data'); | ||
if (!codeElement) return; | ||
|
||
codeElement.textContent = JSON.stringify(formValues, null, 2); | ||
}; | ||
|
||
return html` | ||
<link rel="stylesheet" href="https://unpkg.com/@highlightjs/[email protected]/styles/night-owl.min.css" /> | ||
<div class="grid auto-cols-auto grid-cols-1 gap-y-l sm:grid-cols-2 sm:gap-x-l"> | ||
<bq-card> | ||
<h4 class="m-be-m">Marketing consent</h4> | ||
<form class="flex flex-col gap-y-m" @submit=${handleFormSubmit} method="post"> | ||
<bq-input name="email" value="[email protected]" type="email" autocomplete="organization" required> | ||
<label class="flex flex-grow items-center" slot="label">Email address</label> | ||
</bq-input> | ||
<bq-radio-group | ||
name="marketing-consent" | ||
orientation="horizontal" | ||
required-validation-message="Please, select if you would like to receive marketing emails or not" | ||
value="yes" | ||
required | ||
> | ||
<span slot="label">I would like to receive marketing emails</span> | ||
<bq-radio value="yes">Yes</bq-radio> | ||
<bq-radio value="no">No</bq-radio> | ||
</bq-radio-group> | ||
<fieldset> | ||
<legend>Remember me</legend> | ||
<label> | ||
<input type="radio" name="remember" value="remember" required /> | ||
Yes | ||
</label> | ||
<label> | ||
<input type="radio" name="remember" value="forget" required /> | ||
No | ||
</label> | ||
</fieldset> | ||
<div class="flex justify-end gap-x-s"> | ||
<bq-button appearance="secondary" type="reset">Cancel</bq-button> | ||
<bq-button type="submit">Save</bq-button> | ||
</div> | ||
</form> | ||
</bq-card> | ||
<bq-card class="[&::part(wrapper)]:h-full"> | ||
<h4 class="m-be-m">Form Data</h4> | ||
<div class="language-javascript overflow-x-scroll whitespace-pre rounded-s"> | ||
// Handle form submit<br /> | ||
const form = ev.target as HTMLFormElement;<br /> | ||
const formData = new FormData(form);<br /> | ||
const formValues = Object.fromEntries(formData.entries()); | ||
</div> | ||
<pre> | ||
<code id="form-data" class="rounded-s"> | ||
{ // submit the form to see the data here } | ||
</code> | ||
</pre> | ||
</bq-card> | ||
</div> | ||
<script type="module"> | ||
import hljs from 'https://unpkg.com/@highlightjs/[email protected]/es/highlight.min.js'; | ||
import javascript from 'https://unpkg.com/@highlightjs/[email protected]/es/languages/javascript.min.js'; | ||
hljs.registerLanguage('javascript', javascript); | ||
hljs.highlightAll(); | ||
document.querySelectorAll('div.language-javascript').forEach((block) => { | ||
hljs.highlightElement(block); | ||
}); | ||
</script> | ||
`; | ||
}, | ||
}; |
Oops, something went wrong.