ant-design-vue/components/transfer/search.tsx

66 lines
1.7 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../_util/vue-types';
2020-10-20 08:45:49 +00:00
import initDefaultProps from '../_util/props-util/initDefaultProps';
2020-03-28 07:52:26 +00:00
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
import SearchOutlined from '@ant-design/icons-vue/SearchOutlined';
2019-01-12 03:33:27 +00:00
import Input from '../input';
import type { ExtractPropTypes } from 'vue';
2020-10-20 08:45:49 +00:00
import { defineComponent } from 'vue';
2018-04-06 16:20:45 +00:00
export const transferSearchProps = {
2018-04-06 16:20:45 +00:00
prefixCls: PropTypes.string,
placeholder: PropTypes.string,
value: PropTypes.string,
2018-05-18 13:21:31 +00:00
handleClear: PropTypes.func,
disabled: PropTypes.looseBool,
2020-08-05 09:17:07 +00:00
onChange: PropTypes.func,
2019-01-12 03:33:27 +00:00
};
2018-04-06 16:20:45 +00:00
export type TransferSearchProps = Partial<ExtractPropTypes<typeof transferSearchProps>>;
2020-10-20 08:45:49 +00:00
export default defineComponent({
2018-04-06 16:20:45 +00:00
name: 'Search',
2020-07-19 14:40:35 +00:00
inheritAttrs: false,
props: initDefaultProps(transferSearchProps, {
2018-04-06 16:20:45 +00:00
placeholder: '',
}),
emits: ['change'],
setup(props, { emit }) {
const handleChange = (e: Event) => {
emit('change', e);
};
const handleClearFn = (e: Event) => {
const { handleClear, disabled } = props;
if (!disabled && handleClear) {
2019-01-12 03:33:27 +00:00
handleClear(e);
2018-05-18 13:21:31 +00:00
}
};
2018-04-06 16:20:45 +00:00
return () => {
const { placeholder, value, prefixCls, disabled } = props;
const icon =
value && value.length > 0 ? (
<a class={`${prefixCls}-action`} onClick={handleClearFn}>
<CloseCircleFilled />
</a>
) : (
<span class={`${prefixCls}-action`}>
<SearchOutlined />
</span>
);
return (
<>
<Input
placeholder={placeholder}
class={prefixCls}
value={value}
onChange={handleChange}
disabled={disabled}
/>
{icon}
</>
);
};
2018-04-06 16:20:45 +00:00
},
2020-10-20 08:45:49 +00:00
});