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.
267 lines • 9.17 kB
JavaScript
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, tabKey, } from '../common/controllers/key-bindings.js';
import { registerComponent } from '../common/definitions/register.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { FormAssociatedCheckboxRequiredMixin } from '../common/mixins/forms/associated-required.js';
import { createFormValueState, defaultBooleanTransformers, } from '../common/mixins/forms/form-value.js';
import { createCounter, isDefined, isEmpty, isLTR, last, noop, partNameMap, wrap, } from '../common/util.js';
import IgcValidationContainerComponent from '../validation-container/validation-container.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 FormAssociatedCheckboxRequiredMixin(EventEmitterMixin(LitElement)) {
static register() {
registerComponent(IgcRadioComponent_1, IgcValidationContainerComponent);
}
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;
}
set required(value) {
super.required = value;
if (this.hasUpdated) {
for (const radio of this._siblings) {
radio._validate();
}
}
}
get required() {
return this._required;
}
set value(value) {
this._value = value;
if (this.checked) {
this._setFormValue(this._value || 'on');
}
}
get value() {
return this._value;
}
set checked(value) {
this._formValue.setValueAndFormState(value);
this._tabIndex = this.checked ? 0 : -1;
this._validate();
if (this.hasUpdated && this.checked) {
this._updateCheckedState();
}
}
get checked() {
return this._formValue.value;
}
constructor() {
super();
this.inputId = `radio-${IgcRadioComponent_1.increment()}`;
this.labelId = `radio-label-${this.inputId}`;
this._kbFocus = addKeyboardFocusRing(this);
this.hideLabel = false;
this._tabIndex = 0;
this.labelPosition = 'after';
this._formValue = createFormValueState(this, {
initialValue: false,
transformers: defaultBooleanTransformers,
});
addKeybindings(this, {
skip: () => this.disabled,
bindingDefaults: { preventDefault: true, triggers: ['keydownRepeat'] },
})
.set(tabKey, noop, { preventDefault: false })
.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();
this.hideLabel = isEmpty(this.label);
root.addEventListener('slotchange', () => {
this.hideLabel = isEmpty(this.label);
});
return root;
}
async firstUpdated() {
await this.updateComplete;
if (this.checked && this === last(this._checkedRadios)) {
for (const radio of this._siblings) {
radio.checked = false;
radio.defaultChecked = false;
}
}
else {
this._updateValidity();
}
}
_setDefaultValue(current) {
this._formValue.defaultValue = isDefined(current);
for (const radio of this._siblings) {
radio.defaultChecked = false;
}
}
click() {
this.input.click();
}
focus(options) {
this.input.focus(options);
}
blur() {
this.input.blur();
}
setCustomValidity(message) {
for (const radio of this._radios) {
radio._validate(message);
}
}
_updateCheckedState() {
for (const radio of this._siblings) {
radio.checked = false;
}
}
formResetCallback() {
super.formResetCallback();
this._resetTabIndexes();
}
_resetTabIndexes() {
const radios = this._radios;
if (isEmpty(this._checkedRadios)) {
for (const radio of radios) {
radio._tabIndex = 0;
}
}
else {
for (const radio of radios) {
radio._tabIndex = radio.checked ? 0 : -1;
}
}
}
handleClick(event) {
event.stopPropagation();
if (this.checked) {
return;
}
this.checked = true;
this.input.focus();
this.emitEvent('igcChange', {
detail: {
checked: this.checked,
value: this.value,
},
});
}
handleBlur() {
this._kbFocus.reset();
}
navigate(idx) {
const active = this._active;
const next = wrap(0, active.length - 1, active.indexOf(this) + idx);
const radio = active[next];
radio.focus();
radio.checked = true;
radio.emitEvent('igcChange', {
detail: { checked: radio.checked, value: radio.value },
});
}
renderValidatorContainer() {
return IgcValidationContainerComponent.create(this);
}
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}
=${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}
=${this.handleClick}
=${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>
${this.renderValidatorContainer()}
`;
}
};
IgcRadioComponent.tagName = 'igc-radio';
IgcRadioComponent.styles = [styles, shared];
IgcRadioComponent.increment = createCounter();
__decorate([
query('input', true)
], IgcRadioComponent.prototype, "input", void 0);
__decorate([
queryAssignedNodes({ flatten: true })
], IgcRadioComponent.prototype, "label", void 0);
__decorate([
state()
], IgcRadioComponent.prototype, "hideLabel", void 0);
__decorate([
state()
], IgcRadioComponent.prototype, "_tabIndex", void 0);
__decorate([
property({ type: Boolean, reflect: true })
], IgcRadioComponent.prototype, "required", null);
__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);
IgcRadioComponent = IgcRadioComponent_1 = __decorate([
themes(all)
], IgcRadioComponent);
export default IgcRadioComponent;
//# sourceMappingURL=radio.js.map