-
Notifications
You must be signed in to change notification settings - Fork 138
/
QuickSelectItem.tsx
200 lines (177 loc) Β· 6.35 KB
/
QuickSelectItem.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* @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
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { SearchState } from '../../composables/useSearchStates'
import { computed, defineComponent, inject, normalizeClass, ref, watch } from 'vue'
import { isNil, isObject } from 'lodash-es'
import { Logger, callEmit, useState } from '@idux/cdk/utils'
import { type IconInstance, IxIcon } from '@idux/components/icon'
import { IxInput } from '@idux/components/input'
import { proSearchContext } from '../../token'
import { type SearchDataTypes, quickSelectPanelItemProps, searchDataTypes } from '../../types'
export default defineComponent({
props: quickSelectPanelItemProps,
setup(props, { slots }) {
const {
props: proSearchProps,
mergedPrefixCls,
convertStateToValue,
createSearchState,
getSearchStatesByFieldKey,
removeSearchState,
updateSegmentValue,
updateSearchValues,
getCacheData,
setCacheData,
} = inject(proSearchContext)!
const searchInputRef = ref<HTMLInputElement>()
const searchIconRef = ref<IconInstance>()
const [searchInput, setSearchInput] = useState('')
const setSearchInputOpened = (opened: boolean) => {
if (opened) {
searchInputRef.value?.focus()
props.setSearchInputActive?.(true)
} else {
props.setSearchInputActive?.(false)
}
}
const quickSelectSearchable = computed(() => {
if (isObject(props.searchField.quickSelect)) {
return props.searchField.quickSelect.searchable
}
if (props.searchField.quickSelectSearchable) {
Logger.warn('pro/search', '`quickSelectSearchable` is deprecated, use `quickSelect.searchable` instead')
return true
}
return false
})
const searchBarCls = computed(() => {
const prefixCls = `${mergedPrefixCls.value}-quick-select-item-search-bar`
return normalizeClass({
[prefixCls]: true,
[`${prefixCls}-opened`]: props.searchInputActive,
})
})
const searchState = computed<SearchState | undefined>(() => getSearchStatesByFieldKey(props.searchField.key)[0])
const searchDataSegment = computed(() =>
props.searchField.segments.find(seg => searchDataTypes.includes(seg.name as SearchDataTypes)),
)
const searchDataSegmentState = computed(() =>
searchState.value?.segmentStates.find(seg => searchDataTypes.includes(seg.name as SearchDataTypes)),
)
const [itemValue, setItemValue] = useState<unknown>(searchDataSegmentState.value?.value)
watch(() => searchDataSegmentState.value?.value, setItemValue)
watch(
() => props.searchInputActive,
active => {
if (!active) {
setSearchInput('')
}
},
)
const confirmValue = (value: unknown) => {
let searchStateKey = searchState.value?.key
if (isNil(value) && searchStateKey) {
removeSearchState(searchStateKey)
return
}
if (!searchState.value) {
searchStateKey = createSearchState(props.searchField.key, {
value,
})!.key
callEmit(proSearchProps.onItemCreate, {
...convertStateToValue(searchStateKey),
nameInput: props.searchField.label,
})
} else if (searchDataSegment.value?.name) {
updateSegmentValue(searchState.value.key, searchDataSegment.value.name, value)
}
updateSearchValues()
}
const setValue = (value: unknown) => {
setItemValue(value)
}
const ok = () => confirmValue(itemValue.value)
const cancel = () => {
setItemValue(searchDataSegmentState.value?.value)
}
const setOnKeyDown = () => {}
const handleBlur = () => {
setSearchInputOpened(false)
}
const handleSearchIconClick = () => {
setSearchInputOpened(!props.searchInputActive)
}
const handleSearchIconMouseDown = (evt: MouseEvent) => {
evt.preventDefault()
evt.stopImmediatePropagation()
}
const handleContentMouseDown = (evt: MouseEvent) => {
if (props.searchInputActive) {
evt.preventDefault()
evt.stopImmediatePropagation()
}
}
return () => {
const prefixCls = `${mergedPrefixCls.value}-quick-select-item`
const classes = normalizeClass([prefixCls, ...(searchDataSegment.value?.containerClassName ?? [])])
const _getCacheData = (dataKey: string) => {
if (!searchState.value || !searchDataSegment.value) {
return
}
return getCacheData(searchState.value.key, searchDataSegment.value.name, dataKey)
}
const _setCacheData = (dataKey: string, data: any) => {
if (!searchState.value || !searchDataSegment.value) {
return
}
return setCacheData(searchState.value.key, searchDataSegment.value.name, dataKey, data)
}
return (
<div class={classes}>
<div class={`${prefixCls}-header`}>
<label class={`${prefixCls}-label`}>{props.searchField.label}</label>
{quickSelectSearchable.value && (
<div class={searchBarCls.value}>
<IxInput
ref={searchInputRef}
class={`${prefixCls}-search-input`}
value={searchInput.value}
onBlur={handleBlur}
onChange={setSearchInput}
borderless
/>
<IxIcon
ref={searchIconRef}
class={`${prefixCls}-search-icon`}
name="search"
onMousedown={handleSearchIconMouseDown}
onClick={handleSearchIconClick}
/>
</div>
)}
</div>
<div class={`${prefixCls}-content`} onMousedown={handleContentMouseDown}>
{searchDataSegment.value?.panelRenderer?.({
slots,
input: searchInput.value,
value: itemValue.value,
states: searchState.value?.segmentStates ?? [],
renderLocation: 'quick-select-panel',
ok,
cancel,
setValue,
setOnKeyDown,
getCacheData: _getCacheData,
setCacheData: _setCacheData,
})}
</div>
</div>
)
}
},
})