chore(fieldset): replaced CssClassMixin by DisabledMixin
This commit is contained in:
parent
2bf8c07079
commit
b90d89ccf0
3 changed files with 69 additions and 26 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import { SlotMixin, html } from '@lion/core';
|
import { SlotMixin, html } from '@lion/core';
|
||||||
import { LionLitElement } from '@lion/core/src/LionLitElement.js';
|
import { LionLitElement } from '@lion/core/src/LionLitElement.js';
|
||||||
import { CssClassMixin } from '@lion/core/src/CssClassMixin.js';
|
import { DisabledMixin } from '@lion/core/src/DisabledMixin.js';
|
||||||
import { ObserverMixin } from '@lion/core/src/ObserverMixin.js';
|
import { ObserverMixin } from '@lion/core/src/ObserverMixin.js';
|
||||||
import { ValidateMixin } from '@lion/validate';
|
import { ValidateMixin } from '@lion/validate';
|
||||||
import { FormControlMixin, FormRegistrarMixin } from '@lion/field';
|
import { FormControlMixin, FormRegistrarMixin } from '@lion/field';
|
||||||
|
|
@ -15,16 +15,10 @@ const pascalCase = str => str.charAt(0).toUpperCase() + str.slice(1);
|
||||||
* @extends LionLitElement
|
* @extends LionLitElement
|
||||||
*/
|
*/
|
||||||
export class LionFieldset extends FormRegistrarMixin(
|
export class LionFieldset extends FormRegistrarMixin(
|
||||||
FormControlMixin(ValidateMixin(CssClassMixin(SlotMixin(ObserverMixin(LionLitElement))))),
|
FormControlMixin(ValidateMixin(DisabledMixin(SlotMixin(ObserverMixin(LionLitElement))))),
|
||||||
) {
|
) {
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
...super.properties,
|
|
||||||
disabled: {
|
|
||||||
type: Boolean,
|
|
||||||
reflect: true,
|
|
||||||
nonEmptyToClass: 'state-disabled',
|
|
||||||
},
|
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
|
@ -35,13 +29,6 @@ export class LionFieldset extends FormRegistrarMixin(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static get asyncObservers() {
|
|
||||||
return {
|
|
||||||
...super.asyncObservers,
|
|
||||||
_onDisabledChanged: ['disabled'],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
get inputElement() {
|
get inputElement() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
@ -121,6 +108,36 @@ export class LionFieldset extends FormRegistrarMixin(
|
||||||
this.removeEventListener('dirty-changed', this._updateDirtyClass);
|
this.removeEventListener('dirty-changed', this._updateDirtyClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updated(changedProps) {
|
||||||
|
super.updated(changedProps);
|
||||||
|
|
||||||
|
if (changedProps.has('disabled')) {
|
||||||
|
if (this.disabled) {
|
||||||
|
this.__requestChildrenToBeDisabled();
|
||||||
|
this.classList.add('state-disabled'); // eslint-disable-line wc/no-self-class
|
||||||
|
} else {
|
||||||
|
this.__retractRequestChildrenToBeDisabled();
|
||||||
|
this.classList.remove('state-disabled'); // eslint-disable-line wc/no-self-class
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__requestChildrenToBeDisabled() {
|
||||||
|
this.formElementsArray.forEach(child => {
|
||||||
|
if (child.makeRequestToBeDisabled) {
|
||||||
|
child.makeRequestToBeDisabled();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
__retractRequestChildrenToBeDisabled() {
|
||||||
|
this.formElementsArray.forEach(child => {
|
||||||
|
if (child.retractRequestToBeDisabled) {
|
||||||
|
child.retractRequestToBeDisabled();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line class-methods-use-this
|
// eslint-disable-next-line class-methods-use-this
|
||||||
inputGroupTemplate() {
|
inputGroupTemplate() {
|
||||||
return html`
|
return html`
|
||||||
|
|
@ -251,13 +268,6 @@ export class LionFieldset extends FormRegistrarMixin(
|
||||||
this.classList[this.dirty ? 'add' : 'remove']('state-dirty');
|
this.classList[this.dirty ? 'add' : 'remove']('state-dirty');
|
||||||
}
|
}
|
||||||
|
|
||||||
_onDisabledChanged({ disabled }, { disabled: oldDisabled }) {
|
|
||||||
// do not propagate/override inital disabled value on nested form elements
|
|
||||||
if (typeof oldDisabled !== 'undefined') {
|
|
||||||
this._setValueForAllFormElements('disabled', disabled);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_setRole(role) {
|
_setRole(role) {
|
||||||
this.setAttribute('role', role || 'group');
|
this.setAttribute('role', role || 'group');
|
||||||
}
|
}
|
||||||
|
|
@ -303,7 +313,7 @@ export class LionFieldset extends FormRegistrarMixin(
|
||||||
|
|
||||||
if (this.disabled) {
|
if (this.disabled) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
child.disabled = true;
|
child.makeRequestToBeDisabled();
|
||||||
}
|
}
|
||||||
if (name.substr(-2) === '[]') {
|
if (name.substr(-2) === '[]') {
|
||||||
if (!Array.isArray(this.formElements[name])) {
|
if (!Array.isArray(this.formElements[name])) {
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,30 @@ storiesOf('Forms|Fieldset', module)
|
||||||
</lion-fieldset>
|
</lion-fieldset>
|
||||||
`,
|
`,
|
||||||
)
|
)
|
||||||
|
.add('Disabled', () => {
|
||||||
|
function toggleDisabled() {
|
||||||
|
const fieldset = document.querySelector('#fieldset');
|
||||||
|
fieldset.disabled = !fieldset.disabled;
|
||||||
|
}
|
||||||
|
return html`
|
||||||
|
<lion-fieldset name="nameGroup" label="Name" id="fieldset" disabled>
|
||||||
|
<lion-input name="FirstName" label="First Name" .modelValue=${'Foo'}></lion-input>
|
||||||
|
<lion-input name="LastName" label="Last Name" .modelValue=${'Bar'}></lion-input>
|
||||||
|
<lion-fieldset name="nameGroup2" label="Name">
|
||||||
|
<lion-input
|
||||||
|
name="FirstName2"
|
||||||
|
label="First Name"
|
||||||
|
.modelValue=${'Foo'}
|
||||||
|
disabled
|
||||||
|
></lion-input>
|
||||||
|
<lion-input name="LastName2" label="Last Name" .modelValue=${'Bar'}></lion-input>
|
||||||
|
</lion-fieldset>
|
||||||
|
</lion-fieldset>
|
||||||
|
<button @click=${toggleDisabled}>
|
||||||
|
Toggle disabled
|
||||||
|
</button>
|
||||||
|
`;
|
||||||
|
})
|
||||||
.add(
|
.add(
|
||||||
'Sub Fieldsets Data',
|
'Sub Fieldsets Data',
|
||||||
() => html`
|
() => html`
|
||||||
|
|
|
||||||
|
|
@ -223,12 +223,11 @@ describe('<lion-fieldset>', () => {
|
||||||
expect(el.formElements['hobbies[]'][1].disabled).to.equal(false);
|
expect(el.formElements['hobbies[]'][1].disabled).to.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not propagate/override inital disabled value on nested form elements', async () => {
|
it('does not propagate/override initial disabled value on nested form elements', async () => {
|
||||||
const el = await fixture(
|
const el = await fixture(
|
||||||
`<${tagString}><${tagString} name="sub" disabled>${inputSlotString}</${tagString}></${tagString}>`,
|
`<${tagString}><${tagString} name="sub" disabled>${inputSlotString}</${tagString}></${tagString}>`,
|
||||||
);
|
);
|
||||||
await nextFrame();
|
await el.updateComplete;
|
||||||
|
|
||||||
expect(el.disabled).to.equal(false);
|
expect(el.disabled).to.equal(false);
|
||||||
expect(el.formElements.sub.disabled).to.equal(true);
|
expect(el.formElements.sub.disabled).to.equal(true);
|
||||||
expect(el.formElements.sub.formElements.color.disabled).to.equal(true);
|
expect(el.formElements.sub.formElements.color.disabled).to.equal(true);
|
||||||
|
|
@ -236,6 +235,16 @@ describe('<lion-fieldset>', () => {
|
||||||
expect(el.formElements.sub.formElements['hobbies[]'][1].disabled).to.equal(true);
|
expect(el.formElements.sub.formElements['hobbies[]'][1].disabled).to.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// classes are added only for backward compatibility - they are deprecated
|
||||||
|
it('sets a state-disabled class when disabled', async () => {
|
||||||
|
const el = await fixture(`<${tagString} disabled>${inputSlotString}</${tagString}>`);
|
||||||
|
await nextFrame();
|
||||||
|
expect(el.classList.contains('state-disabled')).to.equal(true);
|
||||||
|
el.disabled = false;
|
||||||
|
await nextFrame();
|
||||||
|
expect(el.classList.contains('state-disabled')).to.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
describe('validation', () => {
|
describe('validation', () => {
|
||||||
it('validates on init', async () => {
|
it('validates on init', async () => {
|
||||||
function isCat(value) {
|
function isCat(value) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue