diff --git a/packages/checkbox/demo/index.vue b/packages/checkbox/demo/index.vue index 401f5d9cc..37c2d346d 100644 --- a/packages/checkbox/demo/index.vue +++ b/packages/checkbox/demo/index.vue @@ -9,8 +9,14 @@ {{ $t('checkbox') }} + + + {{ $t('customColor') }} + + + - + {{ $t('customIcon') }} Checkbox ``` +#### Custom Color + +```html +Checkbox +``` + #### Custom Icon + Use icon slot to custom icon ```html @@ -58,6 +65,7 @@ export default { ``` #### Checkbox Group + When Checkboxes are inside a CheckboxGroup, the checked checkboxes's name is an array and bound with CheckboxGroup by v-model. ```html @@ -131,11 +139,12 @@ export default { | Attribute | Description | Type | Default | |------|------|------|------| | name | Checkbox name | `any` | - | +| shape | Can be set to `round` `square` | `String` | `round` | | v-model | Check status | `Boolean` | `false` | | disabled | Diable checkbox | `Boolean` | `false` | | label-disabled | Whether to disable label click | `Boolean` | `false` | | label-position | Can be set to `left` | `String` | `right` | -| shape | Can be set to `round` `square` | `String` | `round` | +| checked-color | Checked color | `String` | `#1989fa` | - | ### CheckboxGroup API diff --git a/packages/checkbox/index.vue b/packages/checkbox/index.vue index 2af8eddfc..5741108de 100644 --- a/packages/checkbox/index.vue +++ b/packages/checkbox/index.vue @@ -2,7 +2,7 @@
- +
@@ -24,6 +24,7 @@ export default create({ name: null, value: null, disabled: Boolean, + checkedColor: String, labelPosition: String, labelDisabled: { type: Boolean, @@ -44,26 +45,8 @@ export default create({ }, set(val) { - const { parent } = this; - if (parent) { - const parentValue = this.parent.value.slice(); - if (val) { - if (parent.max && parentValue.length >= parent.max) { - return; - } - /* istanbul ignore else */ - if (parentValue.indexOf(this.name) === -1) { - parentValue.push(this.name); - parent.$emit('input', parentValue); - } - } else { - const index = parentValue.indexOf(this.name); - /* istanbul ignore else */ - if (index !== -1) { - parentValue.splice(index, 1); - parent.$emit('input', parentValue); - } - } + if (this.parent) { + this.setParentValue(val); } else { this.$emit('input', val); } @@ -72,6 +55,16 @@ export default create({ isDisabled() { return (this.parent && this.parent.disabled) || this.disabled; + }, + + iconStyle() { + const { checkedColor } = this; + if (checkedColor && this.checked && !this.isDisabled) { + return { + borderColor: checkedColor, + backgroundColor: checkedColor + }; + } } }, @@ -90,6 +83,31 @@ export default create({ if (!this.isDisabled && !(target === 'label' && this.labelDisabled)) { this.checked = !this.checked; } + }, + + setParentValue(val) { + const { parent } = this; + const value = parent.value.slice(); + + if (val) { + if (parent.max && value.length >= parent.max) { + return; + } + + /* istanbul ignore else */ + if (value.indexOf(this.name) === -1) { + value.push(this.name); + parent.$emit('input', value); + } + } else { + const index = value.indexOf(this.name); + + /* istanbul ignore else */ + if (index !== -1) { + value.splice(index, 1); + parent.$emit('input', value); + } + } } } }); diff --git a/packages/checkbox/test/__snapshots__/demo.spec.js.snap b/packages/checkbox/test/__snapshots__/demo.spec.js.snap index 8f1dee5e9..cb0e02430 100644 --- a/packages/checkbox/test/__snapshots__/demo.spec.js.snap +++ b/packages/checkbox/test/__snapshots__/demo.spec.js.snap @@ -24,6 +24,16 @@ exports[`renders demo correctly 1`] = `
复选框 +
+
+
+ + + +
+ 自定义颜色 +
+
diff --git a/packages/checkbox/zh-CN.md b/packages/checkbox/zh-CN.md index 59f069efa..332325ed5 100644 --- a/packages/checkbox/zh-CN.md +++ b/packages/checkbox/zh-CN.md @@ -32,7 +32,14 @@ export default { 复选框 ``` +#### 自定义颜色 + +```html +复选框 +``` + #### 自定义图标 + 通过 icon 插槽自定义图标,可以通过 `slot-scope` 判断是否为选中状态 ```html @@ -58,9 +65,9 @@ export default { } ``` -#### Checkbox 组 +#### 复选框组 -需要与`van-checkbox-group`一起使用,选中值是一个数组,通过`v-model`绑定在`van-checkbox-group`上,数组中的项即为选中的`Checkbox`的`name`属性设置的值 +与`van-checkbox-group`一起使用,选中值是一个数组,通过`v-model`绑定在`van-checkbox-group`上,数组中的项即为选中的`Checkbox`的`name`属性设置的值 ```html @@ -99,7 +106,7 @@ export default { ``` -#### 与 Cell 组件一起使用 +#### 搭配单元格组件使用 此时你需要再引入`Cell`和`CellGroup`组件,并通过 checkbox 的 toggle 方法手动触发切换 @@ -139,6 +146,7 @@ export default { | disabled | 是否禁用单选框 | `Boolean` | `false` | - | | label-disabled | 是否禁用单选框文本点击 | `Boolean` | `false` | - | | label-position | 文本位置,可选值为 `left` | `String` | `right` | 1.1.11 | +| checked-color | 选中状态颜色 | `String` | `#1989fa` | 1.4.3 | ### CheckboxGroup API