UNPKG

igniteui-webcomponents

Version:

Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.

258 lines 8.71 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var IgcRadioComponent_1; import { LitElement, html } from 'lit'; import { property, query, queryAssignedNodes, state } from 'lit/decorators.js'; import { ifDefined } from 'lit/directives/if-defined.js'; import { live } from 'lit/directives/live.js'; import { themes } from '../../theming/theming-decorator.js'; import { addKeyboardFocusRing } from '../common/controllers/focus-ring.js'; import { addKeybindings, arrowDown, arrowLeft, arrowRight, arrowUp, } from '../common/controllers/key-bindings.js'; import { watch } from '../common/decorators/watch.js'; import { registerComponent } from '../common/definitions/register.js'; import { EventEmitterMixin } from '../common/mixins/event-emitter.js'; import { FormAssociatedRequiredMixin } from '../common/mixins/form-associated-required.js'; import { createCounter, isLTR, last, partNameMap, wrap, } from '../common/util.js'; import { styles } from './themes/radio.base.css.js'; import { styles as shared } from './themes/shared/radio.common.css.js'; import { all } from './themes/themes.js'; import { getGroup } from './utils.js'; import { radioValidators } from './validators.js'; let IgcRadioComponent = IgcRadioComponent_1 = class IgcRadioComponent extends FormAssociatedRequiredMixin(EventEmitterMixin(LitElement)) { static register() { registerComponent(IgcRadioComponent_1); } get __validators() { return radioValidators; } get _radios() { return getGroup(this).radios; } get _siblings() { return getGroup(this).siblings; } get _active() { return getGroup(this).active; } get _checkedRadios() { return getGroup(this).checked; } setDefaultValue() { this._defaultValue = this === last(this._checkedRadios); } set value(value) { this._value = value; if (this._checked) { this.setFormValue(this._value || 'on'); } } get value() { return this._value; } set checked(value) { this._checked = Boolean(value); if (this.hasUpdated) { this._checked ? this._updateCheckedState() : this._updateUncheckedState(); } } get checked() { return this._checked; } constructor() { super(); this.inputId = `radio-${IgcRadioComponent_1.increment()}`; this.labelId = `radio-label-${this.inputId}`; this._kbFocus = addKeyboardFocusRing(this); this._checked = false; this._tabIndex = 0; this.hideLabel = false; this.labelPosition = 'after'; addKeybindings(this, { skip: () => this.disabled, bindingDefaults: { preventDefault: true, triggers: ['keydownRepeat'] }, }) .set(arrowLeft, () => this.navigate(isLTR(this) ? -1 : 1)) .set(arrowRight, () => this.navigate(isLTR(this) ? 1 : -1)) .set(arrowUp, () => this.navigate(-1)) .set(arrowDown, () => this.navigate(1)); } createRenderRoot() { const root = super.createRenderRoot(); root.addEventListener('slotchange', () => { this.hideLabel = this.label.length < 1; }); return root; } connectedCallback() { super.connectedCallback(); this.updateValidity(); } async firstUpdated() { await this.updateComplete; this._checked && this === last(this._checkedRadios) ? this._updateCheckedState() : this.updateValidity(); } click() { this.input.click(); } focus(options) { this.input.focus(options); } blur() { this.input.blur(); } setCustomValidity(message) { const radios = this._radios; for (const radio of radios) { radio.updateValidity(message); radio.setInvalidState(); } } requiredChange() { const radios = this._radios; for (const radio of radios) { radio.updateValidity(); radio.setInvalidState(); } } _updateCheckedState() { const siblings = this._siblings; this.setFormValue(this.value || 'on'); this.updateValidity(); this.setInvalidState(); this._tabIndex = 0; this.input?.focus(); for (const radio of siblings) { radio.checked = false; radio._tabIndex = -1; radio.updateValidity(); radio.setInvalidState(); } } _updateUncheckedState() { const siblings = this._siblings; this.setFormValue(null); this.updateValidity(); this.setInvalidState(); this._tabIndex = -1; for (const radio of siblings) { radio.updateValidity(); } } formResetCallback() { super.formResetCallback(); this._resetTabIndexes(); } _resetTabIndexes() { const radios = this._radios; for (const radio of radios) { radio._tabIndex = 0; } } handleClick() { if (this.checked) { return; } this.checked = true; this.emitEvent('igcChange', { detail: { checked: this.checked, value: this.value, }, }); } handleBlur() { this._kbFocus.reset(); } navigate(idx) { const active = this._active; const nextIdx = wrap(0, active.length - 1, active.indexOf(this) + idx); const radio = active[nextIdx]; radio.focus(); radio.checked = true; radio.emitEvent('igcChange', { detail: { checked: radio.checked, value: radio.value }, }); } render() { const labelledBy = this.getAttribute('aria-labelledby'); const checked = this.checked; return html ` <label part=${partNameMap({ base: true, checked, focused: this._kbFocus.focused, })} for=${this.inputId} @pointerdown=${this._kbFocus.reset} > <input id=${this.inputId} type="radio" name=${ifDefined(this.name)} value=${ifDefined(this.value)} .required=${this.required} .disabled=${this.disabled} .checked=${live(checked)} tabindex=${this._tabIndex} aria-checked=${checked ? 'true' : 'false'} aria-disabled=${this.disabled ? 'true' : 'false'} aria-labelledby=${labelledBy ? labelledBy : this.labelId} @click=${this.handleClick} @blur=${this.handleBlur} /> <span part=${partNameMap({ control: true, checked })}> <span .hidden=${this.disabled} part=${partNameMap({ ripple: true, checked })} ></span> </span> <span .hidden=${this.hideLabel} part=${partNameMap({ label: true, checked })} id=${this.labelId} > <slot></slot> </span> </label> `; } }; IgcRadioComponent.tagName = 'igc-radio'; IgcRadioComponent.styles = [styles, shared]; IgcRadioComponent.increment = createCounter(); __decorate([ query('input[type="radio"]') ], IgcRadioComponent.prototype, "input", void 0); __decorate([ queryAssignedNodes({ flatten: true }) ], IgcRadioComponent.prototype, "label", void 0); __decorate([ state() ], IgcRadioComponent.prototype, "_tabIndex", void 0); __decorate([ state() ], IgcRadioComponent.prototype, "hideLabel", void 0); __decorate([ property() ], IgcRadioComponent.prototype, "value", null); __decorate([ property({ type: Boolean }) ], IgcRadioComponent.prototype, "checked", null); __decorate([ property({ reflect: true, attribute: 'label-position' }) ], IgcRadioComponent.prototype, "labelPosition", void 0); __decorate([ watch('required', { waitUntilFirstUpdate: true }) ], IgcRadioComponent.prototype, "requiredChange", null); IgcRadioComponent = IgcRadioComponent_1 = __decorate([ themes(all) ], IgcRadioComponent); export default IgcRadioComponent; //# sourceMappingURL=radio.js.map