Skip to content

Commit fde538a

Browse files
authored
fix: allow to set default value for readOnly property (#6460)
1 parent c280591 commit fde538a

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎packages/component-base/src/polylit-mixin.js‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,6 @@ const PolylitMixinImplementation = (superclass) => {
9393

9494
let result = defaultDescriptor;
9595

96-
if ('value' in options) {
97-
// Set the default value
98-
this.addCheckedInitializer((instance) => {
99-
if (typeof options.value === 'function') {
100-
instance[name] = options.value.call(instance);
101-
} else {
102-
instance[name] = options.value;
103-
}
104-
});
105-
}
106-
10796
if (options.sync) {
10897
result = {
10998
get: defaultDescriptor.get,
@@ -146,6 +135,19 @@ const PolylitMixinImplementation = (superclass) => {
146135
};
147136
}
148137

138+
if ('value' in options) {
139+
// Set the default value
140+
this.addCheckedInitializer((instance) => {
141+
const value = typeof options.value === 'function' ? options.value.call(instance) : options.value;
142+
143+
if (options.readOnly) {
144+
instance[`_set${upper(name)}`](value);
145+
} else {
146+
instance[name] = value;
147+
}
148+
});
149+
}
150+
149151
if (options.observer) {
150152
const method = options.observer;
151153

‎packages/component-base/test/polylit-mixin.test.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ describe('PolylitMixin', () => {
176176
type: String,
177177
readOnly: true,
178178
},
179+
180+
opened: {
181+
type: Boolean,
182+
value: false,
183+
readOnly: true,
184+
},
179185
};
180186
}
181187

@@ -210,6 +216,10 @@ describe('PolylitMixin', () => {
210216
expect(element.helper).to.equal('bar');
211217
expect(element.shadowRoot.textContent).to.equal('bar');
212218
});
219+
220+
it('should set initial value for the property marked as readOnly', () => {
221+
expect(element.opened).to.equal(false);
222+
});
213223
});
214224

215225
describe('observer', () => {

0 commit comments

Comments
 (0)