forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.tsx
169 lines (161 loc) · 5.08 KB
/
Search.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { PropType } from 'vue';
import { computed, shallowRef, defineComponent } from 'vue';
import classNames from '../_util/classNames';
import Input from './Input';
import SearchOutlined from '@ant-design/icons-vue/SearchOutlined';
import Button from '../button';
import { cloneElement } from '../_util/vnode';
import PropTypes from '../_util/vue-types';
import isPlainObject from 'lodash-es/isPlainObject';
import type {
ChangeEvent,
CompositionEventHandler,
MouseEventHandler,
} from '../_util/EventInterface';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import omit from '../_util/omit';
import inputProps from './inputProps';
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'AInputSearch',
inheritAttrs: false,
props: {
...inputProps(),
inputPrefixCls: String,
// 不能设置默认值 https://github.com/vueComponent/ant-design-vue/issues/1916
enterButton: PropTypes.any,
onSearch: {
type: Function as PropType<
(value: string, event?: ChangeEvent | MouseEvent | KeyboardEvent) => void
>,
},
},
setup(props, { slots, attrs, expose, emit }) {
const inputRef = shallowRef();
const composedRef = shallowRef(false);
const focus = () => {
inputRef.value?.focus();
};
const blur = () => {
inputRef.value?.blur();
};
expose({
focus,
blur,
});
const onChange = (e: ChangeEvent) => {
emit('update:value', (e.target as HTMLInputElement).value);
if (e && e.target && e.type === 'click') {
emit('search', e.target.value, e);
}
emit('change', e);
};
const onMousedown: MouseEventHandler = e => {
if (document.activeElement === inputRef.value?.input) {
e.preventDefault();
}
};
const onSearch = (e: MouseEvent | KeyboardEvent) => {
emit('search', inputRef.value?.input?.stateValue, e);
};
const onPressEnter = (e: KeyboardEvent) => {
if (composedRef.value || props.loading) {
return;
}
onSearch(e);
};
const handleOnCompositionStart: CompositionEventHandler = e => {
composedRef.value = true;
emit('compositionstart', e);
};
const handleOnCompositionEnd: CompositionEventHandler = e => {
composedRef.value = false;
emit('compositionend', e);
};
const { prefixCls, getPrefixCls, direction, size } = useConfigInject('input-search', props);
const inputPrefixCls = computed(() => getPrefixCls('input', props.inputPrefixCls));
return () => {
const {
disabled,
loading,
addonAfter = slots.addonAfter?.(),
suffix = slots.suffix?.(),
...restProps
} = props;
let { enterButton = slots.enterButton?.() ?? false } = props;
enterButton = enterButton || enterButton === '';
const searchIcon = typeof enterButton === 'boolean' ? <SearchOutlined /> : null;
const btnClassName = `${prefixCls.value}-button`;
const enterButtonAsElement = Array.isArray(enterButton) ? enterButton[0] : enterButton;
let button: any;
const isAntdButton =
enterButtonAsElement.type &&
isPlainObject(enterButtonAsElement.type) &&
enterButtonAsElement.type.__ANT_BUTTON;
if (isAntdButton || enterButtonAsElement.tagName === 'button') {
button = cloneElement(
enterButtonAsElement,
{
onMousedown,
onClick: onSearch,
key: 'enterButton',
...(isAntdButton
? {
class: btnClassName,
size: size.value,
}
: {}),
},
false,
);
} else {
const iconOnly = searchIcon && !enterButton;
button = (
<Button
class={btnClassName}
type={enterButton ? 'primary' : undefined}
size={size.value}
disabled={disabled}
key="enterButton"
onMousedown={onMousedown}
onClick={onSearch}
loading={loading}
icon={iconOnly ? searchIcon : null}
>
{iconOnly ? null : searchIcon || enterButton}
</Button>
);
}
if (addonAfter) {
button = [button, addonAfter];
}
const cls = classNames(
prefixCls.value,
{
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
[`${prefixCls.value}-${size.value}`]: !!size.value,
[`${prefixCls.value}-with-button`]: !!enterButton,
},
attrs.class,
);
return (
<Input
ref={inputRef}
{...omit(restProps, ['onUpdate:value', 'onSearch', 'enterButton'])}
{...attrs}
onPressEnter={onPressEnter}
onCompositionstart={handleOnCompositionStart}
onCompositionend={handleOnCompositionEnd}
size={size.value}
prefixCls={inputPrefixCls.value}
addonAfter={button}
suffix={suffix}
onChange={onChange}
class={cls}
disabled={disabled}
v-slots={slots}
/>
);
};
},
});