From 7661c378c9292dfa255f6cdaad5c523bdc23151e Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sat, 29 Sep 2018 19:29:29 +0200 Subject: [PATCH] Added `valueTrue` and `valueFalse` to trait checkbox type. Closes #1424 This feature allows you to customize the boolean attribute on components Example ```js traits: [{ type: 'checkbox', label: 'Show Name', name: 'show-name', valueTrue: 'TRUE-value', valueFalse: 'FALSE-value', // For default values you have to specify same true/false values // You can add `value` in trait or on your components value: 'FALSE-value', }] ``` --- src/trait_manager/view/TraitCheckboxView.js | 31 ++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/trait_manager/view/TraitCheckboxView.js b/src/trait_manager/view/TraitCheckboxView.js index fd706b1b3..c11c6da6b 100644 --- a/src/trait_manager/view/TraitCheckboxView.js +++ b/src/trait_manager/view/TraitCheckboxView.js @@ -1,3 +1,4 @@ +import { isUndefined } from 'underscore'; var TraitView = require('./TraitView'); module.exports = TraitView.extend({ @@ -16,7 +17,23 @@ module.exports = TraitView.extend({ * @private */ onChange() { - this.model.set('value', this.getInputEl().checked); + const value = this.getInputEl().checked; + this.model.set('value', this.getCheckedValue(value)); + }, + + getCheckedValue(checked) { + let result = checked; + const { valueTrue, valueFalse } = this.model.attributes; + + if (result && !isUndefined(valueTrue)) { + result = valueTrue; + } + + if (!result && !isUndefined(valueFalse)) { + result = valueFalse; + } + + return result; }, /** @@ -29,15 +46,21 @@ module.exports = TraitView.extend({ const el = TraitView.prototype.getInputEl.apply(this, args); if (toInit) { - let checked; + let checked, targetValue; const { model, target } = this; + const { valueTrue, valueFalse } = model.attributes; const name = model.get('name'); if (model.get('changeProp')) { checked = target.get(name); + targetValue = checked; } else { - checked = target.get('attributes')[name]; - checked = checked || checked === '' ? !0 : !1; + targetValue = target.get('attributes')[name]; + checked = targetValue || targetValue === '' ? !0 : !1; + } + + if (!isUndefined(valueFalse) && targetValue === valueFalse) { + checked = !1; } el.checked = checked;