-
Notifications
You must be signed in to change notification settings - Fork 138
/
SelectPanel.tsx
212 lines (190 loc) Β· 5.75 KB
/
SelectPanel.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
201
202
203
204
205
206
207
208
209
210
211
212
/**
* @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 ComputedRef,
type Ref,
computed,
defineComponent,
inject,
onBeforeUnmount,
onMounted,
onUnmounted,
ref,
watch,
} from 'vue'
import { type VKey, callEmit, useState } from '@idux/cdk/utils'
import { IxCheckbox } from '@idux/components/checkbox'
import { IxSelectPanel, type SelectData, type SelectPanelInstance } from '@idux/components/select'
import PanelFooter from './PanelFooter'
import { proSearchContext } from '../token'
import { type ProSearchSelectPanelProps, type SelectPanelData, proSearchSelectPanelProps } from '../types'
import { filterDataSource, matchRule } from '../utils/selectData'
export default defineComponent({
props: proSearchSelectPanelProps,
setup(props, { slots }) {
const { locale, mergedPrefixCls } = inject(proSearchContext)!
const [activeValue, setActiveValue] = useState<VKey | undefined>(undefined)
const partiallySelected = computed(() => props.value && props.value.length > 0 && !props.allSelected)
const filteredDataSource = computed(() => {
const { dataSource, searchValue, searchFn } = props
if (!searchValue) {
return dataSource
}
const trimedSearchValue = searchValue?.trim() ?? ''
const mergedSearchFn = searchFn
? (option: SelectPanelData) => searchFn(option, trimedSearchValue)
: (option: SelectPanelData) => matchRule(option.label, trimedSearchValue)
return filterDataSource(dataSource ?? [], mergedSearchFn)
})
watch(
() => props.value,
value => {
const key = value?.[value.length - 1]
key && setActiveValue(key)
},
{ immediate: true },
)
watch(
() => props.searchValue,
searchValue => {
callEmit(props.onSearch, searchValue ?? '')
},
)
onUnmounted(() => {
if (props.searchValue) {
callEmit(props.onSearch, '')
}
})
const panelRef = ref<SelectPanelInstance>()
const changeSelected = (key: VKey) => {
const multiple = !!props.multiple
const currValue = props.value ?? []
const targetIndex = currValue.findIndex(item => item === key)
const isSelected = targetIndex > -1
if (!multiple) {
callEmit(props.onChange, [key])
return
}
if (isSelected) {
callEmit(
props.onChange,
currValue.filter((_, index) => targetIndex !== index),
)
return
}
callEmit(props.onChange, [...currValue, key])
}
const handleOptionClick = (option: SelectData) => {
changeSelected(option.key!)
}
const handleConfirm = () => {
callEmit(props.onConfirm)
}
const handleCancel = () => {
callEmit(props.onCancel)
}
const handleSelectAllClick = () => {
callEmit(props.onSelectAllClick)
}
const handleKeyDown = useOnKeyDown(props, panelRef, activeValue, changeSelected, handleConfirm)
onMounted(() => {
props.setOnKeyDown?.(handleKeyDown)
})
onBeforeUnmount(() => {
props.setOnKeyDown?.(undefined)
})
const renderSelectAll = () => {
if (!props.multiple || !props.showSelectAll) {
return
}
const prefixCls = `${mergedPrefixCls.value}-select-panel-select-all-option`
return (
<div
class={prefixCls}
title={locale.selectAll}
aria-label={locale.selectAll}
aria-selected={props.allSelected}
onClick={handleSelectAllClick}
>
<IxCheckbox checked={props.allSelected} indeterminate={partiallySelected.value} />
<span class={`${prefixCls}-label`}>{locale.selectAll}</span>
</div>
)
}
const renderFooter = () => {
if (!props.multiple) {
return
}
return (
<PanelFooter
prefixCls={mergedPrefixCls.value}
locale={locale}
onConfirm={handleConfirm}
onCancel={handleCancel}
/>
)
}
return () => {
const prefixCls = `${mergedPrefixCls.value}-select-panel`
const panelProps = {
activeValue: activeValue.value,
dataSource: filteredDataSource.value,
multiple: props.multiple,
getKey: 'key',
labelKey: 'label',
selectedKeys: props.value,
onOptionClick: handleOptionClick,
'onUpdate:activeValue': setActiveValue,
}
return (
<div class={prefixCls} tabindex={-1} onMousedown={evt => evt.preventDefault()}>
{renderSelectAll()}
<IxSelectPanel ref={panelRef} class={`${prefixCls}-inner`} v-slots={slots} {...panelProps} />
{renderFooter()}
</div>
)
}
},
})
function useOnKeyDown(
props: ProSearchSelectPanelProps,
panelRef: Ref<SelectPanelInstance | undefined>,
activeValue: ComputedRef<VKey | undefined>,
changeSelected: (key: VKey) => void,
handleConfirm: () => void,
) {
return function (evt: KeyboardEvent) {
if (!panelRef.value) {
return true
}
switch (evt.key) {
case 'ArrowUp':
evt.preventDefault()
panelRef.value.changeActiveIndex(-1)
break
case 'ArrowDown':
evt.preventDefault()
panelRef.value.changeActiveIndex(1)
break
case 'Enter': {
if (!props.dataSource || props.dataSource.length <= 0) {
return true
}
evt.preventDefault()
if (!props.multiple || evt.ctrlKey) {
activeValue.value && changeSelected(activeValue.value)
} else if ((props.value && props.value.length > 0) || !activeValue.value) {
handleConfirm()
}
return false
}
default:
break
}
return true
}
}