Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(input-datepicker): disable invoker when readonly #105

Merged
merged 1 commit into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/input-datepicker/src/LionInputDatepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,28 @@ export class LionInputDatepicker extends LionInputDate {
*/
_requestUpdate(name, oldValue) {
super._requestUpdate(name, oldValue);

if (name === 'disabled') {
this.__delegateDisabled();
tlouisse marked this conversation as resolved.
Show resolved Hide resolved
}

if (name === 'disabled' || name === 'readOnly') {
this.__toggleInvokerDisabled();
}
}

/**
* TODO: [delegation of disabled] move this to LionField (or FormControl) level
*/
__delegateDisabled() {
if (this.delegations.target()) {
this.delegations.target().disabled = this.disabled;
if (this.inputElement) {
this.inputElement.disabled = this.disabled;
}
}

__toggleInvokerDisabled() {
if (this._invokerElement) {
this._invokerElement.disabled = this.disabled;
this._invokerElement.disabled = this.disabled || this.readOnly;
}
}
tlouisse marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -220,6 +228,7 @@ export class LionInputDatepicker extends LionInputDate {
firstUpdated(c) {
super.firstUpdated(c);
this.__delegateDisabled();
this.__toggleInvokerDisabled();
}

/**
Expand Down
13 changes: 12 additions & 1 deletion packages/input-datepicker/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ storiesOf('Forms|Input Datepicker', module)
.add(
'Disabled',
() => html`
<lion-input-datepicker disabled></lion-input-datepicker>
<lion-input-datepicker label="Disabled" disabled></lion-input-datepicker>
`,
)
.add(
'Readonly',
() => html`
<lion-input-datepicker
help-text="Notice that it's not possible to open the calendar on readonly inputs"
label="Readonly"
readonly
.modelValue="${new Date()}"
></lion-input-datepicker>
`,
);
16 changes: 16 additions & 0 deletions packages/input-datepicker/test/lion-input-datepicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ describe('<lion-input-datepicker>', () => {
expect(elObj.overlayController.isShown).to.equal(false);
await elObj.openCalendar();
expect(elObj.overlayController.isShown).to.equal(false);
el.disabled = false;
await elObj.openCalendar();
expect(elObj.overlayController.isShown).to.equal(true);
});

it('disables invoker when host input is readonly', async () => {
const el = await fixture(html`
<lion-input-datepicker readonly></lion-input-datepicker>
`);
const elObj = new DatepickerInputObject(el);
expect(elObj.overlayController.isShown).to.equal(false);
await elObj.openCalendar();
expect(elObj.overlayController.isShown).to.equal(false);
el.readOnly = false;
await elObj.openCalendar();
expect(elObj.overlayController.isShown).to.equal(true);
});
});

Expand Down
37 changes: 32 additions & 5 deletions packages/input/src/LionInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ import { LionField } from '@lion/field';
* @extends LionField
*/
export class LionInput extends LionField {
get delegations() {
static get properties() {
return {
...super.delegations,
target: () => this.inputElement,
properties: [...super.delegations.properties, 'readOnly'],
attributes: [...super.delegations.attributes, 'readonly'],
bashmish marked this conversation as resolved.
Show resolved Hide resolved
...super.properties,
/**
* A Boolean attribute which, if present, indicates that the user should not be able to edit
* the value of the input. The difference between disabled and readonly is that read-only
* controls can still function, whereas disabled controls generally do not function as
* controls until they are enabled.
*
* (From: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-readonly)
*/
readOnly: {
type: Boolean,
attribute: 'readonly',
},
tlouisse marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand All @@ -30,4 +39,22 @@ export class LionInput extends LionField {
},
};
}

_requestUpdate(name, oldValue) {
super._requestUpdate(name, oldValue);
if (name === 'readOnly') {
this.__delegateReadOnly();
}
}

firstUpdated(c) {
super.firstUpdated(c);
this.__delegateReadOnly();
}

__delegateReadOnly() {
if (this.inputElement) {
this.inputElement.readOnly = this.readOnly;
}
}
}