-
Notifications
You must be signed in to change notification settings - Fork 138
/
ProSearch.tsx
180 lines (162 loc) · 6.2 KB
/
ProSearch.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
170
171
172
173
174
175
176
177
178
179
180
/**
* @license
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/
import { computed, defineComponent, nextTick, normalizeClass, normalizeStyle, provide, ref, toRef, watch } from 'vue'
import { callEmit } from '@idux/cdk/utils'
import { ɵOverflow } from '@idux/components/_private/overflow'
import { useGlobalConfig as useComponentGlobalConfig, useDateConfig } from '@idux/components/config'
import { useZIndex } from '@idux/components/utils'
import { useGlobalConfig } from '@idux/pro/config'
import { useActiveSegment } from './composables/useActiveSegment'
import { useCommonOverlayProps } from './composables/useCommonOverlayProps'
import { useFocusedState } from './composables/useFocusedState'
import { useSearchItems } from './composables/useSearchItem'
import { useSearchItemErrors } from './composables/useSearchItemErrors'
import { useSearchStates } from './composables/useSearchStates'
import { useSearchValues } from './composables/useSearchValues'
import SearchItemComp from './searchItem/SearchItem'
import { proSearchContext } from './token'
import { type SearchItem, proSearchProps } from './types'
import { renderIcon } from './utils/RenderIcon'
export default defineComponent({
name: 'IxProSearch',
props: proSearchProps,
setup(props, { attrs, expose, slots }) {
const common = useGlobalConfig('common')
const componentCommon = useComponentGlobalConfig('common')
const locale = useGlobalConfig('locale')
const config = useGlobalConfig('search')
const dateConfig = useDateConfig()
const mergedPrefixCls = computed(() => `${common.prefixCls}-search`)
const { searchValues, searchValueEmpty, setSearchValues } = useSearchValues(props)
const searchStateContext = useSearchStates(props, dateConfig, searchValues, setSearchValues)
const errors = useSearchItemErrors(props, searchValues)
const searchItems = useSearchItems(
props,
slots,
mergedPrefixCls,
searchStateContext.searchStates,
errors,
dateConfig,
)
const elementRef = ref<HTMLElement | undefined>()
const activeSegmentContext = useActiveSegment(
props,
elementRef,
searchItems,
searchStateContext.tempSearchStateAvailable,
)
const commonOverlayProps = useCommonOverlayProps(props, config, mergedPrefixCls)
const { focused, focus, blur } = useFocusedState(
props,
elementRef,
commonOverlayProps,
searchStateContext,
activeSegmentContext,
)
//TODO: fix ref(true)
const currentZIndex = useZIndex(toRef(props, 'zIndex'), toRef(componentCommon, 'overlayZIndex'), ref(true))
const { initSearchStates, clearSearchState } = searchStateContext
const { activeSegment } = activeSegmentContext
watch(
() => props.value,
() => {
nextTick(initSearchStates)
},
{ immediate: true, deep: true },
)
const placeholder = computed(() => props.placeholder ?? locale.search.placeholder)
const clearable = computed(() => props.clearable ?? config.clearable)
const clearIcon = computed(() => props.clearIcon ?? config.clearIcon)
const searchIcon = computed(() => props.searchIcon ?? config.searchIcon)
const classes = computed(() => {
const prefixCls = mergedPrefixCls.value
return normalizeClass({
[prefixCls]: true,
[`${prefixCls}-focused`]: focused.value,
[`${prefixCls}-disabled`]: !!props.disabled,
})
})
const containerStyle = computed(() =>
normalizeStyle({
zIndex: currentZIndex.value,
}),
)
expose({ focus, blur })
const handleSearchBtnClick = () => {
callEmit(props.onSearch, searchValues.value)
}
const handleClearBtnClick = () => {
clearSearchState()
}
const handleSearchBtnMouseDown = (evt: MouseEvent) => {
evt.preventDefault()
}
const handleClearBtnMouseDown = (evt: MouseEvent) => {
evt.preventDefault()
}
provide(proSearchContext, {
props,
slots,
locale: locale.search,
mergedPrefixCls,
commonOverlayProps,
...searchStateContext,
...activeSegmentContext,
})
return () => {
const prefixCls = mergedPrefixCls.value
const overflowSlots = {
item: (item: SearchItem) => <SearchItemComp key={item.key} tagOnly={true} searchItem={item} />,
rest: (rest: SearchItem[]) => (
<span class={`${prefixCls}-search-item ${prefixCls}-search-item-tag`}>
{slots.overflowedLabel?.(rest) ?? `+ ${rest.length}`}
</span>
),
}
return (
<div ref={elementRef} class={classes.value} tabindex={(attrs.tabIndex as number) ?? 0}>
<div class={`${prefixCls}-input-container`} style={containerStyle.value}>
<div class={`${prefixCls}-input-content`}>
<ɵOverflow
v-show={!focused.value}
v-slots={overflowSlots}
prefixCls={prefixCls}
dataSource={searchItems.value}
getKey={item => item.key}
maxLabel={props.maxLabel}
/>
<div class={`${prefixCls}-search-item-container`} v-show={focused.value}>
{searchItems.value?.map(item => (
<SearchItemComp key={item.key} searchItem={item} />
))}
</div>
{searchValueEmpty.value && !activeSegment.value && (
<span class={`${prefixCls}-placeholder`}>{placeholder.value}</span>
)}
</div>
{!searchValueEmpty.value && clearable.value && !props.disabled && (
<div
class={`${prefixCls}-clear-icon`}
onMousedown={handleClearBtnMouseDown}
onClick={handleClearBtnClick}
>
{renderIcon(clearIcon.value, slots.clearIcon)}
</div>
)}
</div>
<div
class={`${prefixCls}-search-button`}
onMousedown={handleSearchBtnMouseDown}
onClick={handleSearchBtnClick}
>
{renderIcon(searchIcon.value, slots.searchIcon)}
</div>
</div>
)
}
},
})