From ca15c9832e6678d55c24a470c581183ef280bea6 Mon Sep 17 00:00:00 2001 From: afc163 Date: Wed, 9 Mar 2016 21:08:08 +0800 Subject: [PATCH] CheckboxGroup support label in option close #1155, #1025 --- components/checkbox/Group.jsx | 24 ++++++++++++++++++------ components/checkbox/demo/group.md | 20 ++++++++++++++++++-- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/components/checkbox/Group.jsx b/components/checkbox/Group.jsx index 43d1bc39ea..02995db992 100644 --- a/components/checkbox/Group.jsx +++ b/components/checkbox/Group.jsx @@ -32,11 +32,23 @@ export default React.createClass({ }); } }, + getOptions() { + const { options } = this.props; + return options.map(option => { + if (typeof option === 'string') { + return { + label: option, + value: option, + }; + } + return option; + }); + }, toggleOption(option) { - const optionIndex = this.state.value.indexOf(option); + const optionIndex = this.state.value.indexOf(option.value); const value = [...this.state.value]; if (optionIndex === - 1) { - value.push(option); + value.push(option.value); } else { value.splice(optionIndex, 1); } @@ -46,16 +58,16 @@ export default React.createClass({ this.props.onChange(value); }, render() { - const options = this.props.options; + const options = this.getOptions(); return (
{ options.map(option => - {option} + className="ant-checkbox-group-item" key={option.value}> + {option.label} ) } diff --git a/components/checkbox/demo/group.md b/components/checkbox/demo/group.md index b7dacc4f69..eb0d77ca03 100644 --- a/components/checkbox/demo/group.md +++ b/components/checkbox/demo/group.md @@ -2,7 +2,7 @@ - order: 3 -方便的从数组生成 Checkbox 组。若需要 label 和 value 分离请直接使用 Checkbox。 +方便的从数组生成 Checkbox 组。 --- @@ -14,7 +14,23 @@ function onChange(checkedValues) { console.log('checked = ', checkedValues); } +const plainOptions = ['Apple', 'Pear', 'Orange']; +const options = [{ + label: '苹果', + value: 'Apple', +}, { + label: '梨', + value: 'Pear', +}, { + label: '橘', + value: 'Orange', +}]; + ReactDOM.render( - +
+ +
+ +
, mountNode); ````