2020-06-27 06:02:30 +00:00
|
|
|
|
import { inject } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import classNames from 'classnames';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
import { isMobile } from 'is-mobile';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import Input from './Input';
|
2020-03-26 03:53:51 +00:00
|
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
|
|
|
|
import SearchOutlined from '@ant-design/icons-vue/SearchOutlined';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import inputProps from './inputProps';
|
|
|
|
|
import Button from '../button';
|
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-06-27 06:02:30 +00:00
|
|
|
|
import { getOptionProps, getComponent } from '../_util/props-util';
|
2019-04-07 09:19:18 +00:00
|
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2020-06-27 06:02:30 +00:00
|
|
|
|
import isPlainObject from 'lodash/isPlainObject';
|
2017-12-06 10:54:20 +00:00
|
|
|
|
|
|
|
|
|
export default {
|
2018-04-08 13:17:20 +00:00
|
|
|
|
name: 'AInputSearch',
|
2019-05-25 07:10:28 +00:00
|
|
|
|
inheritAttrs: false,
|
2017-12-06 10:54:20 +00:00
|
|
|
|
props: {
|
2018-03-19 13:23:38 +00:00
|
|
|
|
...inputProps,
|
2020-03-21 09:29:53 +00:00
|
|
|
|
// 不能设置默认值 https://github.com/vueComponent/ant-design-vue/issues/1916
|
|
|
|
|
enterButton: PropTypes.any,
|
2017-12-06 10:54:20 +00:00
|
|
|
|
},
|
2020-06-27 06:02:30 +00:00
|
|
|
|
setup() {
|
|
|
|
|
return {
|
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
|
};
|
2019-04-07 09:19:18 +00:00
|
|
|
|
},
|
2017-12-06 10:54:20 +00:00
|
|
|
|
methods: {
|
2020-06-27 06:02:30 +00:00
|
|
|
|
saveInput(node) {
|
|
|
|
|
this.input = node;
|
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
onChange(e) {
|
|
|
|
|
if (e && e.target && e.type === 'click') {
|
|
|
|
|
this.$emit('search', e.target.value, e);
|
|
|
|
|
}
|
|
|
|
|
this.$emit('change', e);
|
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
onSearch(e) {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
if (this.loading || this.disabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-06-27 06:02:30 +00:00
|
|
|
|
this.$emit('search', this.input.stateValue, e);
|
2020-03-07 11:45:13 +00:00
|
|
|
|
if (!isMobile({ tablet: true })) {
|
2020-06-27 06:02:30 +00:00
|
|
|
|
this.input.focus();
|
2020-03-07 11:45:13 +00:00
|
|
|
|
}
|
2017-12-06 10:54:20 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
focus() {
|
2020-06-27 06:02:30 +00:00
|
|
|
|
this.input.focus();
|
2018-06-10 01:29:18 +00:00
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
blur() {
|
2020-06-27 06:02:30 +00:00
|
|
|
|
this.input.blur();
|
2018-06-10 01:29:18 +00:00
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
|
renderLoading(prefixCls) {
|
|
|
|
|
const { size } = this.$props;
|
2020-06-27 06:02:30 +00:00
|
|
|
|
let enterButton = getComponent(this, 'enterButton');
|
2020-03-07 11:45:13 +00:00
|
|
|
|
// 兼容 <a-input-search enterButton />, 因enterButton类型为 any,此类写法 enterButton 为空字符串
|
|
|
|
|
enterButton = enterButton || enterButton === '';
|
|
|
|
|
if (enterButton) {
|
|
|
|
|
return (
|
|
|
|
|
<Button class={`${prefixCls}-button`} type="primary" size={size} key="enterButton">
|
2020-03-26 03:53:51 +00:00
|
|
|
|
<LoadingOutlined />
|
2020-03-07 11:45:13 +00:00
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-03-26 03:53:51 +00:00
|
|
|
|
return <LoadingOutlined class={`${prefixCls}-icon`} key="loadingIcon" />;
|
2020-03-07 11:45:13 +00:00
|
|
|
|
},
|
2019-04-07 09:19:18 +00:00
|
|
|
|
renderSuffix(prefixCls) {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const { loading } = this;
|
2020-06-27 06:02:30 +00:00
|
|
|
|
const suffix = getComponent(this, 'suffix');
|
|
|
|
|
let enterButton = getComponent(this, 'enterButton');
|
2020-03-07 11:45:13 +00:00
|
|
|
|
// 兼容 <a-input-search enterButton />, 因enterButton类型为 any,此类写法 enterButton 为空字符串
|
|
|
|
|
enterButton = enterButton || enterButton === '';
|
|
|
|
|
if (loading && !enterButton) {
|
|
|
|
|
return [suffix, this.renderLoading(prefixCls)];
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 09:19:18 +00:00
|
|
|
|
if (enterButton) return suffix;
|
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const icon = (
|
2020-03-26 03:53:51 +00:00
|
|
|
|
<SearchOutlined class={`${prefixCls}-icon`} key="searchIcon" onClick={this.onSearch} />
|
2019-04-07 09:19:18 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (suffix) {
|
2019-09-11 10:11:48 +00:00
|
|
|
|
// let cloneSuffix = suffix;
|
|
|
|
|
// if (isValidElement(cloneSuffix) && !cloneSuffix.key) {
|
|
|
|
|
// cloneSuffix = cloneElement(cloneSuffix, {
|
|
|
|
|
// key: 'originSuffix',
|
|
|
|
|
// });
|
|
|
|
|
// }
|
2020-03-07 11:45:13 +00:00
|
|
|
|
return [suffix, icon];
|
2019-04-07 09:19:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
|
return icon;
|
2019-04-07 09:19:18 +00:00
|
|
|
|
},
|
|
|
|
|
renderAddonAfter(prefixCls) {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const { size, disabled, loading } = this;
|
|
|
|
|
const btnClassName = `${prefixCls}-button`;
|
2020-06-27 06:02:30 +00:00
|
|
|
|
let enterButton = getComponent(this, 'enterButton');
|
2020-03-07 11:45:13 +00:00
|
|
|
|
enterButton = enterButton || enterButton === '';
|
2020-06-27 06:02:30 +00:00
|
|
|
|
const addonAfter = getComponent(this, 'addonAfter');
|
2020-03-07 11:45:13 +00:00
|
|
|
|
if (loading && enterButton) {
|
|
|
|
|
return [this.renderLoading(prefixCls), addonAfter];
|
|
|
|
|
}
|
2019-05-28 03:37:38 +00:00
|
|
|
|
if (!enterButton) return addonAfter;
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const enterButtonAsElement = Array.isArray(enterButton) ? enterButton[0] : enterButton;
|
2019-04-07 09:19:18 +00:00
|
|
|
|
let button;
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const isAntdButton =
|
2020-06-27 06:02:30 +00:00
|
|
|
|
enterButtonAsElement.type &&
|
|
|
|
|
isPlainObject(enterButtonAsElement.type) &&
|
|
|
|
|
enterButtonAsElement.type.__ANT_BUTTON;
|
|
|
|
|
if (enterButtonAsElement.tagName === 'button' || isAntdButton) {
|
2019-04-07 09:19:18 +00:00
|
|
|
|
button = cloneElement(enterButtonAsElement, {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
key: 'enterButton',
|
|
|
|
|
class: isAntdButton ? btnClassName : '',
|
2020-06-27 06:02:30 +00:00
|
|
|
|
...(isAntdButton ? { size } : {}),
|
|
|
|
|
onClick: this.onSearch,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
} else {
|
2019-04-07 09:19:18 +00:00
|
|
|
|
button = (
|
2018-09-05 13:28:54 +00:00
|
|
|
|
<Button
|
2019-04-07 09:19:18 +00:00
|
|
|
|
class={btnClassName}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
type="primary"
|
2018-09-05 13:28:54 +00:00
|
|
|
|
size={size}
|
|
|
|
|
disabled={disabled}
|
2019-01-12 03:33:27 +00:00
|
|
|
|
key="enterButton"
|
2019-04-07 09:19:18 +00:00
|
|
|
|
onClick={this.onSearch}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
>
|
2020-03-26 03:53:51 +00:00
|
|
|
|
{enterButton === true || enterButton === '' ? <SearchOutlined /> : enterButton}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
</Button>
|
2019-01-12 03:33:27 +00:00
|
|
|
|
);
|
2018-04-07 06:29:59 +00:00
|
|
|
|
}
|
2019-04-07 09:19:18 +00:00
|
|
|
|
if (addonAfter) {
|
|
|
|
|
return [button, addonAfter];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return button;
|
2018-04-07 06:29:59 +00:00
|
|
|
|
},
|
2017-12-06 10:54:20 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
render() {
|
2019-04-07 09:19:18 +00:00
|
|
|
|
const {
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
inputPrefixCls: customizeInputPrefixCls,
|
|
|
|
|
size,
|
2020-06-27 06:02:30 +00:00
|
|
|
|
class: className,
|
|
|
|
|
...restProps
|
|
|
|
|
} = { ...getOptionProps(this), ...this.$attrs };
|
|
|
|
|
delete restProps.onSearch;
|
|
|
|
|
delete restProps.loading;
|
|
|
|
|
delete restProps.enterButton;
|
|
|
|
|
delete restProps.addonBefore;
|
2019-09-11 14:35:25 +00:00
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-07 09:19:18 +00:00
|
|
|
|
const prefixCls = getPrefixCls('input-search', customizePrefixCls);
|
|
|
|
|
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
|
|
|
|
|
|
2020-06-27 06:02:30 +00:00
|
|
|
|
let enterButton = getComponent(this, 'enterButton');
|
|
|
|
|
const addonBefore = getComponent(this, 'addonBefore');
|
2020-03-07 11:45:13 +00:00
|
|
|
|
enterButton = enterButton || enterButton === '';
|
2019-04-07 09:19:18 +00:00
|
|
|
|
let inputClassName;
|
2019-05-28 03:37:38 +00:00
|
|
|
|
if (enterButton) {
|
2020-06-27 06:02:30 +00:00
|
|
|
|
inputClassName = classNames(prefixCls, className, {
|
2019-04-07 09:19:18 +00:00
|
|
|
|
[`${prefixCls}-enter-button`]: !!enterButton,
|
|
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
});
|
2019-04-07 09:19:18 +00:00
|
|
|
|
} else {
|
|
|
|
|
inputClassName = prefixCls;
|
2018-12-13 13:26:21 +00:00
|
|
|
|
}
|
2019-04-07 09:19:18 +00:00
|
|
|
|
|
2017-12-07 04:31:12 +00:00
|
|
|
|
const inputProps = {
|
2020-06-27 06:02:30 +00:00
|
|
|
|
...restProps,
|
|
|
|
|
prefixCls: inputPrefixCls,
|
|
|
|
|
size,
|
|
|
|
|
suffix: this.renderSuffix(prefixCls),
|
|
|
|
|
prefix: getComponent(this, 'prefix'),
|
|
|
|
|
addonAfter: this.renderAddonAfter(prefixCls),
|
|
|
|
|
addonBefore,
|
|
|
|
|
class: inputClassName,
|
|
|
|
|
onPressEnter: this.onSearch,
|
|
|
|
|
onChange: this.onChange,
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|
2020-06-27 06:02:30 +00:00
|
|
|
|
return <Input {...inputProps} ref={this.saveInput} />;
|
2017-12-06 10:54:20 +00:00
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
|
};
|