Skip to content

Commit 5cd9452

Browse files
tlouisseMikhail Bashkirov
authored andcommitted
fix(input-datepicker): disable invoker when readonly
1 parent c58f95e commit 5cd9452

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

packages/input-datepicker/src/LionInputDatepicker.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,28 @@ export class LionInputDatepicker extends LionInputDate {
197197
*/
198198
_requestUpdate(name, oldValue) {
199199
super._requestUpdate(name, oldValue);
200+
200201
if (name === 'disabled') {
201202
this.__delegateDisabled();
202203
}
204+
205+
if (name === 'disabled' || name === 'readOnly') {
206+
this.__toggleInvokerDisabled();
207+
}
203208
}
204209

205210
/**
206211
* TODO: [delegation of disabled] move this to LionField (or FormControl) level
207212
*/
208213
__delegateDisabled() {
209-
if (this.delegations.target()) {
210-
this.delegations.target().disabled = this.disabled;
214+
if (this.inputElement) {
215+
this.inputElement.disabled = this.disabled;
211216
}
217+
}
218+
219+
__toggleInvokerDisabled() {
212220
if (this._invokerElement) {
213-
this._invokerElement.disabled = this.disabled;
221+
this._invokerElement.disabled = this.disabled || this.readOnly;
214222
}
215223
}
216224

@@ -220,6 +228,7 @@ export class LionInputDatepicker extends LionInputDate {
220228
firstUpdated(c) {
221229
super.firstUpdated(c);
222230
this.__delegateDisabled();
231+
this.__toggleInvokerDisabled();
223232
}
224233

225234
/**

packages/input-datepicker/stories/index.stories.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ storiesOf('Forms|Input Datepicker', module)
4949
.add(
5050
'Disabled',
5151
() => html`
52-
<lion-input-datepicker disabled></lion-input-datepicker>
52+
<lion-input-datepicker label="Disabled" disabled></lion-input-datepicker>
53+
`,
54+
)
55+
.add(
56+
'Readonly',
57+
() => html`
58+
<lion-input-datepicker
59+
help-text="Notice that it's not possible to open the calendar on readonly inputs"
60+
label="Readonly"
61+
readonly
62+
.modelValue="${new Date()}"
63+
></lion-input-datepicker>
5364
`,
5465
);

packages/input-datepicker/test/lion-input-datepicker.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ describe('<lion-input-datepicker>', () => {
128128
expect(elObj.overlayController.isShown).to.equal(false);
129129
await elObj.openCalendar();
130130
expect(elObj.overlayController.isShown).to.equal(false);
131+
el.disabled = false;
132+
await elObj.openCalendar();
133+
expect(elObj.overlayController.isShown).to.equal(true);
134+
});
135+
136+
it('disables invoker when host input is readonly', async () => {
137+
const el = await fixture(html`
138+
<lion-input-datepicker readonly></lion-input-datepicker>
139+
`);
140+
const elObj = new DatepickerInputObject(el);
141+
expect(elObj.overlayController.isShown).to.equal(false);
142+
await elObj.openCalendar();
143+
expect(elObj.overlayController.isShown).to.equal(false);
144+
el.readOnly = false;
145+
await elObj.openCalendar();
146+
expect(elObj.overlayController.isShown).to.equal(true);
131147
});
132148
});
133149

packages/input/src/LionInput.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ import { LionField } from '@lion/field';
77
* @extends LionField
88
*/
99
export class LionInput extends LionField {
10-
get delegations() {
10+
static get properties() {
1111
return {
12-
...super.delegations,
13-
target: () => this.inputElement,
14-
properties: [...super.delegations.properties, 'readOnly'],
15-
attributes: [...super.delegations.attributes, 'readonly'],
12+
...super.properties,
13+
/**
14+
* A Boolean attribute which, if present, indicates that the user should not be able to edit
15+
* the value of the input. The difference between disabled and readonly is that read-only
16+
* controls can still function, whereas disabled controls generally do not function as
17+
* controls until they are enabled.
18+
*
19+
* (From: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-readonly)
20+
*/
21+
readOnly: {
22+
type: Boolean,
23+
attribute: 'readonly',
24+
},
1625
};
1726
}
1827

@@ -30,4 +39,22 @@ export class LionInput extends LionField {
3039
},
3140
};
3241
}
42+
43+
_requestUpdate(name, oldValue) {
44+
super._requestUpdate(name, oldValue);
45+
if (name === 'readOnly') {
46+
this.__delegateReadOnly();
47+
}
48+
}
49+
50+
firstUpdated(c) {
51+
super.firstUpdated(c);
52+
this.__delegateReadOnly();
53+
}
54+
55+
__delegateReadOnly() {
56+
if (this.inputElement) {
57+
this.inputElement.readOnly = this.readOnly;
58+
}
59+
}
3360
}

0 commit comments

Comments
 (0)