|
@ -1,6 +1,7 @@ |
|
|
import React from 'react'; |
|
|
import React from 'react'; |
|
|
import Cascader from 'rc-cascader'; |
|
|
import Cascader from 'rc-cascader'; |
|
|
import Input from '../input'; |
|
|
import Input from '../input'; |
|
|
|
|
|
import Icon from '../icon'; |
|
|
import arrayTreeFilter from 'array-tree-filter'; |
|
|
import arrayTreeFilter from 'array-tree-filter'; |
|
|
import classNames from 'classnames'; |
|
|
import classNames from 'classnames'; |
|
|
|
|
|
|
|
@ -8,15 +9,33 @@ class AntCascader extends React.Component { |
|
|
constructor(props) { |
|
|
constructor(props) { |
|
|
super(props); |
|
|
super(props); |
|
|
this.state = { |
|
|
this.state = { |
|
|
value: [], |
|
|
value: props.value || props.defaultValue || [], |
|
|
|
|
|
popupVisible: false, |
|
|
}; |
|
|
}; |
|
|
[ |
|
|
[ |
|
|
'handleChange', |
|
|
'handleChange', |
|
|
|
|
|
'handlePopupVisibleChange', |
|
|
|
|
|
'setValue', |
|
|
'getLabel', |
|
|
'getLabel', |
|
|
|
|
|
'clearSelection', |
|
|
].forEach((method) => this[method] = this[method].bind(this)); |
|
|
].forEach((method) => this[method] = this[method].bind(this)); |
|
|
} |
|
|
} |
|
|
|
|
|
componentWillReceiveProps(nextProps) { |
|
|
|
|
|
if ('value' in nextProps) { |
|
|
|
|
|
this.setState({ value: nextProps.value }); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
handleChange(value, selectedOptions) { |
|
|
handleChange(value, selectedOptions) { |
|
|
this.setState({ value }); |
|
|
this.setValue(value, selectedOptions); |
|
|
|
|
|
} |
|
|
|
|
|
handlePopupVisibleChange(popupVisible) { |
|
|
|
|
|
this.setState({ popupVisible }); |
|
|
|
|
|
this.props.onPopupVisibleChange(popupVisible); |
|
|
|
|
|
} |
|
|
|
|
|
setValue(value, selectedOptions = []) { |
|
|
|
|
|
if (!('value' in this.props)) { |
|
|
|
|
|
this.setState({ value }); |
|
|
|
|
|
} |
|
|
this.props.onChange(value, selectedOptions); |
|
|
this.props.onChange(value, selectedOptions); |
|
|
} |
|
|
} |
|
|
getLabel() { |
|
|
getLabel() { |
|
@ -25,20 +44,37 @@ class AntCascader extends React.Component { |
|
|
.map(o => o.label); |
|
|
.map(o => o.label); |
|
|
return displayRender(label); |
|
|
return displayRender(label); |
|
|
} |
|
|
} |
|
|
|
|
|
clearSelection(e) { |
|
|
|
|
|
e.preventDefault(); |
|
|
|
|
|
this.setValue([]); |
|
|
|
|
|
this.setState({ popupVisible: false }); |
|
|
|
|
|
} |
|
|
render() { |
|
|
render() { |
|
|
const { prefixCls, children, placeholder, style, size } = this.props; |
|
|
const { prefixCls, children, placeholder, style, size } = this.props; |
|
|
const sizeCls = classNames({ |
|
|
const sizeCls = classNames({ |
|
|
'ant-input-lg': size === 'large', |
|
|
'ant-input-lg': size === 'large', |
|
|
'ant-input-sm': size === 'small', |
|
|
'ant-input-sm': size === 'small', |
|
|
}); |
|
|
}); |
|
|
|
|
|
const clearIcon = this.state.value.length > 0 ? |
|
|
|
|
|
<Icon type="cross-circle" |
|
|
|
|
|
className={`${prefixCls}-picker-clear`} |
|
|
|
|
|
onClick={this.clearSelection} /> : null; |
|
|
return ( |
|
|
return ( |
|
|
<Cascader {...this.props} onChange={this.handleChange}> |
|
|
<Cascader {...this.props} |
|
|
|
|
|
value={this.state.value} |
|
|
|
|
|
popupVisible={this.state.popupVisible} |
|
|
|
|
|
onPopupVisibleChange={this.handlePopupVisibleChange} |
|
|
|
|
|
onChange={this.handleChange}> |
|
|
{children || |
|
|
{children || |
|
|
<Input placeholder={placeholder} |
|
|
<span className={`${prefixCls}-picker`}> |
|
|
className={`${prefixCls}-input ant-input ${sizeCls}`} |
|
|
<Input placeholder={placeholder} |
|
|
style={style} |
|
|
className={`${prefixCls}-input ant-input ${sizeCls}`} |
|
|
value={this.getLabel()} |
|
|
style={style} |
|
|
readOnly />} |
|
|
value={this.getLabel()} |
|
|
|
|
|
readOnly /> |
|
|
|
|
|
{clearIcon} |
|
|
|
|
|
</span> |
|
|
|
|
|
} |
|
|
</Cascader> |
|
|
</Cascader> |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
@ -54,6 +90,7 @@ AntCascader.defaultProps = { |
|
|
return label.join(' / '); |
|
|
return label.join(' / '); |
|
|
}, |
|
|
}, |
|
|
size: 'default', |
|
|
size: 'default', |
|
|
|
|
|
onPopupVisibleChange() {}, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
export default AntCascader; |
|
|
export default AntCascader; |
|
|