import { html, css, nothing, dedupeMixin, SlotMixin } from '@lion/core'; import { FormRegisteringMixin } from './FormRegisteringMixin.js'; import { getAriaElementsInRightDomOrder } from './utils/getAriaElementsInRightDomOrder.js'; /** * Generates random unique identifier (for dom elements) * @param {string} prefix */ function uuid(prefix) { return `${prefix}-${Math.random() .toString(36) .substr(2, 10)}`; } /** * #FormControlMixin : * * This Mixin is a shared fundament for all form components, it's applied on: * - LionField (which is extended to LionInput, LionTextarea, LionSelect etc. etc.) * - LionFieldset (which is extended to LionRadioGroup, LionCheckboxGroup, LionForm) * * @polymerMixin * @mixinFunction */ export const FormControlMixin = dedupeMixin( superclass => // eslint-disable-next-line no-shadow, no-unused-vars class FormControlMixin extends FormRegisteringMixin(SlotMixin(superclass)) { static get properties() { return { /** * When no light dom defined and prop set */ label: String, /** * When no light dom defined and prop set */ helpText: { type: String, attribute: 'help-text', }, /** * Contains all elements that should end up in aria-labelledby of `._inputNode` */ _ariaLabelledNodes: Array, /** * Contains all elements that should end up in aria-describedby of `._inputNode` */ _ariaDescribedNodes: Array, }; } get slots() { return { ...super.slots, label: () => { const label = document.createElement('label'); label.textContent = this.label; return label; }, 'help-text': () => { const helpText = document.createElement('div'); helpText.textContent = this.helpText; return helpText; }, }; } updated(changedProps) { super.updated(changedProps); if (changedProps.has('_ariaLabelledNodes')) { this.__reflectAriaAttr( 'aria-labelledby', this._ariaLabelledNodes, this.__reorderAriaLabelledNodes, ); } if (changedProps.has('_ariaDescribedNodes')) { this.__reflectAriaAttr( 'aria-describedby', this._ariaDescribedNodes, this.__reorderAriaDescribedNodes, ); } if (changedProps.has('label')) { this._onLabelChanged({ label: this.label }); } if (changedProps.has('helpText')) { this._onHelpTextChanged({ helpText: this.helpText }); } } get _inputNode() { return this.__getDirectSlotChild('input'); } get _labelNode() { return this.__getDirectSlotChild('label'); } get _helpTextNode() { return this.__getDirectSlotChild('help-text'); } get _feedbackNode() { return this.__getDirectSlotChild('feedback'); } constructor() { super(); this._inputId = uuid(this.localName); this._ariaLabelledNodes = []; this._ariaDescribedNodes = []; } connectedCallback() { super.connectedCallback(); this._enhanceLightDomClasses(); this._enhanceLightDomA11y(); } /** * Public methods */ _enhanceLightDomClasses() { if (this._inputNode) { this._inputNode.classList.add('form-control'); } } _enhanceLightDomA11y() { const { _inputNode, _labelNode, _helpTextNode, _feedbackNode } = this; if (_inputNode) { _inputNode.id = _inputNode.id || this._inputId; } if (_labelNode) { _labelNode.setAttribute('for', this._inputId); this.addToAriaLabelledBy(_labelNode, { idPrefix: 'label' }); } if (_helpTextNode) { this.addToAriaDescribedBy(_helpTextNode, { idPrefix: 'help-text' }); } if (_feedbackNode) { _feedbackNode.setAttribute('aria-live', 'polite'); this.addToAriaDescribedBy(_feedbackNode, { idPrefix: 'feedback' }); } this._enhanceLightDomA11yForAdditionalSlots(); } /** * Enhances additional slots(prefix, suffix, before, after) defined by developer. * * When boolean attribute data-label or data-description is found, * the slot element will be connected to the input via aria-labelledby or aria-describedby */ _enhanceLightDomA11yForAdditionalSlots( additionalSlots = ['prefix', 'suffix', 'before', 'after'], ) { additionalSlots.forEach(additionalSlot => { const element = this.__getDirectSlotChild(additionalSlot); if (element) { if (element.hasAttribute('data-label') === true) { this.addToAriaLabelledBy(element, { idPrefix: additionalSlot }); } if (element.hasAttribute('data-description') === true) { this.addToAriaDescribedBy(element, { idPrefix: additionalSlot }); } } }); } /** * Will handle help text, validation feedback and character counter, * prefix/suffix/before/after (if they contain data-description flag attr). * Also, contents of id references that will be put in the ._ariaDescribedby property * from an external context, will be read by a screen reader. */ __reflectAriaAttr(attrName, nodes, reorder) { if (this._inputNode) { if (reorder) { const insideNodes = nodes.filter(n => this.contains(n)); const outsideNodes = nodes.filter(n => !this.contains(n)); // eslint-disable-next-line no-param-reassign nodes = [...getAriaElementsInRightDomOrder(insideNodes), ...outsideNodes]; } const string = nodes.map(n => n.id).join(' '); this._inputNode.setAttribute(attrName, string); } } _onLabelChanged({ label }) { if (this._labelNode) { this._labelNode.textContent = label; } } _onHelpTextChanged({ helpText }) { if (this._helpTextNode) { this._helpTextNode.textContent = helpText; } } /** * Default Render Result: *
*
* *
* * * *
*
*
*
* *
*
*
* *
*
* *
*
* *
*
*
* *
*
* *
*/ render() { return html`
${this.groupOneTemplate()}
${this.groupTwoTemplate()}
`; } groupOneTemplate() { return html` ${this.labelTemplate()} ${this.helpTextTemplate()} `; } groupTwoTemplate() { return html` ${this.inputGroupTemplate()} ${this.feedbackTemplate()} `; } // eslint-disable-next-line class-methods-use-this labelTemplate() { return html`
`; } // eslint-disable-next-line class-methods-use-this helpTextTemplate() { return html` `; } inputGroupTemplate() { return html`
${this.inputGroupBeforeTemplate()}
${this.inputGroupPrefixTemplate()} ${this.inputGroupInputTemplate()} ${this.inputGroupSuffixTemplate()}
${this.inputGroupAfterTemplate()}
`; } // eslint-disable-next-line class-methods-use-this inputGroupBeforeTemplate() { return html`
`; } inputGroupPrefixTemplate() { return !Array.from(this.children).find(child => child.slot === 'prefix') ? nothing : html`
`; } // eslint-disable-next-line class-methods-use-this inputGroupInputTemplate() { return html`
`; } inputGroupSuffixTemplate() { return !Array.from(this.children).find(child => child.slot === 'suffix') ? nothing : html`
`; } // eslint-disable-next-line class-methods-use-this inputGroupAfterTemplate() { return html`
`; } // eslint-disable-next-line class-methods-use-this feedbackTemplate() { return html`
`; } /** * All CSS below is written from a generic mindset, following BEM conventions: * https://en.bem.info/methodology/ * Although the CSS and HTML are implemented by the component, they should be regarded as * totally decoupled. * * Not only does this force us to write better structured css, it also allows for future * reusability in many different ways like: * - disabling shadow DOM for a component (for water proof encapsulation can be combined with * a build step) * - easier translation to more flexible, WebComponents agnostic solutions like JSS * (allowing extends, mixins, reasoning, IDE integration, tree shaking etc.) * - export to a CSS module for reuse in an outer context * * * Please note that the HTML structure is purposely 'loose', allowing multiple design systems * to be compatible * with the CSS component. * Note that every occurence of '::slotted(*)' can be rewritten to '> *' for use in an other * context * * TODO: find best naming convention: https://en.bem.info/methodology/naming-convention/ * (react style would align better with JSS) */ /** * {block} .form-field * * Structure: * - {element} .form-field__label : a wrapper element around the projected label * - {element} .form-field__help-text (optional) : a wrapper element around the projected * help-text * - {block} .input-group : a container around the input element, including prefixes and * suffixes * - {element} .form-field__feedback (optional) : a wrapper element around the projected * (validation) feedback message * * Modifiers: * - {state} .state-disabled : when .form-control (,