2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-03-21 21:16:38 +08:00
|
|
|
import RcCascader from 'rc-cascader';
|
2015-12-29 11:46:13 +08:00
|
|
|
import arrayTreeFilter from 'array-tree-filter';
|
2015-12-29 18:31:48 +08:00
|
|
|
import classNames from 'classnames';
|
2016-09-10 17:17:55 +08:00
|
|
|
import omit from 'omit.js';
|
2017-03-28 13:51:58 +08:00
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
2016-12-07 10:43:06 +08:00
|
|
|
import Input from '../input';
|
|
|
|
import Icon from '../icon';
|
2015-12-29 11:46:13 +08:00
|
|
|
|
2016-07-01 20:52:17 +08:00
|
|
|
export interface CascaderOptionType {
|
2018-05-04 17:27:34 +08:00
|
|
|
value?: string;
|
|
|
|
label?: React.ReactNode;
|
2016-07-13 11:14:24 +08:00
|
|
|
disabled?: boolean;
|
|
|
|
children?: Array<CascaderOptionType>;
|
2018-08-13 11:44:25 +08:00
|
|
|
|
2018-05-04 17:27:34 +08:00
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2018-07-03 19:54:33 +08:00
|
|
|
export interface FieldNamesType {
|
2018-05-04 17:27:34 +08:00
|
|
|
value?: string;
|
|
|
|
label?: string;
|
|
|
|
children?: string;
|
|
|
|
}
|
|
|
|
|
2018-07-03 19:54:33 +08:00
|
|
|
export interface FilledFieldNamesType {
|
2018-05-04 17:27:34 +08:00
|
|
|
value: string;
|
|
|
|
label: string;
|
|
|
|
children: string;
|
2016-07-01 20:52:17 +08:00
|
|
|
}
|
|
|
|
|
2017-03-23 21:15:49 +08:00
|
|
|
export type CascaderExpandTrigger = 'click' | 'hover';
|
2016-09-01 11:08:41 +08:00
|
|
|
|
|
|
|
export interface ShowSearchType {
|
2018-07-03 19:54:33 +08:00
|
|
|
filter?: (inputValue: string, path: CascaderOptionType[], names: FilledFieldNamesType) => boolean;
|
2018-05-04 17:27:34 +08:00
|
|
|
render?: (
|
|
|
|
inputValue: string,
|
|
|
|
path: CascaderOptionType[],
|
|
|
|
prefixCls: string | undefined,
|
2018-07-03 19:54:33 +08:00
|
|
|
names: FilledFieldNamesType,
|
2018-05-04 17:27:34 +08:00
|
|
|
) => React.ReactNode;
|
2018-07-03 19:54:33 +08:00
|
|
|
sort?: (a: CascaderOptionType[], b: CascaderOptionType[], inputValue: string, names: FilledFieldNamesType) => number;
|
2016-09-01 11:08:41 +08:00
|
|
|
matchInputWidth?: boolean;
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:25:03 +08:00
|
|
|
export interface CascaderProps {
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 可选项数据源 */
|
2016-12-22 16:35:37 +08:00
|
|
|
options: CascaderOptionType[];
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 默认的选中项 */
|
2017-11-21 21:52:40 +08:00
|
|
|
defaultValue?: string[];
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 指定选中项 */
|
2017-11-21 21:52:40 +08:00
|
|
|
value?: string[];
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 选择完成后的回调 */
|
2016-12-22 16:35:37 +08:00
|
|
|
onChange?: (value: string[], selectedOptions?: CascaderOptionType[]) => void;
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 选择后展示的渲染函数 */
|
2016-12-22 16:35:37 +08:00
|
|
|
displayRender?: (label: string[], selectedOptions?: CascaderOptionType[]) => React.ReactNode;
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 自定义样式 */
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
/** 自定义类名 */
|
|
|
|
className?: string;
|
|
|
|
/** 自定义浮层类名 */
|
|
|
|
popupClassName?: string;
|
2016-07-01 20:52:17 +08:00
|
|
|
/** 浮层预设位置:`bottomLeft` `bottomRight` `topLeft` `topRight` */
|
2016-07-13 11:14:24 +08:00
|
|
|
popupPlacement?: string;
|
2016-07-01 20:52:17 +08:00
|
|
|
/** 输入框占位文本*/
|
2016-07-13 11:14:24 +08:00
|
|
|
placeholder?: string;
|
2016-07-01 20:52:17 +08:00
|
|
|
/** 输入框大小,可选 `large` `default` `small` */
|
2016-07-13 11:14:24 +08:00
|
|
|
size?: string;
|
2016-07-01 20:52:17 +08:00
|
|
|
/** 禁用*/
|
2016-07-13 11:14:24 +08:00
|
|
|
disabled?: boolean;
|
2016-07-01 20:52:17 +08:00
|
|
|
/** 是否支持清除*/
|
2016-07-13 11:14:24 +08:00
|
|
|
allowClear?: boolean;
|
2016-09-01 11:08:41 +08:00
|
|
|
showSearch?: boolean | ShowSearchType;
|
|
|
|
notFoundContent?: React.ReactNode;
|
2016-12-22 16:35:37 +08:00
|
|
|
loadData?: (selectedOptions?: CascaderOptionType[]) => void;
|
2016-07-01 20:52:17 +08:00
|
|
|
/** 次级菜单的展开方式,可选 'click' 和 'hover' */
|
2016-07-13 11:14:24 +08:00
|
|
|
expandTrigger?: CascaderExpandTrigger;
|
2016-07-01 20:52:17 +08:00
|
|
|
/** 当此项为 true 时,点选每级菜单选项值都会发生变化 */
|
2016-07-13 11:14:24 +08:00
|
|
|
changeOnSelect?: boolean;
|
2016-07-01 20:52:17 +08:00
|
|
|
/** 浮层可见变化时回调 */
|
2016-07-13 11:14:24 +08:00
|
|
|
onPopupVisibleChange?: (popupVisible: boolean) => void;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
inputPrefixCls?: string;
|
2017-03-21 16:35:31 +08:00
|
|
|
getPopupContainer?: (triggerNode?: HTMLElement) => HTMLElement;
|
2017-11-21 21:52:40 +08:00
|
|
|
popupVisible?: boolean;
|
2018-11-06 12:36:03 +08:00
|
|
|
/** use this after antd@3.7.0 */
|
2018-07-03 19:54:33 +08:00
|
|
|
fieldNames?: FieldNamesType;
|
2018-11-06 12:36:03 +08:00
|
|
|
/** typo props name before antd@3.7.0 */
|
|
|
|
filedNames?: FieldNamesType;
|
2018-09-16 19:36:19 +08:00
|
|
|
suffixIcon?: React.ReactNode;
|
2016-07-01 20:52:17 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
export interface CascaderState {
|
|
|
|
inputFocused: boolean;
|
|
|
|
inputValue: string;
|
|
|
|
value: string[];
|
|
|
|
popupVisible: boolean | undefined;
|
2018-05-21 20:35:14 +08:00
|
|
|
flattenOptions: CascaderOptionType[][] | undefined;
|
2017-11-21 21:52:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function highlightKeyword(str: string, keyword: string, prefixCls: string | undefined) {
|
2016-09-01 11:08:41 +08:00
|
|
|
return str.split(keyword)
|
|
|
|
.map((node: string, index: number) => index === 0 ? node : [
|
2016-09-14 16:18:33 +08:00
|
|
|
<span className={`${prefixCls}-menu-item-keyword`} key="seperator">{keyword}</span>,
|
2016-09-01 11:08:41 +08:00
|
|
|
node,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-07-03 19:54:33 +08:00
|
|
|
function defaultFilterOption(inputValue: string, path: CascaderOptionType[], names: FilledFieldNamesType) {
|
2018-05-04 17:27:34 +08:00
|
|
|
return path.some(option => (option[names.label] as string).indexOf(inputValue) > -1);
|
2016-09-01 11:08:41 +08:00
|
|
|
}
|
|
|
|
|
2018-05-04 17:27:34 +08:00
|
|
|
function defaultRenderFilteredOption(
|
|
|
|
inputValue: string,
|
|
|
|
path: CascaderOptionType[],
|
|
|
|
prefixCls: string | undefined,
|
2018-07-03 19:54:33 +08:00
|
|
|
names: FilledFieldNamesType,
|
2018-05-04 17:27:34 +08:00
|
|
|
) {
|
|
|
|
return path.map((option, index) => {
|
|
|
|
const label = option[names.label];
|
2017-11-21 21:52:40 +08:00
|
|
|
const node = (label as string).indexOf(inputValue) > -1 ?
|
|
|
|
highlightKeyword(label as string, inputValue, prefixCls) : label;
|
2016-09-01 11:08:41 +08:00
|
|
|
return index === 0 ? node : [' / ', node];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-21 20:35:14 +08:00
|
|
|
function defaultSortFilteredOption(
|
2018-07-03 19:54:33 +08:00
|
|
|
a: CascaderOptionType[], b: CascaderOptionType[], inputValue: string, names: FilledFieldNamesType,
|
2018-05-21 20:35:14 +08:00
|
|
|
) {
|
2017-11-21 21:52:40 +08:00
|
|
|
function callback(elem: CascaderOptionType) {
|
2018-05-04 17:27:34 +08:00
|
|
|
return (elem[names.label] as string).indexOf(inputValue) > -1;
|
2016-09-01 11:08:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.findIndex(callback) - b.findIndex(callback);
|
|
|
|
}
|
|
|
|
|
2018-11-06 12:36:03 +08:00
|
|
|
function getFieldNames(props: CascaderProps) {
|
|
|
|
const { fieldNames, filedNames } = props;
|
|
|
|
if ('filedNames' in props) {
|
|
|
|
return filedNames; // For old compatibility
|
|
|
|
}
|
|
|
|
return fieldNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilledFieldNames(props: CascaderProps) {
|
|
|
|
const fieldNames = getFieldNames(props) || {};
|
2018-07-03 19:54:33 +08:00
|
|
|
const names: FilledFieldNamesType = {
|
|
|
|
children: fieldNames.children || 'children',
|
|
|
|
label: fieldNames.label || 'label',
|
|
|
|
value: fieldNames.value || 'value',
|
2018-05-04 17:27:34 +08:00
|
|
|
};
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
const defaultDisplayRender = (label: string[]) => label.join(' / ');
|
2016-10-24 12:04:26 +08:00
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
export default class Cascader extends React.Component<CascaderProps, CascaderState> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-cascader',
|
2016-09-14 16:18:33 +08:00
|
|
|
inputPrefixCls: 'ant-input',
|
2016-04-01 13:51:26 +08:00
|
|
|
placeholder: 'Please select',
|
2016-03-29 14:01:10 +08:00
|
|
|
transitionName: 'slide-up',
|
|
|
|
popupPlacement: 'bottomLeft',
|
|
|
|
options: [],
|
|
|
|
disabled: false,
|
|
|
|
allowClear: true,
|
2016-09-01 11:08:41 +08:00
|
|
|
notFoundContent: 'Not Found',
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2016-09-01 11:08:41 +08:00
|
|
|
cachedOptions: CascaderOptionType[];
|
2017-09-08 09:43:21 +08:00
|
|
|
|
2017-09-17 15:48:44 +08:00
|
|
|
private input: Input;
|
2016-09-01 11:08:41 +08:00
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
constructor(props: CascaderProps) {
|
2015-12-29 11:46:13 +08:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2016-10-18 18:19:13 +08:00
|
|
|
value: props.value || props.defaultValue || [],
|
2016-09-01 11:08:41 +08:00
|
|
|
inputValue: '',
|
|
|
|
inputFocused: false,
|
2017-11-03 13:03:54 +08:00
|
|
|
popupVisible: props.popupVisible,
|
2018-05-21 20:35:14 +08:00
|
|
|
flattenOptions:
|
2018-11-06 12:36:03 +08:00
|
|
|
props.showSearch ? this.flattenTree(props.options, props) : undefined,
|
2015-12-29 11:46:13 +08:00
|
|
|
};
|
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
componentWillReceiveProps(nextProps: CascaderProps) {
|
2015-12-29 21:18:27 +08:00
|
|
|
if ('value' in nextProps) {
|
2016-01-06 11:45:47 +08:00
|
|
|
this.setState({ value: nextProps.value || [] });
|
2015-12-29 21:18:27 +08:00
|
|
|
}
|
2017-11-03 13:03:54 +08:00
|
|
|
if ('popupVisible' in nextProps) {
|
|
|
|
this.setState({ popupVisible: nextProps.popupVisible });
|
|
|
|
}
|
2016-09-01 11:08:41 +08:00
|
|
|
if (nextProps.showSearch && this.props.options !== nextProps.options) {
|
2018-05-04 17:27:34 +08:00
|
|
|
this.setState({
|
2018-11-06 12:36:03 +08:00
|
|
|
flattenOptions: this.flattenTree(nextProps.options, nextProps),
|
2018-05-04 17:27:34 +08:00
|
|
|
});
|
2016-09-01 11:08:41 +08:00
|
|
|
}
|
2015-12-29 21:18:27 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2018-05-21 20:35:14 +08:00
|
|
|
handleChange = (value: any, selectedOptions: CascaderOptionType[]) => {
|
2016-09-01 11:08:41 +08:00
|
|
|
this.setState({ inputValue: '' });
|
2016-12-07 10:43:06 +08:00
|
|
|
if (selectedOptions[0].__IS_FILTERED_OPTION) {
|
|
|
|
const unwrappedValue = value[0];
|
|
|
|
const unwrappedSelectedOptions = selectedOptions[0].path;
|
|
|
|
this.setValue(unwrappedValue, unwrappedSelectedOptions);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setValue(value, selectedOptions);
|
2015-12-29 21:18:27 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
handlePopupVisibleChange = (popupVisible: boolean) => {
|
2017-11-03 13:03:54 +08:00
|
|
|
if (!('popupVisible' in this.props)) {
|
|
|
|
this.setState({
|
|
|
|
popupVisible,
|
|
|
|
inputFocused: popupVisible,
|
|
|
|
inputValue: popupVisible ? this.state.inputValue : '',
|
|
|
|
});
|
|
|
|
}
|
2016-10-24 12:04:26 +08:00
|
|
|
|
|
|
|
const onPopupVisibleChange = this.props.onPopupVisibleChange;
|
|
|
|
if (onPopupVisibleChange) {
|
|
|
|
onPopupVisibleChange(popupVisible);
|
|
|
|
}
|
2015-12-29 21:18:27 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2016-09-01 11:08:41 +08:00
|
|
|
handleInputBlur = () => {
|
|
|
|
this.setState({
|
|
|
|
inputFocused: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
handleInputClick = (e: React.MouseEvent<HTMLInputElement>) => {
|
2016-09-01 11:08:41 +08:00
|
|
|
const { inputFocused, popupVisible } = this.state;
|
|
|
|
// Prevent `Trigger` behaviour.
|
|
|
|
if (inputFocused || popupVisible) {
|
|
|
|
e.stopPropagation();
|
2018-05-25 19:31:34 +08:00
|
|
|
if (e.nativeEvent.stopImmediatePropagation) {
|
|
|
|
e.nativeEvent.stopImmediatePropagation();
|
|
|
|
}
|
2016-09-01 11:08:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
2017-03-28 13:51:58 +08:00
|
|
|
if (e.keyCode === KeyCode.BACKSPACE) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
2016-09-01 11:08:41 +08:00
|
|
|
const inputValue = e.target.value;
|
|
|
|
this.setState({ inputValue });
|
|
|
|
}
|
|
|
|
|
2018-05-21 20:35:14 +08:00
|
|
|
setValue = (value: string[], selectedOptions: CascaderOptionType[] = []) => {
|
2015-12-29 21:18:27 +08:00
|
|
|
if (!('value' in this.props)) {
|
|
|
|
this.setState({ value });
|
|
|
|
}
|
2016-10-24 12:04:26 +08:00
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
|
|
|
onChange(value, selectedOptions);
|
|
|
|
}
|
2015-12-29 11:46:13 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2015-12-29 11:46:13 +08:00
|
|
|
getLabel() {
|
2018-11-06 12:36:03 +08:00
|
|
|
const { options, displayRender = defaultDisplayRender as Function } = this.props;
|
|
|
|
const names = getFilledFieldNames(this.props);
|
2016-09-01 11:08:41 +08:00
|
|
|
const value = this.state.value;
|
|
|
|
const unwrappedValue = Array.isArray(value[0]) ? value[0] : value;
|
2017-11-21 21:52:40 +08:00
|
|
|
const selectedOptions: CascaderOptionType[] = arrayTreeFilter(options,
|
2018-05-04 17:27:34 +08:00
|
|
|
(o: CascaderOptionType, level: number) => o[names.value] === unwrappedValue[level],
|
2018-07-28 13:29:39 +08:00
|
|
|
{ childrenKeyName: names.children },
|
2017-11-21 21:52:40 +08:00
|
|
|
);
|
2018-05-04 17:27:34 +08:00
|
|
|
const label = selectedOptions.map(o => o[names.label]);
|
2016-05-18 15:34:30 +08:00
|
|
|
return displayRender(label, selectedOptions);
|
2015-12-29 11:46:13 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
clearSelection = (e: React.MouseEvent<HTMLElement>) => {
|
2015-12-29 21:18:27 +08:00
|
|
|
e.preventDefault();
|
2015-12-29 22:34:23 +08:00
|
|
|
e.stopPropagation();
|
2016-09-01 11:08:41 +08:00
|
|
|
if (!this.state.inputValue) {
|
|
|
|
this.setValue([]);
|
2017-11-03 13:03:54 +08:00
|
|
|
this.handlePopupVisibleChange(false);
|
2016-09-01 11:08:41 +08:00
|
|
|
} else {
|
|
|
|
this.setState({ inputValue: '' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:27:34 +08:00
|
|
|
flattenTree(
|
|
|
|
options: CascaderOptionType[],
|
2018-11-06 12:36:03 +08:00
|
|
|
props: CascaderProps,
|
2018-05-04 17:27:34 +08:00
|
|
|
ancestor: CascaderOptionType[] = [],
|
|
|
|
) {
|
2018-11-06 12:36:03 +08:00
|
|
|
const names: FilledFieldNamesType = getFilledFieldNames(props);
|
2018-05-21 20:35:14 +08:00
|
|
|
let flattenOptions = [] as CascaderOptionType[][];
|
|
|
|
let childrenName = names.children;
|
2016-09-01 11:08:41 +08:00
|
|
|
options.forEach((option) => {
|
|
|
|
const path = ancestor.concat(option);
|
2018-11-06 12:36:03 +08:00
|
|
|
if (props.changeOnSelect || !option[childrenName] || !option[childrenName].length) {
|
2016-09-01 11:08:41 +08:00
|
|
|
flattenOptions.push(path);
|
|
|
|
}
|
2018-05-04 17:27:34 +08:00
|
|
|
if (option[childrenName]) {
|
|
|
|
flattenOptions = flattenOptions.concat(
|
|
|
|
this.flattenTree(
|
|
|
|
option[childrenName],
|
2018-11-06 12:36:03 +08:00
|
|
|
props,
|
2018-05-04 17:27:34 +08:00
|
|
|
path,
|
|
|
|
),
|
|
|
|
);
|
2016-09-01 11:08:41 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return flattenOptions;
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
generateFilteredOptions(prefixCls: string | undefined) {
|
2018-11-06 12:36:03 +08:00
|
|
|
const { showSearch, notFoundContent } = this.props;
|
|
|
|
const names: FilledFieldNamesType = getFilledFieldNames(this.props);
|
2016-09-01 11:08:41 +08:00
|
|
|
const {
|
|
|
|
filter = defaultFilterOption,
|
|
|
|
render = defaultRenderFilteredOption,
|
|
|
|
sort = defaultSortFilteredOption,
|
|
|
|
} = showSearch as ShowSearchType;
|
2018-05-21 20:35:14 +08:00
|
|
|
const { flattenOptions = [], inputValue } = this.state;
|
2018-05-04 17:27:34 +08:00
|
|
|
const filtered = flattenOptions.filter((path) => filter(this.state.inputValue, path, names))
|
|
|
|
.sort((a, b) => sort(a, b, inputValue, names));
|
2016-09-01 11:08:41 +08:00
|
|
|
|
|
|
|
if (filtered.length > 0) {
|
2018-05-21 20:35:14 +08:00
|
|
|
return filtered.map((path: CascaderOptionType[]) => {
|
2016-09-01 11:08:41 +08:00
|
|
|
return {
|
2016-12-07 10:43:06 +08:00
|
|
|
__IS_FILTERED_OPTION: true,
|
|
|
|
path,
|
2018-05-04 17:27:34 +08:00
|
|
|
[names.label]: render(inputValue, path, prefixCls, names),
|
|
|
|
[names.value]: path.map((o: CascaderOptionType) => o[names.value]),
|
2018-05-21 20:35:14 +08:00
|
|
|
disabled: path.some((o: CascaderOptionType) => !!o.disabled),
|
2017-11-21 21:52:40 +08:00
|
|
|
} as CascaderOptionType;
|
2016-09-01 11:08:41 +08:00
|
|
|
});
|
|
|
|
}
|
2018-05-04 17:27:34 +08:00
|
|
|
return [{ [names.label]: notFoundContent, [names.value]: 'ANT_CASCADER_NOT_FOUND', disabled: true }];
|
2015-12-29 21:18:27 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
focus() {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.input.blur();
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:52:40 +08:00
|
|
|
saveInput = (node: Input) => {
|
2017-09-08 09:43:21 +08:00
|
|
|
this.input = node;
|
|
|
|
}
|
|
|
|
|
2015-12-29 11:46:13 +08:00
|
|
|
render() {
|
2016-12-07 10:43:06 +08:00
|
|
|
const { props, state } = this;
|
2016-12-19 15:19:15 +08:00
|
|
|
const {
|
|
|
|
prefixCls, inputPrefixCls, children, placeholder, size, disabled,
|
2018-09-16 19:36:19 +08:00
|
|
|
className, style, allowClear, showSearch = false, suffixIcon, ...otherProps
|
2016-12-19 15:19:15 +08:00
|
|
|
} = props;
|
2018-07-03 15:46:12 +08:00
|
|
|
const { value, inputFocused } = state;
|
2016-07-07 15:13:01 +08:00
|
|
|
|
2015-12-29 18:31:48 +08:00
|
|
|
const sizeCls = classNames({
|
2016-09-14 16:59:45 +08:00
|
|
|
[`${inputPrefixCls}-lg`]: size === 'large',
|
|
|
|
[`${inputPrefixCls}-sm`]: size === 'small',
|
2015-12-29 18:31:48 +08:00
|
|
|
});
|
2017-03-23 21:15:49 +08:00
|
|
|
const clearIcon = (allowClear && !disabled && value.length > 0) || state.inputValue ? (
|
2016-11-23 17:53:10 +08:00
|
|
|
<Icon
|
2018-09-11 20:45:40 +08:00
|
|
|
type="close-circle"
|
|
|
|
theme="filled"
|
2015-12-29 21:18:27 +08:00
|
|
|
className={`${prefixCls}-picker-clear`}
|
2016-06-06 13:54:10 +08:00
|
|
|
onClick={this.clearSelection}
|
2017-03-23 21:15:49 +08:00
|
|
|
/>
|
|
|
|
) : null;
|
2015-12-29 22:34:23 +08:00
|
|
|
const arrowCls = classNames({
|
|
|
|
[`${prefixCls}-picker-arrow`]: true,
|
2016-09-01 11:08:41 +08:00
|
|
|
[`${prefixCls}-picker-arrow-expand`]: state.popupVisible,
|
2015-12-29 22:34:23 +08:00
|
|
|
});
|
2017-09-27 22:32:49 +08:00
|
|
|
const pickerCls = classNames(
|
|
|
|
className, `${prefixCls}-picker`, {
|
2018-08-13 11:44:25 +08:00
|
|
|
[`${prefixCls}-picker-with-value`]: state.inputValue,
|
|
|
|
[`${prefixCls}-picker-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-picker-${size}`]: !!size,
|
|
|
|
[`${prefixCls}-picker-show-search`]: !!showSearch,
|
|
|
|
[`${prefixCls}-picker-focused`]: inputFocused,
|
|
|
|
});
|
2016-03-03 14:57:26 +08:00
|
|
|
|
|
|
|
// Fix bug of https://github.com/facebook/react/pull/5004
|
2016-07-06 12:21:49 +08:00
|
|
|
// and https://fb.me/react-unknown-prop
|
|
|
|
const inputProps = omit(otherProps, [
|
|
|
|
'onChange',
|
|
|
|
'options',
|
|
|
|
'popupPlacement',
|
|
|
|
'transitionName',
|
|
|
|
'displayRender',
|
|
|
|
'onPopupVisibleChange',
|
|
|
|
'changeOnSelect',
|
|
|
|
'expandTrigger',
|
2016-07-16 15:50:27 +08:00
|
|
|
'popupVisible',
|
|
|
|
'getPopupContainer',
|
|
|
|
'loadData',
|
|
|
|
'popupClassName',
|
2016-09-01 11:08:41 +08:00
|
|
|
'filterOption',
|
|
|
|
'renderFilteredOption',
|
|
|
|
'sortFilteredOption',
|
|
|
|
'notFoundContent',
|
2018-07-03 19:54:33 +08:00
|
|
|
'fieldNames',
|
2018-11-06 12:36:03 +08:00
|
|
|
'filedNames', // For old compatibility
|
2016-07-06 12:21:49 +08:00
|
|
|
]);
|
2016-03-03 14:57:26 +08:00
|
|
|
|
2016-09-01 11:08:41 +08:00
|
|
|
let options = props.options;
|
|
|
|
if (state.inputValue) {
|
2016-09-14 16:18:33 +08:00
|
|
|
options = this.generateFilteredOptions(prefixCls);
|
2016-09-01 11:08:41 +08:00
|
|
|
}
|
|
|
|
// Dropdown menu should keep previous status until it is fully closed.
|
|
|
|
if (!state.popupVisible) {
|
|
|
|
options = this.cachedOptions;
|
|
|
|
} else {
|
|
|
|
this.cachedOptions = options;
|
|
|
|
}
|
|
|
|
|
2016-10-24 12:04:26 +08:00
|
|
|
const dropdownMenuColumnStyle: { width?: number, height?: string } = {};
|
2016-09-01 11:08:41 +08:00
|
|
|
const isNotFound = (options || []).length === 1 && options[0].value === 'ANT_CASCADER_NOT_FOUND';
|
|
|
|
if (isNotFound) {
|
|
|
|
dropdownMenuColumnStyle.height = 'auto'; // Height of one row.
|
|
|
|
}
|
|
|
|
// The default value of `matchInputWidth` is `true`
|
2016-12-19 15:19:15 +08:00
|
|
|
const resultListMatchInputWidth = (showSearch as ShowSearchType).matchInputWidth === false ? false : true;
|
2017-09-08 09:43:21 +08:00
|
|
|
if (resultListMatchInputWidth && state.inputValue && this.input) {
|
|
|
|
dropdownMenuColumnStyle.width = this.input.input.offsetWidth;
|
2016-09-01 11:08:41 +08:00
|
|
|
}
|
2016-11-25 12:03:39 +08:00
|
|
|
|
2018-09-16 19:36:19 +08:00
|
|
|
const inputIcon = suffixIcon && (
|
|
|
|
React.isValidElement<{ className?: string }>(suffixIcon)
|
2018-09-14 22:22:43 +08:00
|
|
|
? React.cloneElement(
|
2018-09-16 19:36:19 +08:00
|
|
|
suffixIcon,
|
2018-09-14 22:22:43 +08:00
|
|
|
{
|
|
|
|
className: classNames({
|
2018-09-16 19:36:19 +08:00
|
|
|
[suffixIcon.props.className!]: suffixIcon.props.className,
|
2018-09-14 22:22:43 +08:00
|
|
|
[`${prefixCls}-picker-arrow`]: true,
|
|
|
|
}),
|
|
|
|
},
|
2018-09-16 19:36:19 +08:00
|
|
|
) : <span className={`${prefixCls}-picker-arrow`}>{suffixIcon}</span>) || (
|
2018-09-14 22:22:43 +08:00
|
|
|
<Icon type="down" className={arrowCls} />
|
|
|
|
);
|
|
|
|
|
2016-11-25 12:03:39 +08:00
|
|
|
const input = children || (
|
|
|
|
<span
|
|
|
|
style={style}
|
|
|
|
className={pickerCls}
|
|
|
|
>
|
2017-05-22 10:09:17 +08:00
|
|
|
<span className={`${prefixCls}-picker-label`}>
|
|
|
|
{this.getLabel()}
|
|
|
|
</span>
|
2016-11-25 12:03:39 +08:00
|
|
|
<Input
|
|
|
|
{...inputProps}
|
2017-09-08 09:43:21 +08:00
|
|
|
ref={this.saveInput}
|
2017-10-20 10:55:06 +08:00
|
|
|
prefixCls={inputPrefixCls}
|
2016-12-19 15:19:15 +08:00
|
|
|
placeholder={value && value.length > 0 ? undefined : placeholder}
|
2016-11-25 12:03:39 +08:00
|
|
|
className={`${prefixCls}-input ${sizeCls}`}
|
|
|
|
value={state.inputValue}
|
|
|
|
disabled={disabled}
|
|
|
|
readOnly={!showSearch}
|
|
|
|
autoComplete="off"
|
|
|
|
onClick={showSearch ? this.handleInputClick : undefined}
|
|
|
|
onBlur={showSearch ? this.handleInputBlur : undefined}
|
2017-03-28 13:51:58 +08:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2016-11-25 12:03:39 +08:00
|
|
|
onChange={showSearch ? this.handleInputChange : undefined}
|
|
|
|
/>
|
|
|
|
{clearIcon}
|
2018-09-14 22:22:43 +08:00
|
|
|
{inputIcon}
|
2016-11-25 12:03:39 +08:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
2018-08-13 11:44:25 +08:00
|
|
|
const expandIcon = (
|
2018-08-23 22:15:25 +08:00
|
|
|
<Icon type="right" />
|
|
|
|
);
|
|
|
|
|
|
|
|
const loadingIcon = (
|
|
|
|
<span className={`${prefixCls}-menu-item-loading-icon`}>
|
|
|
|
<Icon type="redo" spin />
|
|
|
|
</span>
|
2018-08-13 11:44:25 +08:00
|
|
|
);
|
|
|
|
|
2018-09-14 22:22:43 +08:00
|
|
|
const rest = omit(props, ['inputIcon', 'expandIcon', 'loadingIcon']);
|
|
|
|
|
2015-12-29 11:46:13 +08:00
|
|
|
return (
|
2016-10-18 18:10:41 +08:00
|
|
|
<RcCascader
|
2018-09-14 22:22:43 +08:00
|
|
|
{...rest}
|
2016-09-01 11:08:41 +08:00
|
|
|
options={options}
|
|
|
|
value={value}
|
|
|
|
popupVisible={state.popupVisible}
|
2015-12-29 21:18:27 +08:00
|
|
|
onPopupVisibleChange={this.handlePopupVisibleChange}
|
2016-06-06 13:54:10 +08:00
|
|
|
onChange={this.handleChange}
|
2016-09-01 11:08:41 +08:00
|
|
|
dropdownMenuColumnStyle={dropdownMenuColumnStyle}
|
2018-08-13 11:44:25 +08:00
|
|
|
expandIcon={expandIcon}
|
2018-08-23 22:15:25 +08:00
|
|
|
loadingIcon={loadingIcon}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-11-25 12:03:39 +08:00
|
|
|
{input}
|
2016-03-21 21:16:38 +08:00
|
|
|
</RcCascader>
|
2015-12-29 11:46:13 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|