Browse Source

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',
}]
```
pull/1518/head
Artur Arseniev 8 years ago
parent
commit
7661c378c9
  1. 31
      src/trait_manager/view/TraitCheckboxView.js

31
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;

Loading…
Cancel
Save