Skip to content

Commit 5e236eb

Browse files
committed
feat(value): set value based on the type
1 parent 9fefba2 commit 5e236eb

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/client/scripts/rb-checkbox.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import Type from '../../rb-base/scripts/type-service.js';
77
import template from '../views/rb-checkbox.html';
88

99
export class RbCheckbox extends FormControl(RbBase()) {
10+
11+
constructor() {
12+
super();
13+
this.state = {
14+
value: undefined
15+
}
16+
}
1017
/* Lifecycle
1118
************/
1219
viewReady() { // :void
@@ -40,12 +47,13 @@ export class RbCheckbox extends FormControl(RbBase()) {
4047
case /^{[^]*}$/.test(val): // object
4148
newVal = JSON.parse(val);
4249
break;
43-
case /^-?\d*\.?\d*$/.test(val): //number
50+
case /^-?\d*\.?\d*$/.test(val): // number
4451
newVal = parseFloat(val)
4552
break;
4653
default: // string
4754
newVal = val;
4855
}
56+
4957
return newVal;
5058
}
5159
})
@@ -59,8 +67,10 @@ export class RbCheckbox extends FormControl(RbBase()) {
5967
return code.toLowerCase();
6068
}
6169
async setValue(value) { // :void
62-
this.value = !value;
63-
// await this.validate();
70+
if (this.value === undefined || this.state.value === undefined) return this.value = true;
71+
if (typeof(value) === 'boolean') return this.value = !value;
72+
if (!!this.value) return this.value = null;
73+
this.value = this.state.value;
6474
}
6575

6676
/* Observer
@@ -72,10 +82,17 @@ export class RbCheckbox extends FormControl(RbBase()) {
7282
});
7383
}
7484

85+
updated(prevProps, prevState) {
86+
super.updated && super.updated(prevProps, prevState);
87+
if (!!this.state.value) return;
88+
this.state.value = this.value;
89+
}
90+
7591
/* Event Handlers
7692
*****************/
77-
_onclick(value, evt) { // :void
93+
async _onclick(value, evt) { // :void
7894
this.setValue(value);
95+
await this.validate()
7996
}
8097
_onkeypress(value, evt) { // :void
8198
const keys = ['enter','space'];

0 commit comments

Comments
 (0)