-
Notifications
You must be signed in to change notification settings - Fork 138
/
useSegmentStates.ts
266 lines (226 loc) Β· 7.72 KB
/
useSegmentStates.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @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 type { ProSearchContext } from '../token'
import { type ComputedRef, type Ref, nextTick, onUnmounted, ref, watch } from 'vue'
import { isNil, isNumber } from 'lodash-es'
import { callEmit } from '@idux/cdk/utils'
import { type SegmentValue } from '../composables/useSearchStates'
import { type ProSearchProps, type SearchItemProps, Segment, searchDataTypes } from '../types'
interface SegmentState {
input: string
value: unknown
index: number
selectionStart: number
}
type SegmentStates = Record<string, SegmentState>
export interface SegmentStatesContext {
segmentStates: Ref<SegmentStates>
initSegmentStates: (force?: boolean) => void
changeActiveAndSelect: (index: number, selectionStart: number | 'start' | 'end') => void
handleSegmentInput: (name: string, input: string) => void
handleSegmentChange: (name: string, value: unknown) => void
handleSegmentConfirm: (name: string, confirmItem?: boolean) => void
handleSegmentCancel: (name: string) => void
handleSegmentSelect: (name: string, selectionStart: number | undefined | null) => void
}
export function useSegmentStates(
props: SearchItemProps,
proSearchProps: ProSearchProps,
proSearchContext: ProSearchContext,
isActive: ComputedRef<boolean>,
): SegmentStatesContext {
const {
getSearchStateByKey,
validateSearchState,
updateSearchState,
updateSegmentValue,
removeSearchState,
convertStateToValue,
activeSegment,
changeActive,
setInactive,
setTempActive,
setOverlayOpened,
onSearchTrigger,
watchSearchState,
} = proSearchContext
const segmentStates = ref<SegmentStates>({})
const _genInitSegmentState = (
segment: Segment,
segmentValue: SegmentValue | undefined,
index: number,
): SegmentState => {
return {
input: segment.format(segmentValue?.value) ?? '',
value: segmentValue?.value,
index: index,
selectionStart: 0,
}
}
// reset temp segment states
const initSegmentStates = () => {
const searchState = getSearchStateByKey(props.searchItem!.key)!
segmentStates.value = props.searchItem!.resolvedSearchField.segments.reduce((states, segment, index) => {
const segmentValue = searchState?.segmentValues.find(value => value.name === segment.name)
states[segment.name] = _genInitSegmentState(segment, segmentValue, index)
return states
}, {} as SegmentStates)
}
// reset certain segment state indentified by name
const initSegmentState = (name: string) => {
const segmentState = segmentStates.value[name]
if (!segmentState) {
return
}
const searchState = getSearchStateByKey(props.searchItem!.key)!
const segment = props.searchItem!.resolvedSearchField.segments[segmentState.index]
const segmentValue = searchState?.segmentValues.find(value => value.name === segment.name)
segmentStates.value[name] = _genInitSegmentState(segment, segmentValue, segmentState.index)
}
const changeActiveAndSelect = (offset: number, selectionStart: number | 'start' | 'end') => {
const currentIndex = activeSegment.value?.name ? segmentStates.value[activeSegment.value.name].index : -1
if (currentIndex < 0) {
return
}
const length = props.searchItem!.resolvedSearchField.segments.length
const _offset =
offset > 0 ? Math.min(offset, length - 1 - currentIndex) : Math.min(offset, -length + 1 + currentIndex)
if (_offset === 0) {
return
}
changeActive(_offset, false)
if (isNil(selectionStart)) {
return
}
const targetSegmentName = props.searchItem!.resolvedSearchField.segments[currentIndex + _offset]?.name
const targetSegmentState = segmentStates.value[targetSegmentName]
if (!targetSegmentState) {
return
}
/* eslint-disable indent */
const _selectionStart = isNumber(selectionStart)
? selectionStart
: selectionStart === 'start'
? 0
: targetSegmentState.input.length
/* eslint-enable indent */
targetSegmentState.selectionStart = _selectionStart
}
let searchStateWatchStop: () => void
watch(
() => props.searchItem,
(searchItem, oldSearchItem) => {
if (searchItem?.key === oldSearchItem?.key) {
return
}
searchStateWatchStop?.()
initSegmentStates()
if (searchItem?.key) {
searchStateWatchStop = watchSearchState(searchItem?.key, () => nextTick(initSegmentStates))
}
},
{
immediate: true,
},
)
watch([isActive, () => props.searchItem?.resolvedSearchField], ([active, searchField]) => {
if (!active || !searchField) {
initSegmentStates()
}
})
onUnmounted(() => {
searchStateWatchStop?.()
})
const setSegmentValue = (name: string, value: unknown) => {
if (!segmentStates.value[name]) {
return
}
const segment = props.searchItem!.resolvedSearchField.segments.find(seg => seg.name === name)!
segmentStates.value[name].value = value
segmentStates.value[name].input = segment.format(value)
}
const setSegmentInput = (name: string, input: string) => {
if (!segmentStates.value[name]) {
return
}
const segment = props.searchItem!.resolvedSearchField.segments.find(seg => seg.name === name)!
segmentStates.value[name].input = input
segmentStates.value[name].value = segment.parse(input)
}
const setSegmentSelectionStart = (name: string, selectionStart: number | undefined | null) => {
if (!segmentStates.value[name]) {
return
}
segmentStates.value[name].selectionStart = selectionStart ?? 0
}
const confirmSearchItem = () => {
const key = props.searchItem!.key
const validateRes = validateSearchState(key)
const searchValue = convertStateToValue(key)
Object.entries(segmentStates.value).forEach(([name, state]) => {
updateSegmentValue(state.value, name, key)
})
if (!validateRes) {
removeSearchState(key)
} else {
updateSearchState(key)
}
const valueName = searchDataTypes.find(name => !!segmentStates.value[name])
callEmit(proSearchProps.onItemConfirm, {
...(searchValue ?? {}),
nameInput: searchValue?.name ?? '',
operatorInput: segmentStates.value.operator?.input,
valueInput: valueName && segmentStates.value[valueName]?.input,
removed: !validateRes,
})
setTempActive()
setOverlayOpened(false)
}
const handleSegmentConfirm = (name: string, confirmItem?: boolean) => {
const segmentState = segmentStates.value[name]
if (!segmentState) {
return
}
const { index } = segmentState
// only confirm searchItem when the last segment is confirmed
// if the last segment is searchItem name, confirm the searchItem only when no searchField is selected
if (index === props.searchItem!.resolvedSearchField.segments.length - 1 && confirmItem) {
confirmSearchItem()
} else {
changeActive(1)
}
}
const handleSegmentCancel = (name: string) => {
// init the canceled segment state
initSegmentState(name)
if (!segmentStates.value[name].value) {
changeActive(-1, true)
} else {
setInactive()
}
}
onSearchTrigger(() => {
if (
!props.searchItem?.key ||
!activeSegment.value?.itemKey ||
activeSegment.value.itemKey !== props.searchItem.key
) {
return
}
handleSegmentConfirm(activeSegment.value.name, true)
}, 'pre')
return {
segmentStates,
initSegmentStates,
changeActiveAndSelect,
handleSegmentInput: setSegmentInput,
handleSegmentChange: setSegmentValue,
handleSegmentSelect: setSegmentSelectionStart,
handleSegmentConfirm,
handleSegmentCancel,
}
}