|
| 1 | +import { storiesOf, html } from '@open-wc/demoing-storybook'; |
| 2 | +import { LitElement } from '@lion/core'; |
| 3 | + |
| 4 | +import { LocalizeMixin } from '@lion/localize'; |
| 5 | + |
| 6 | +import '../lion-input-switch.js'; |
| 7 | +import '@lion/form/lion-form.js'; |
| 8 | + |
| 9 | +storiesOf('Forms|Switch', module) |
| 10 | + .add( |
| 11 | + 'All text slots', |
| 12 | + () => html` |
| 13 | + <lion-input-switch label="Label" help-text="Help text"> </lion-input-switch> |
| 14 | + `, |
| 15 | + ) |
| 16 | + .add( |
| 17 | + 'Disabled', |
| 18 | + () => html` |
| 19 | + <lion-input-switch label="Disabled label" disabled> </lion-input-switch> |
| 20 | + `, |
| 21 | + ) |
| 22 | + .add('Validation', () => { |
| 23 | + const isTrue = value => value && value.checked && value.checked === true; |
| 24 | + const isTrueValidator = (...factoryParams) => [ |
| 25 | + (...params) => ({ |
| 26 | + isTrue: isTrue(...params), |
| 27 | + }), |
| 28 | + ...factoryParams, |
| 29 | + ]; |
| 30 | + const tagName = 'lion-switch-validation-demo'; |
| 31 | + if (!customElements.get(tagName)) { |
| 32 | + customElements.define( |
| 33 | + tagName, |
| 34 | + class extends LocalizeMixin(LitElement) { |
| 35 | + static get localizeNamespaces() { |
| 36 | + const result = [ |
| 37 | + { |
| 38 | + 'lion-validate+isTrue': () => |
| 39 | + Promise.resolve({ |
| 40 | + info: { |
| 41 | + isTrue: 'You will not get the latest news!', |
| 42 | + }, |
| 43 | + }), |
| 44 | + }, |
| 45 | + ...super.localizeNamespaces, |
| 46 | + ]; |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + render() { |
| 51 | + return html` |
| 52 | + <lion-form id="postsForm" @submit="${this.submit}"> |
| 53 | + <form> |
| 54 | + <lion-input-switch name="emailAddress" label="Share email address"> |
| 55 | + </lion-input-switch> |
| 56 | + <lion-input-switch name="subjectField" label="Show subject field" checked> |
| 57 | + </lion-input-switch> |
| 58 | + <lion-input-switch name="characterCount" label="Character count"> |
| 59 | + </lion-input-switch> |
| 60 | + <lion-input-switch |
| 61 | + name="newsletterCheck" |
| 62 | + label="* Subscribe to newsletter" |
| 63 | + .infoValidators="${[isTrueValidator()]}" |
| 64 | + > |
| 65 | + </lion-input-switch> |
| 66 | + <button type="submit"> |
| 67 | + Submit |
| 68 | + </button> |
| 69 | + </form> |
| 70 | + </lion-form> |
| 71 | + `; |
| 72 | + } |
| 73 | + |
| 74 | + submit() { |
| 75 | + const form = this.shadowRoot.querySelector('#postsForm'); |
| 76 | + if (form.errorState === false) { |
| 77 | + console.log(form.serializeGroup()); |
| 78 | + } |
| 79 | + } |
| 80 | + }, |
| 81 | + ); |
| 82 | + } |
| 83 | + return html` |
| 84 | + <lion-switch-validation-demo></lion-switch-validation-demo> |
| 85 | + `; |
| 86 | + }); |
0 commit comments