[//]: # 'AUTO INSERT HEADER PREPUBLISH' # Features Overview This is a meta package to show interaction between various form elements. For usage and installation please see the appropriate packages. ```js script import { html } from '@lion/core'; import '@lion/button/lion-button.js'; import '@lion/checkbox-group/lion-checkbox-group.js'; import '@lion/checkbox-group/lion-checkbox.js'; import '@lion/combobox/lion-combobox.js'; import '@lion/fieldset/lion-fieldset.js'; import '@lion/form/lion-form.js'; import '@lion/input-amount/lion-input-amount.js'; import '@lion/input-date/lion-input-date.js'; import '@lion/input-datepicker/lion-input-datepicker.js'; import '@lion/input-email/lion-input-email.js'; import '@lion/input-iban/lion-input-iban.js'; import '@lion/input-range/lion-input-range.js'; import '@lion/input-stepper/lion-input-stepper.js'; import '@lion/input/lion-input.js'; import '@lion/listbox/lion-listbox.js'; import '@lion/listbox/lion-option.js'; import '@lion/listbox/lion-options.js'; import '@lion/radio-group/lion-radio-group.js'; import '@lion/radio-group/lion-radio.js'; import '@lion/select/lion-select.js'; import '@lion/select-rich/lion-select-rich.js'; import '@lion/textarea/lion-textarea.js'; import { MinLength, Required } from '@lion/form-core'; import { loadDefaultFeedbackMessages } from '@lion/validate-messages'; loadDefaultFeedbackMessages(); export default { title: 'Forms/Features Overview', }; ``` ## Umbrella Form ```js story export const main = () => { Required.getMessage = () => 'Please enter a value'; return html`
Apple Banana Mango Rocky Rocky II Rocky III Rocky IV Rocky V Rocky Balboa Red Hotpink Teal
Max. 5 guests
Submit ev.currentTarget.parentElement.parentElement.parentElement.resetGroup()} >Reset
`; }; ``` ## Submitting a form To submit a form, use a regular button (or `LionButton`) with `type="submit"` (which is default) somewhere inside the native `
`. Then, add a `submit` handler on the `lion-form`. You can use this event to do your own (pre-)submit logic, like getting the serialized form data and sending it to a backend API. Another example is checking if the form has errors, and focusing the first field with an error. To fire a submit from JavaScript, select the `lion-form` element and call `.submit()`. ```js preview-story export const formSubmit = () => { const submitHandler = ev => { if (ev.target.hasFeedbackFor.includes('error')) { const firstFormElWithError = ev.target.formElements.find(el => el.hasFeedbackFor.includes('error'), ); firstFormElWithError.focus(); return; } const formData = ev.target.serializedValue; fetch('/api/foo/', { method: 'POST', body: JSON.stringify(formData), }); }; const submitViaJS = ev => { // Call submit on the lion-form element, in your own code you should use // a selector that's not dependent on DOM structure like this one. ev.target.previousElementSibling.submit(); }; return html` ev.preventDefault()}>
Submit ev.currentTarget.parentElement.parentElement.parentElement.resetGroup()} >Reset
`; }; ```