You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.1 KiB

9 years ago
import React from 'react';
import Cascader from 'rc-cascader';
import Input from '../input';
9 years ago
import Icon from '../icon';
9 years ago
import arrayTreeFilter from 'array-tree-filter';
import classNames from 'classnames';
9 years ago
class AntCascader extends React.Component {
constructor(props) {
super(props);
this.state = {
9 years ago
value: props.value || props.defaultValue || [],
popupVisible: false,
9 years ago
};
[
'handleChange',
9 years ago
'handlePopupVisibleChange',
'setValue',
9 years ago
'getLabel',
9 years ago
'clearSelection',
9 years ago
].forEach((method) => this[method] = this[method].bind(this));
}
9 years ago
componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({ value: nextProps.value || [] });
9 years ago
}
}
9 years ago
handleChange(value, selectedOptions) {
9 years ago
this.setValue(value, selectedOptions);
}
handlePopupVisibleChange(popupVisible) {
this.setState({ popupVisible });
this.props.onPopupVisibleChange(popupVisible);
}
setValue(value, selectedOptions = []) {
if (!('value' in this.props)) {
this.setState({ value });
}
9 years ago
this.props.onChange(value, selectedOptions);
}
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);
}
9 years ago
clearSelection(e) {
e.preventDefault();
9 years ago
e.stopPropagation();
9 years ago
this.setValue([]);
this.setState({ popupVisible: false });
}
9 years ago
render() {
const { prefixCls, children, placeholder, size, disabled, className } = this.props;
const sizeCls = classNames({
'ant-input-lg': size === 'large',
'ant-input-sm': size === 'small',
});
9 years ago
const clearIcon = this.state.value.length > 0 ?
<Icon type="cross-circle"
className={`${prefixCls}-picker-clear`}
onClick={this.clearSelection} /> : null;
9 years ago
const arrowCls = classNames({
[`${prefixCls}-picker-arrow`]: true,
[`${prefixCls}-picker-arrow-expand`]: this.state.popupVisible,
});
const pickerCls = classNames({
[className]: !!className,
[`${prefixCls}-picker`]: true,
[`${prefixCls}-picker-disabled`]: disabled,
});
9 years ago
return (
9 years ago
<Cascader {...this.props}
value={this.state.value}
popupVisible={this.state.popupVisible}
onPopupVisibleChange={this.handlePopupVisibleChange}
onChange={this.handleChange}>
9 years ago
{children ||
<span
{...this.props}
className={pickerCls}>
9 years ago
<Input placeholder={placeholder}
className={`${prefixCls}-input ant-input ${sizeCls}`}
style={{ width: '100%' }}
9 years ago
value={this.getLabel()}
disabled={disabled}
9 years ago
readOnly />
{clearIcon}
9 years ago
<Icon type="down" className={arrowCls} />
9 years ago
</span>
}
9 years ago
</Cascader>
);
}
}
AntCascader.defaultProps = {
prefixCls: 'ant-cascader',
placeholder: '请选择',
transitionName: 'slide-up',
onChange() {},
options: [],
displayRender(label) {
return label.join(' / ');
},
disabled: false,
9 years ago
onPopupVisibleChange() {},
9 years ago
};
export default AntCascader;