Skip to content

Commit 647a47d

Browse files
committed
fix(attribute): don't null boolean properties
1 parent 930061f commit 647a47d

5 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/core/attribute-changed.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export function attributeChangedCallback(attrPropsMap: {[attr: string]: string},
88
if (propName) {
99
// there is not need to cast the value since, it's already casted when
1010
// the prop is setted
11-
(elm as any)[propName] = newVal;
11+
(elm as any)[propName] = newVal === null && typeof (elm as any)[propName] === 'boolean'
12+
? false
13+
: newVal;
1214
}
1315
}

test/karma/test-app/components.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ export namespace Components {
218218

219219
interface ReflectToAttr {
220220
'bool': boolean;
221+
'disabled': boolean;
221222
'dynamicNu': number;
222223
'dynamicStr': string;
223224
'nu': number;
@@ -228,6 +229,7 @@ export namespace Components {
228229
}
229230
interface ReflectToAttrAttributes extends StencilHTMLAttributes {
230231
'bool'?: boolean;
232+
'disabled'?: boolean;
231233
'dynamicNu'?: number;
232234
'dynamicStr'?: string;
233235
'nu'?: number;

test/karma/test-app/reflect-to-attr/cmp.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class ReflectToAttr {
1313
@Prop({reflectToAttr: true}) null: string = null;
1414
@Prop({reflectToAttr: true}) bool = false;
1515
@Prop({reflectToAttr: true}) otherBool = true;
16+
@Prop({reflectToAttr: true}) disabled = false;
1617

1718
@Prop({reflectToAttr: true, mutable: true}) dynamicStr: string;
1819
@Prop({reflectToAttr: true}) dynamicNu: number;

test/karma/test-app/reflect-to-attr/index.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@
22
<meta charset="utf8">
33
<script src="/build/testapp.js"></script>
44

5-
<reflect-to-attr></reflect-to-attr>
5+
<reflect-to-attr></reflect-to-attr>
6+
7+
<button id="toggle" onclick="toggleDisabled()">Toggle disabled</button>
8+
9+
<script>
10+
function toggleDisabled() {
11+
const item = document.querySelector('reflect-to-attr');
12+
item.disabled = !item.disabled;
13+
}
14+
</script>

test/karma/test-app/reflect-to-attr/karma.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,18 @@ describe('reflect-to-attr', function() {
4141
expect(cmp.getAttribute('dynamic-nu')).toEqual('123');
4242
});
4343

44+
it('should reflect booleans property', async () => {
45+
const cmp = app.querySelector('reflect-to-attr') as any;
46+
expect(cmp.disabled).toBe(false);
47+
48+
const toggle = app.querySelector('#toggle') as any;
49+
toggle.click();
50+
await waitForChanges();
51+
expect(cmp.disabled).toBe(true);
52+
53+
toggle.click();
54+
await waitForChanges();
55+
expect(cmp.disabled).toBe(false);
56+
});
57+
4458
});

0 commit comments

Comments
 (0)