From 3d1c723d39bb1df448489c57675245616a9d75ae Mon Sep 17 00:00:00 2001 From: gerjanvangeest Date: Wed, 20 Nov 2024 08:13:31 +0100 Subject: [PATCH] chore: enhance form documentation (#2391) --- docs/components/form/use-cases.md | 165 ++++++++++++++++++++++++++++-- 1 file changed, 157 insertions(+), 8 deletions(-) diff --git a/docs/components/form/use-cases.md b/docs/components/form/use-cases.md index 830046769..888a0bbd1 100644 --- a/docs/components/form/use-cases.md +++ b/docs/components/form/use-cases.md @@ -1,22 +1,20 @@ # Form >> Use Cases ||20 ```js script -import { html } from '@mdjs/mdjs-preview'; +import { html as previewHtml } from '@mdjs/mdjs-preview'; import { Required } from '@lion/ui/form-core.js'; import { loadDefaultFeedbackMessages } from '@lion/ui/validate-messages.js'; +import '@lion/ui/define/lion-fieldset.js'; import '@lion/ui/define/lion-input.js'; import '@lion/ui/define/lion-form.js'; ``` ## Submit & Reset -To submit a form, use a regular ` + + + + + `; +}; +``` + +Learn more about [formatting and parsing](../../fundamentals/systems/form/formatting-and-parsing.md). + +## Sub forms + +To create sections inside a form you can make use of fieldsets. + +In some cases you want to create a reusable sub-form component. Since all form elements need to be in the light DOM, to be accessible, the sub-form needs to be in the light DOM too. To do this you'll need to make use of the `SlotMixin`. + +```js preview-story +import { html, LitElement } from 'lit'; +import { ScopedElementsMixin } from '@open-wc/scoped-elements/lit-element.js'; +import { SlotMixin } from '@lion/ui/core.js'; +import { Required as _Required } from '@lion/ui/form-core.js'; +import { LionFieldset } from '@lion/ui/fieldset.js'; +import { LionInput } from '@lion/ui/input.js'; + +class SubFormAddress extends SlotMixin(ScopedElementsMixin(LitElement)) { + static get scopedElements() { + return { + 'lion-fieldset': LionFieldset, + 'lion-input': LionInput, + }; + } + + get slots() { + return { + ...super.slots, + '': () => ({ + template: this.#renderInSlot(), + }), + }; + } + + render() { + return html` `; + } + + /** + * @private + */ + #renderInSlot() { + return html` + + + + + + `; + } +} + +customElements.define('sub-form-address', SubFormAddress); + +export const subForms = () => { + loadDefaultFeedbackMessages(); + const submitHandler = ev => { + const formData = ev.target.serializedValue; + console.log('formData', formData); + if (!ev.target.hasFeedbackFor?.includes('error')) { + fetch('/api/foo/', { + method: 'POST', + body: JSON.stringify(formData), + }); + } + }; + return html` + +
+ + + + + +
+ + +
+
+
+ `; +}; +```