From 159b28a7a2624f340cf54e9e324f245b6f964f40 Mon Sep 17 00:00:00 2001 From: afc163 Date: Wed, 18 May 2016 15:34:30 +0800 Subject: [PATCH] Support custom label node render in Cascader, close #1726 --- components/cascader/demo/custom-render.md | 61 +++++++++++++++++++++++ components/cascader/index.jsx | 12 ++--- components/cascader/index.md | 2 +- components/cascader/style/index.less | 15 ++++++ 4 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 components/cascader/demo/custom-render.md diff --git a/components/cascader/demo/custom-render.md b/components/cascader/demo/custom-render.md new file mode 100644 index 0000000000..67ea032c61 --- /dev/null +++ b/components/cascader/demo/custom-render.md @@ -0,0 +1,61 @@ +--- +order: 8 +title: 自定义已选项 +--- + +例如给最后一项加上邮编链接。 + +````jsx +import { Cascader } from 'antd'; + +const options = [{ + value: 'zhejiang', + label: '浙江', + children: [{ + value: 'hangzhou', + label: '杭州', + children: [{ + value: 'xihu', + label: '西湖', + code: 752100, + }], + }], +}, { + value: 'jiangsu', + label: '江苏', + children: [{ + value: 'nanjing', + label: '南京', + children: [{ + value: 'zhonghuamen', + label: '中华门', + code: 453400, + }], + }], +}]; + +function handleAreaClick(e, label, option) { + e.stopPropagation(); + console.log('点击了', label, option); +} + +const displayRender = (labels, selectedOptions) => labels.map((label, i) => { + const option = selectedOptions[i]; + if (i === labels.length - 1) { + return ( + + {label} ( handleAreaClick(e, label, option)}>{option.code}) + + ); + } + return {label} / ; +}); + +ReactDOM.render( + +, mountNode); +```` diff --git a/components/cascader/index.jsx b/components/cascader/index.jsx index 33be722150..e86e66580e 100644 --- a/components/cascader/index.jsx +++ b/components/cascader/index.jsx @@ -57,9 +57,9 @@ export default class Cascader extends React.Component { getLabel() { const { options, displayRender } = this.props; - const label = arrayTreeFilter(options, (o, level) => o.value === this.state.value[level]) - .map(o => o.label); - return displayRender(label); + const selectedOptions = arrayTreeFilter(options, (o, level) => o.value === this.state.value[level]); + const label = selectedOptions.map(o => o.label); + return displayRender(label, selectedOptions); } clearSelection = (e) => { @@ -105,12 +105,12 @@ export default class Cascader extends React.Component { style={style} className={pickerCls}> 0 ? null : placeholder} className={`${prefixCls}-input ant-input ${sizeCls}`} - style={{ width: '100%' }} - value={this.getLabel()} + value={null} disabled={disabled} readOnly /> + {this.getLabel()} {clearIcon} diff --git a/components/cascader/index.md b/components/cascader/index.md index d8c0558b5d..356267b7bf 100644 --- a/components/cascader/index.md +++ b/components/cascader/index.md @@ -26,7 +26,7 @@ english: Cascader | defaultValue | 默认的选中项 | array |[] | | value | 指定选中项 | array | - | | onChange | 选择完成后的回调 | `function(value, selectedOptions)` | - | -| displayRender | 选择后展示的渲染函数 | `function(label)` | `label => label.join(' / ')` | +| displayRender | 选择后展示的渲染函数 | `function(label, selectedOptions)` | `label => label.join(' / ')` | | style | 自定义样式 | string | - | | className | 自定义类名 | string | - | | popupClassName | 自定义浮层类名 | string | - | diff --git a/components/cascader/style/index.less b/components/cascader/style/index.less index a3b5d7d7b5..0951ce8fe1 100644 --- a/components/cascader/style/index.less +++ b/components/cascader/style/index.less @@ -8,17 +8,31 @@ &-input { display: block; cursor: pointer; + width: 100%; } &-picker { position: relative; display: inline-block; cursor: pointer; vertical-align: middle; + font-size: @font-size-base; + overflow: hidden; &-disabled { cursor: not-allowed; } + &-label { + position: absolute; + top: 0; + left: 8px; + height: 20px; + line-height: 20px; + top: 50%; + margin-top: -10px; + white-space: nowrap; + } + &-clear { opacity: 0; position: absolute; @@ -53,6 +67,7 @@ margin-top: -6px; line-height: 12px; color: #999; + background: #fff; .iconfont-size-under-12px(8px); &:before { transition: transform 0.2s ease;