/* eslint-disable class-methods-use-this */
import { html, css, nothing } from '@lion/core';
import { FormatMixin } from '@lion/field';
export const ChoiceInputMixin = superclass =>
// eslint-disable-next-line
class ChoiceInputMixin extends FormatMixin(superclass) {
static get properties() {
return {
...super.properties,
/**
* Boolean indicating whether or not this element is checked by the end user.
*/
checked: {
type: Boolean,
reflect: true,
},
/**
* Whereas 'normal' `.modelValue`s usually store a complex/typed version
* of a view value, choice inputs have a slightly different approach.
* In order to remain their Single Source of Truth characteristic, choice inputs
* store both the value and 'checkedness', in the format { value: 'x', checked: true }
* Different from the platform, this also allows to serialize the 'non checkedness',
* allowing to restore form state easily and inform the server about unchecked options.
*/
modelValue: {
type: Object,
hasChanged: (nw, old = {}) => nw.value !== old.value || nw.checked !== old.checked,
},
/**
* The value property of the modelValue. It provides an easy inteface for storing
* (complex) values in the modelValue
*/
choiceValue: {
type: Object,
},
};
}
get choiceValue() {
return this.modelValue.value;
}
set choiceValue(value) {
this.requestUpdate('choiceValue', this.choiceValue);
if (this.modelValue.value !== value) {
this.modelValue = { value, checked: this.modelValue.checked };
}
}
_requestUpdate(name, oldValue) {
super._requestUpdate(name, oldValue);
if (name === 'modelValue') {
if (this.modelValue.checked !== this.checked) {
this.__syncModelCheckedToChecked(this.modelValue.checked);
}
} else if (name === 'checked') {
if (this.modelValue.checked !== this.checked) {
this.__syncCheckedToModel(this.checked);
}
}
}
firstUpdated(c) {
super.firstUpdated(c);
if (c.has('checked')) {
// Here we set the initial value for our [slot=input] content,
// which has been set by our SlotMixin
this.__syncCheckedToInputElement();
}
}
updated(c) {
super.updated(c);
if (c.has('modelValue')) {
this._reflectCheckedToCssClass({ modelValue: this.modelValue });
this.__syncCheckedToInputElement();
}
}
constructor() {
super();
this.modelValue = { value: '', checked: false };
}
/**
* Styles for [input=radio] and [input=checkbox] wrappers.
* For [role=option] extensions, please override completely
*/
static get styles() {
return [
css`
:host {
display: flex;
}
.choice-field__graphic-container {
display: none;
}
`,
];
}
/**
* Template for [input=radio] and [input=checkbox] wrappers.
* For [role=option] extensions, please override completely
*/
render() {
return html`