-
Notifications
You must be signed in to change notification settings - Fork 138
/
CreateNameSegment.tsx
97 lines (79 loc) 路 3.1 KB
/
CreateNameSegment.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
/**
* @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 { PanelRenderContext, SearchField, Segment, SelectPanelData } from '../types'
import type { VNodeChild } from 'vue'
import { isString } from 'lodash-es'
import { type VKey, convertArray } from '@idux/cdk/utils'
import SelectPanel from '../panel/SelectPanel'
import { filterDataSource, matchRule } from '../utils/selectData'
export const defaultNameSegmentEndSymbol = ':'
export function createNameSegment(
prefixCls: string,
searchFields: SearchField[] | undefined,
customNameLabel: string | ((searchField: SearchField) => VNodeChild) | undefined,
showEndSymbol: boolean,
): Segment<VKey | undefined> {
const names = getSearchOptionNameList(searchFields ?? [])
const panelRenderer = (context: PanelRenderContext<VKey | undefined>) => {
const { slots, input, value, setValue, ok, setOnKeyDown } = context
const handleChange = (value: VKey[]) => {
setValue(value[0])
ok()
}
const filteredDataSource = filterDataSource(names, nameOption => matchRule(nameOption.label, getRawInput(input)))
if (!filteredDataSource?.length) {
return
}
const renderNameLabel = (key: VKey, renderer: (searchField: SearchField) => VNodeChild) => {
const searchField = searchFields!.find(field => field.key === key)!
return renderer(searchField)
}
const _customNameLabel = customNameLabel ?? 'nameLabel'
const customNameLabelRender = isString(_customNameLabel) ? slots[_customNameLabel] : _customNameLabel
const panelSlots = {
optionLabel: customNameLabelRender
? (option: SelectPanelData) => renderNameLabel(option.key, customNameLabelRender)
: undefined,
}
return (
<SelectPanel
class={`${prefixCls}-name-segment-panel`}
v-slots={panelSlots}
value={convertArray(value)}
dataSource={filteredDataSource}
multiple={false}
onChange={handleChange}
onConfirm={ok}
setOnKeyDown={setOnKeyDown}
/>
)
}
return {
name: 'name',
inputClassName: `${prefixCls}-name-segment-input`,
parse: input => parseInput(input, names),
format: value => formatValue(value, names, showEndSymbol),
panelRenderer,
}
}
function getRawInput(input: string): string {
return input.trim().replace(new RegExp(`${defaultNameSegmentEndSymbol}$`), '')
}
function getSearchOptionNameList(dataSource: SearchField[]): { key: VKey; label: string }[] {
return dataSource.map(data => ({ key: data.key, label: data.label }))
}
function parseInput(input: string, names: { key: VKey; label: string }[]): VKey | undefined {
const name = getRawInput(input)
return names.find(item => item.label === name)?.key
}
function formatValue(value: VKey | undefined, names: { key: VKey; label: string }[], showEndSymbol: boolean): string {
if (!value) {
return ''
}
const name = names.find(item => item.key === value)?.label ?? ''
return name && showEndSymbol ? name + defaultNameSegmentEndSymbol : name
}