niuma-ui 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +65 -1
- package/package.json +10 -2
- package/src/__tests__/RsButton.spec.ts +47 -0
- package/src/__tests__/RsCard.spec.ts +160 -1
- package/src/__tests__/RsConfirmDialog.spec.ts +127 -0
- package/src/__tests__/RsContextMenu.spec.ts +12 -0
- package/src/__tests__/RsDatePicker.spec.ts +21 -0
- package/src/__tests__/RsDateTimePicker.spec.ts +11 -0
- package/src/__tests__/RsDescriptions.spec.ts +50 -0
- package/src/__tests__/RsDialog.spec.ts +178 -0
- package/src/__tests__/RsDrawer.spec.ts +96 -3
- package/src/__tests__/RsDynamicTags.spec.ts +52 -0
- package/src/__tests__/RsForm.spec.ts +250 -6
- package/src/__tests__/RsFormItem.spec.ts +478 -0
- package/src/__tests__/RsFormList.spec.ts +109 -0
- package/src/__tests__/RsInput.spec.ts +108 -1
- package/src/__tests__/RsInputNumber.spec.ts +33 -0
- package/src/__tests__/RsMenu.spec.ts +406 -14
- package/src/__tests__/RsPagination.spec.ts +67 -2
- package/src/__tests__/RsSelect.spec.ts +465 -2
- package/src/__tests__/RsSwitch.spec.ts +74 -0
- package/src/__tests__/RsTable.a11y.spec.ts +83 -0
- package/src/__tests__/RsTable.dom-baseline.spec.ts +87 -0
- package/src/__tests__/RsTable.perf.spec.ts +23 -4
- package/src/__tests__/RsTable.spec.ts +63 -8
- package/src/__tests__/RsTable.ssr.spec.ts +71 -0
- package/src/__tests__/RsTable.summary.spec.ts +45 -0
- package/src/__tests__/RsTabs.spec.ts +102 -4
- package/src/__tests__/RsTimePicker.spec.ts +20 -0
- package/src/__tests__/RsTooltip.spec.ts +43 -0
- package/src/__tests__/RsUpload.spec.ts +62 -6
- package/src/__tests__/createRsDialog.spec.ts +145 -0
- package/src/__tests__/css-token.spec.ts +51 -0
- package/src/__tests__/date-picker-utils.spec.ts +49 -0
- package/src/__tests__/dialog-utils.spec.ts +43 -0
- package/src/__tests__/form-path.spec.ts +46 -0
- package/src/__tests__/form-rules.spec.ts +139 -0
- package/src/__tests__/perf-budgets.ts +32 -0
- package/src/__tests__/rs-table-api.spec.ts +206 -0
- package/src/__tests__/rs-table-compat-matrix.spec.ts +108 -0
- package/src/__tests__/rs-table-echarts-adapter.spec.ts +43 -0
- package/src/__tests__/rs-table-grid-nav.spec.ts +63 -0
- package/src/__tests__/rs-table-isolation.spec.ts +158 -0
- package/src/__tests__/split-pane-utils.spec.ts +21 -0
- package/src/__tests__/table-chart-utils.spec.ts +88 -0
- package/src/__tests__/table-summary-utils.spec.ts +62 -0
- package/src/__tests__/table-utils.spec.ts +124 -0
- package/src/__tests__/theme.spec.ts +27 -0
- package/src/__tests__/useRsTableColumnResize.spec.ts +25 -0
- package/src/__tests__/useRsTableCore.perf.spec.ts +170 -0
- package/src/components/RsAlert.vue +212 -0
- package/src/components/RsAvatar.vue +1 -1
- package/src/components/RsBadge.vue +1 -1
- package/src/components/RsBreadcrumb.vue +1 -1
- package/src/components/RsButton.vue +147 -23
- package/src/components/RsCalendarGrid.vue +3 -3
- package/src/components/RsCard.vue +204 -20
- package/src/components/RsCodeBlock.vue +4 -4
- package/src/components/RsCodeEditor.vue +8 -8
- package/src/components/RsConfirmDialog.vue +217 -30
- package/src/components/RsContextMenu.vue +57 -17
- package/src/components/RsDatePicker.vue +316 -39
- package/src/components/RsDateTimePicker.vue +27 -2
- package/src/components/RsDescriptions.vue +186 -0
- package/src/components/RsDescriptionsItem.vue +32 -0
- package/src/components/RsDialog.vue +374 -52
- package/src/components/RsDivider.vue +89 -0
- package/src/components/RsDrawer.vue +559 -34
- package/src/components/RsDropdown.vue +34 -3
- package/src/components/RsDynamicTags.vue +531 -0
- package/src/components/RsEmpty.vue +1 -1
- package/src/components/RsForm.vue +164 -8
- package/src/components/RsFormItem.vue +423 -0
- package/src/components/RsFormItemControl.vue +21 -0
- package/src/components/RsFormList.vue +117 -0
- package/src/components/RsInput.vue +491 -100
- package/src/components/RsInputNumber.vue +77 -27
- package/src/components/RsLabel.vue +20 -5
- package/src/components/RsLink.vue +1 -1
- package/src/components/RsLoadingBar.vue +151 -0
- package/src/components/RsMarkdown.vue +5 -5
- package/src/components/RsMenu.vue +74 -16
- package/src/components/RsMenuItems.vue +147 -9
- package/src/components/RsMonacoEditor.vue +9 -3
- package/src/components/RsPagination.vue +93 -14
- package/src/components/RsProseEditor.vue +3 -3
- package/src/components/RsRadio.vue +81 -0
- package/src/components/RsRadioItem.vue +156 -0
- package/src/components/RsSelect.vue +372 -642
- package/src/components/RsSidebarGroup.vue +1 -1
- package/src/components/RsSplitPane.vue +90 -1
- package/src/components/RsStatCard.vue +2 -2
- package/src/components/RsSteps.vue +2 -2
- package/src/components/RsSwitch.vue +192 -0
- package/src/components/RsTable.vue +653 -2555
- package/src/components/RsTabs.vue +422 -90
- package/src/components/RsTag.vue +191 -0
- package/src/components/RsTerminal.vue +58 -19
- package/src/components/RsTimePicker.vue +72 -9
- package/src/components/RsTimePickerColumns.vue +137 -78
- package/src/components/RsToaster.vue +64 -24
- package/src/components/RsTooltip.vue +10 -1
- package/src/components/RsTooltipProvider.vue +11 -1
- package/src/components/RsTree.vue +45 -27
- package/src/components/RsUpload.vue +147 -14
- package/src/components/RsVNodeHost.vue +20 -0
- package/src/components/button-utils.ts +39 -0
- package/src/components/context-menu-utils.ts +6 -0
- package/src/components/date-picker-utils.ts +195 -4
- package/src/components/descriptions-utils.ts +24 -0
- package/src/components/dialog-utils.ts +182 -0
- package/src/components/dialog-window.ts +74 -9
- package/src/components/drawer-utils.ts +73 -0
- package/src/components/form-item-control.ts +55 -0
- package/src/components/form-path.ts +92 -0
- package/src/components/form-rules.ts +311 -0
- package/src/components/form-utils.ts +145 -3
- package/src/components/menu-utils.ts +11 -0
- package/src/components/overlay-utils.ts +3 -0
- package/src/components/radio-utils.ts +12 -0
- package/src/components/reka.ts +5 -0
- package/src/components/select-utils.ts +285 -6
- package/src/components/split-pane-utils.ts +65 -12
- package/src/components/table/RsTableBody.vue +217 -0
- package/src/components/table/RsTableBodyRow.vue +90 -1
- package/src/components/table/RsTableColGroup.vue +41 -0
- package/src/components/table/RsTableHeader.vue +158 -0
- package/src/components/table/RsTableHeaderFilter.vue +1 -1
- package/src/components/table/RsTableSummaryRow.vue +52 -0
- package/src/components/table/rs-table-api.ts +99 -0
- package/src/components/table/rs-table-compat-matrix.ts +71 -0
- package/src/components/table/rs-table-echarts-adapter.ts +81 -0
- package/src/components/table/rs-table-feature-host.ts +178 -0
- package/src/components/table/rs-table-grid-nav.ts +122 -0
- package/src/components/table/rs-table-header-types.ts +47 -0
- package/src/components/table/rs-table-module-registry.ts +72 -0
- package/src/components/table/rs-table-props.ts +274 -0
- package/src/components/table/rs-table-view-context.ts +258 -0
- package/src/components/table/rs-table.css +85 -7
- package/src/components/table/table-chart-utils.ts +165 -0
- package/src/components/table/table-features.ts +284 -0
- package/src/components/table/table-summary-utils.ts +194 -0
- package/src/components/table-utils.ts +452 -2
- package/src/components/tabs-utils.ts +142 -2
- package/src/components/time-picker-utils.ts +76 -3
- package/src/components/upload-utils.ts +122 -0
- package/src/components/use-rs-select.ts +436 -0
- package/src/components/virtual-list-utils.ts +15 -2
- package/src/composables/assembleRsTableApi.ts +79 -0
- package/src/composables/bindRsTableViewContext.ts +269 -0
- package/src/composables/createRsDialog.ts +357 -0
- package/src/composables/createRsTableEditEmitBridge.ts +38 -0
- package/src/composables/useRsConfig.ts +10 -3
- package/src/composables/useRsI18n.ts +16 -5
- package/src/composables/useRsLoadingBar.ts +36 -0
- package/src/composables/useRsTable.ts +134 -0
- package/src/composables/useRsTableA11y.ts +52 -0
- package/src/composables/useRsTableChartBridge.ts +79 -0
- package/src/composables/useRsTableColumnLayout.ts +320 -0
- package/src/composables/useRsTableColumnResize.ts +244 -0
- package/src/composables/useRsTableColumnVirtual.ts +180 -0
- package/src/composables/useRsTableColumns.ts +200 -0
- package/src/composables/useRsTableContextMenu.ts +257 -0
- package/src/composables/useRsTableCore.ts +256 -0
- package/src/composables/useRsTableEditActions.ts +606 -0
- package/src/composables/useRsTableEditLayer.ts +251 -0
- package/src/composables/useRsTableEngine.ts +357 -0
- package/src/composables/useRsTableGridKeyboard.ts +123 -0
- package/src/composables/useRsTableHeadless.ts +137 -0
- package/src/composables/useRsTableInteraction.ts +433 -0
- package/src/composables/useRsTableScrollHost.ts +75 -0
- package/src/composables/useRsTableScrollLayout.ts +255 -0
- package/src/composables/useRsTableSelectionSource.ts +89 -0
- package/src/composables/useRsTableShell.ts +416 -0
- package/src/composables/useRsTableShellChrome.ts +170 -0
- package/src/composables/useRsTableSummary.ts +113 -0
- package/src/composables/useRsTableViewProvide.ts +263 -0
- package/src/composables/useRsTableVirtual.ts +154 -0
- package/src/composables/useRsTabsNav.ts +4 -0
- package/src/composables/useTableEdit.ts +1 -1
- package/src/index.ts +378 -10
- package/src/locale/interpolate.ts +32 -0
- package/src/locale/messages.ts +40 -0
- package/src/styles.css +365 -56
- package/src/theme/brand.example.css +24 -2
- package/src/theme/css-token.ts +107 -0
- package/src/theme/presets.ts +4 -0
- package/src/theme/types.ts +68 -0
- package/src/components/RsTableColgroup.vue +0 -17
- package/src/components/RsTableHeaderRow.vue +0 -30
|
@@ -30,16 +30,89 @@ export function rangeInclusive(max: number): number[] {
|
|
|
30
30
|
return Array.from({ length: max + 1 }, (_, index) => index)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/** 时分秒列选项(value + 预格式化 label,避免模板内反复 padStart) */
|
|
34
|
+
export interface RsTimeUnitOption {
|
|
35
|
+
value: number
|
|
36
|
+
label: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 时间列单位 */
|
|
40
|
+
export type RsTimeUnit = 'hour' | 'minute' | 'second'
|
|
41
|
+
|
|
42
|
+
export function formatTimeUnitLabel(value: number): string {
|
|
43
|
+
return String(value).padStart(2, '0')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function buildTimeUnitOptions(max: number, step = 1): RsTimeUnitOption[] {
|
|
47
|
+
const options: RsTimeUnitOption[] = []
|
|
48
|
+
for (let value = 0; value <= max; value += step) {
|
|
49
|
+
options.push({ value, label: formatTimeUnitLabel(value) })
|
|
50
|
+
}
|
|
51
|
+
return options
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 小时列固定选项 00–23 */
|
|
55
|
+
export const TIME_HOUR_OPTIONS: readonly RsTimeUnitOption[] = buildTimeUnitOptions(23)
|
|
56
|
+
|
|
57
|
+
/** 秒列固定选项 00–59 */
|
|
58
|
+
export const TIME_SECOND_OPTIONS: readonly RsTimeUnitOption[] = buildTimeUnitOptions(59)
|
|
59
|
+
|
|
60
|
+
const minuteOptionsCache = new Map<number, readonly RsTimeUnitOption[]>()
|
|
61
|
+
|
|
62
|
+
/** 按步进缓存分钟列选项,默认 step=1 为 00–59 */
|
|
63
|
+
export function getTimeMinuteOptions(step = 1): readonly RsTimeUnitOption[] {
|
|
64
|
+
const normalized = Math.max(1, Math.floor(step) || 1)
|
|
65
|
+
const cached = minuteOptionsCache.get(normalized)
|
|
66
|
+
if (cached) return cached
|
|
67
|
+
const options = buildTimeUnitOptions(59, normalized)
|
|
68
|
+
minuteOptionsCache.set(normalized, options)
|
|
69
|
+
return options
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 将时间列滚到指定 value,只改容器 scrollTop。
|
|
74
|
+
* 避免 scrollIntoView 带动外层页面 / Popover 滚动。
|
|
75
|
+
*/
|
|
76
|
+
export function scrollTimeColumnToValue(
|
|
77
|
+
container: HTMLElement | null | undefined,
|
|
78
|
+
value: number,
|
|
79
|
+
): void {
|
|
80
|
+
if (!container) return
|
|
81
|
+
const item = container.querySelector<HTMLElement>(`:scope > [data-value="${value}"]`)
|
|
82
|
+
if (!item) return
|
|
83
|
+
const top = item.offsetTop - (container.clientHeight - item.offsetHeight) / 2
|
|
84
|
+
container.scrollTop = Math.max(0, top)
|
|
85
|
+
}
|
|
86
|
+
|
|
33
87
|
export function parseTimeValue(value?: string, withSeconds = false): RsParsedTime | null {
|
|
34
88
|
if (!value?.trim()) return null
|
|
35
89
|
const trimmed = value.trim()
|
|
36
|
-
|
|
37
|
-
|
|
90
|
+
if (withSeconds) {
|
|
91
|
+
const withSec = TIME_WITH_SECONDS_PATTERN.exec(trimmed)
|
|
92
|
+
if (withSec) {
|
|
93
|
+
return {
|
|
94
|
+
hour: Number.parseInt(withSec[1], 10),
|
|
95
|
+
minute: Number.parseInt(withSec[2], 10),
|
|
96
|
+
second: Number.parseInt(withSec[3], 10),
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// 兼容仅到分的旧值,秒补 0
|
|
100
|
+
const minuteOnly = TIME_PATTERN.exec(trimmed)
|
|
101
|
+
if (minuteOnly) {
|
|
102
|
+
return {
|
|
103
|
+
hour: Number.parseInt(minuteOnly[1], 10),
|
|
104
|
+
minute: Number.parseInt(minuteOnly[2], 10),
|
|
105
|
+
second: 0,
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return null
|
|
109
|
+
}
|
|
110
|
+
const match = TIME_PATTERN.exec(trimmed)
|
|
38
111
|
if (!match) return null
|
|
39
112
|
return {
|
|
40
113
|
hour: Number.parseInt(match[1], 10),
|
|
41
114
|
minute: Number.parseInt(match[2], 10),
|
|
42
|
-
second:
|
|
115
|
+
second: 0,
|
|
43
116
|
}
|
|
44
117
|
}
|
|
45
118
|
|
|
@@ -53,3 +53,125 @@ export function mergeUploadFiles(current: readonly File[], next: readonly File[]
|
|
|
53
53
|
export function removeUploadFileAt(files: readonly File[], index: number): File[] {
|
|
54
54
|
return files.filter((_, currentIndex) => currentIndex !== index)
|
|
55
55
|
}
|
|
56
|
+
|
|
57
|
+
/** 触发浏览器下载指定 File / Blob */
|
|
58
|
+
export function downloadUploadFile(file: File | Blob, filename?: string): void {
|
|
59
|
+
const name = filename || (file instanceof File ? file.name : 'download')
|
|
60
|
+
const url = URL.createObjectURL(file)
|
|
61
|
+
const link = document.createElement('a')
|
|
62
|
+
link.href = url
|
|
63
|
+
link.download = name
|
|
64
|
+
document.body.appendChild(link)
|
|
65
|
+
link.click()
|
|
66
|
+
document.body.removeChild(link)
|
|
67
|
+
URL.revokeObjectURL(url)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 将文本等内容还原为可回显的 File(编辑态回填) */
|
|
71
|
+
export function createUploadFileFromContent(
|
|
72
|
+
name: string,
|
|
73
|
+
content: BlobPart,
|
|
74
|
+
type = 'application/octet-stream',
|
|
75
|
+
): File {
|
|
76
|
+
return new File([content], name, { type })
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 扩展名 → Lucide 图标名(kebab-case) */
|
|
80
|
+
const UPLOAD_EXT_ICON: Record<string, string> = {
|
|
81
|
+
// 证书 / 密钥
|
|
82
|
+
pem: 'file-key',
|
|
83
|
+
crt: 'file-key',
|
|
84
|
+
cer: 'file-key',
|
|
85
|
+
key: 'key-round',
|
|
86
|
+
p12: 'file-lock',
|
|
87
|
+
pfx: 'file-lock',
|
|
88
|
+
// 文本 / 文档
|
|
89
|
+
txt: 'file-text',
|
|
90
|
+
md: 'file-text',
|
|
91
|
+
markdown: 'file-text',
|
|
92
|
+
log: 'file-text',
|
|
93
|
+
pdf: 'file-type',
|
|
94
|
+
doc: 'file-text',
|
|
95
|
+
docx: 'file-text',
|
|
96
|
+
rtf: 'file-text',
|
|
97
|
+
// 表格
|
|
98
|
+
csv: 'file-spreadsheet',
|
|
99
|
+
xls: 'file-spreadsheet',
|
|
100
|
+
xlsx: 'file-spreadsheet',
|
|
101
|
+
// 代码 / 配置
|
|
102
|
+
json: 'file-braces',
|
|
103
|
+
yaml: 'file-code',
|
|
104
|
+
yml: 'file-code',
|
|
105
|
+
xml: 'file-code',
|
|
106
|
+
html: 'file-code',
|
|
107
|
+
htm: 'file-code',
|
|
108
|
+
css: 'file-code',
|
|
109
|
+
scss: 'file-code',
|
|
110
|
+
js: 'file-code',
|
|
111
|
+
mjs: 'file-code',
|
|
112
|
+
cjs: 'file-code',
|
|
113
|
+
ts: 'file-code',
|
|
114
|
+
tsx: 'file-code',
|
|
115
|
+
jsx: 'file-code',
|
|
116
|
+
vue: 'file-code',
|
|
117
|
+
go: 'file-code',
|
|
118
|
+
py: 'file-code',
|
|
119
|
+
java: 'file-code',
|
|
120
|
+
rs: 'file-code',
|
|
121
|
+
sql: 'file-terminal',
|
|
122
|
+
sh: 'file-terminal',
|
|
123
|
+
bat: 'file-terminal',
|
|
124
|
+
ps1: 'file-terminal',
|
|
125
|
+
// 图片
|
|
126
|
+
png: 'file-image',
|
|
127
|
+
jpg: 'file-image',
|
|
128
|
+
jpeg: 'file-image',
|
|
129
|
+
gif: 'file-image',
|
|
130
|
+
webp: 'file-image',
|
|
131
|
+
svg: 'file-image',
|
|
132
|
+
bmp: 'file-image',
|
|
133
|
+
ico: 'file-image',
|
|
134
|
+
// 音视频
|
|
135
|
+
mp3: 'file-music',
|
|
136
|
+
wav: 'file-music',
|
|
137
|
+
flac: 'file-music',
|
|
138
|
+
aac: 'file-music',
|
|
139
|
+
mp4: 'file-video-camera',
|
|
140
|
+
webm: 'file-video-camera',
|
|
141
|
+
mov: 'file-video-camera',
|
|
142
|
+
avi: 'file-video-camera',
|
|
143
|
+
// 压缩包
|
|
144
|
+
zip: 'file-archive',
|
|
145
|
+
rar: 'file-archive',
|
|
146
|
+
'7z': 'file-archive',
|
|
147
|
+
tar: 'file-archive',
|
|
148
|
+
gz: 'file-archive',
|
|
149
|
+
tgz: 'file-archive',
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** MIME 前缀 → Lucide 图标名(扩展名未知时回退) */
|
|
153
|
+
const UPLOAD_MIME_ICON: Array<{ prefix: string; icon: string }> = [
|
|
154
|
+
{ prefix: 'image/', icon: 'file-image' },
|
|
155
|
+
{ prefix: 'audio/', icon: 'file-music' },
|
|
156
|
+
{ prefix: 'video/', icon: 'file-video-camera' },
|
|
157
|
+
{ prefix: 'text/', icon: 'file-text' },
|
|
158
|
+
{ prefix: 'application/pdf', icon: 'file-type' },
|
|
159
|
+
{ prefix: 'application/json', icon: 'file-braces' },
|
|
160
|
+
{ prefix: 'application/zip', icon: 'file-archive' },
|
|
161
|
+
{ prefix: 'application/x-tar', icon: 'file-archive' },
|
|
162
|
+
{ prefix: 'application/gzip', icon: 'file-archive' },
|
|
163
|
+
]
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 按文件名扩展名 / MIME 解析列表图标(Lucide kebab-case)。
|
|
167
|
+
* 未匹配时返回 `file`。
|
|
168
|
+
*/
|
|
169
|
+
export function resolveUploadFileIcon(file: Pick<File, 'name' | 'type'> | string): string {
|
|
170
|
+
const name = typeof file === 'string' ? file : file.name
|
|
171
|
+
const mime = typeof file === 'string' ? '' : file.type || ''
|
|
172
|
+
const ext = name.includes('.') ? name.split('.').pop()!.toLowerCase() : ''
|
|
173
|
+
if (ext && UPLOAD_EXT_ICON[ext]) return UPLOAD_EXT_ICON[ext]
|
|
174
|
+
const byMime = UPLOAD_MIME_ICON.find((item) => mime.startsWith(item.prefix))
|
|
175
|
+
if (byMime) return byMime.icon
|
|
176
|
+
return 'file'
|
|
177
|
+
}
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import { computed, nextTick, watch, type ModelRef } from 'vue'
|
|
2
|
+
import type { RsTranslateFn } from '../composables/useRsI18n'
|
|
3
|
+
import { useFilter } from './reka'
|
|
4
|
+
import {
|
|
5
|
+
buildOptionDisabledMap,
|
|
6
|
+
buildOptionLabelMap,
|
|
7
|
+
comboboxBindingToTokens,
|
|
8
|
+
filterSelectOptions,
|
|
9
|
+
findSelectOption,
|
|
10
|
+
flattenSelectOptions,
|
|
11
|
+
flattenSelectValues,
|
|
12
|
+
fromComboboxValue,
|
|
13
|
+
isSelectLabeledValue,
|
|
14
|
+
normalizeSelectOptions,
|
|
15
|
+
optionDisplayLabel,
|
|
16
|
+
packSelectModel,
|
|
17
|
+
restoreSelectValue,
|
|
18
|
+
restoreSelectValues,
|
|
19
|
+
selectModelEntries,
|
|
20
|
+
sortSelectOptions,
|
|
21
|
+
splitByTokenSeparators,
|
|
22
|
+
toComboboxValue,
|
|
23
|
+
toSelectedTokens,
|
|
24
|
+
type RsSelectFieldNames,
|
|
25
|
+
type RsSelectFilterOption,
|
|
26
|
+
type RsSelectFilterSort,
|
|
27
|
+
type RsSelectModelValue,
|
|
28
|
+
type RsSelectOption,
|
|
29
|
+
type RsSelectOptionFilterProp,
|
|
30
|
+
type RsSelectOptionsInput,
|
|
31
|
+
type RsSelectValue,
|
|
32
|
+
} from './select-utils'
|
|
33
|
+
|
|
34
|
+
/** RsSelect 行为层入参(不含尺寸 / 表单外观) */
|
|
35
|
+
export interface RsSelectEngineProps {
|
|
36
|
+
options: RsSelectOptionsInput
|
|
37
|
+
fieldNames?: RsSelectFieldNames
|
|
38
|
+
optionLabelProp?: string
|
|
39
|
+
optionFilterProp?: RsSelectOptionFilterProp
|
|
40
|
+
filterOption?: RsSelectFilterOption | boolean
|
|
41
|
+
filterSort?: RsSelectFilterSort
|
|
42
|
+
searchable?: boolean
|
|
43
|
+
creatable?: boolean
|
|
44
|
+
multiple?: boolean
|
|
45
|
+
remote?: boolean
|
|
46
|
+
virtual?: boolean
|
|
47
|
+
virtualThreshold?: number
|
|
48
|
+
maxTagCount?: number
|
|
49
|
+
maxTagPlaceholder?: string | ((omitted: number) => string)
|
|
50
|
+
maxTagTooltip?: boolean
|
|
51
|
+
maxTagTextLength?: number
|
|
52
|
+
multipleLimit?: number
|
|
53
|
+
labelInValue?: boolean
|
|
54
|
+
tokenSeparators?: string[]
|
|
55
|
+
autoClearSearchValue?: boolean
|
|
56
|
+
searchPlaceholder?: string
|
|
57
|
+
emptyText?: string
|
|
58
|
+
loadingText?: string
|
|
59
|
+
placeholder?: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type RsSelectEngineEmit = {
|
|
63
|
+
(e: 'search', query: string): void
|
|
64
|
+
(e: 'select', value: RsSelectValue, option?: RsSelectOption): void
|
|
65
|
+
(e: 'deselect', value: RsSelectValue, option?: RsSelectOption): void
|
|
66
|
+
(e: 'clear'): void
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Select 过滤、选中、多选 tag、creatable / 分隔符提交。
|
|
71
|
+
* 组件模板只消费返回值;表单校验仍留在 SFC。
|
|
72
|
+
*/
|
|
73
|
+
export function useRsSelect(
|
|
74
|
+
props: RsSelectEngineProps,
|
|
75
|
+
model: ModelRef<RsSelectModelValue>,
|
|
76
|
+
open: ModelRef<boolean>,
|
|
77
|
+
searchQuery: ModelRef<string>,
|
|
78
|
+
emit: RsSelectEngineEmit,
|
|
79
|
+
t: RsTranslateFn,
|
|
80
|
+
) {
|
|
81
|
+
const { contains } = useFilter({ sensitivity: 'base' })
|
|
82
|
+
|
|
83
|
+
const normalizedOptions = computed(() =>
|
|
84
|
+
normalizeSelectOptions(props.options, props.fieldNames),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
const resolvedPlaceholder = computed(() => props.placeholder ?? t('select.placeholder'))
|
|
88
|
+
const resolvedSearchPlaceholder = computed(
|
|
89
|
+
() => props.searchPlaceholder ?? t('select.searchPlaceholder'),
|
|
90
|
+
)
|
|
91
|
+
const resolvedEmptyText = computed(() => props.emptyText ?? t('select.empty'))
|
|
92
|
+
const resolvedLoadingText = computed(() => props.loadingText ?? t('select.loading'))
|
|
93
|
+
const isSearchable = computed(() => Boolean(props.searchable || props.creatable))
|
|
94
|
+
|
|
95
|
+
const labelMap = computed(() => buildOptionLabelMap(normalizedOptions.value))
|
|
96
|
+
const disabledMap = computed(() => buildOptionDisabledMap(normalizedOptions.value))
|
|
97
|
+
const flatOptions = computed(() => flattenSelectOptions(normalizedOptions.value))
|
|
98
|
+
const flatCount = computed(() => flatOptions.value.length)
|
|
99
|
+
const useVirtual = computed(
|
|
100
|
+
() => Boolean(props.virtual) || flatCount.value > (props.virtualThreshold ?? 50),
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
const useManualFilter = computed(
|
|
104
|
+
() =>
|
|
105
|
+
Boolean(props.remote) ||
|
|
106
|
+
useVirtual.value ||
|
|
107
|
+
typeof props.filterOption === 'function' ||
|
|
108
|
+
props.filterOption === false ||
|
|
109
|
+
Boolean(props.optionFilterProp && props.optionFilterProp !== 'label') ||
|
|
110
|
+
Boolean(props.filterSort) ||
|
|
111
|
+
Boolean(props.fieldNames),
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
const displayOptions = computed(() => {
|
|
115
|
+
let next = normalizedOptions.value
|
|
116
|
+
if (!props.remote && props.filterOption !== false) {
|
|
117
|
+
if (useManualFilter.value || props.filterOption) {
|
|
118
|
+
next = filterSelectOptions(
|
|
119
|
+
normalizedOptions.value,
|
|
120
|
+
searchQuery.value,
|
|
121
|
+
contains,
|
|
122
|
+
props.filterOption,
|
|
123
|
+
props.optionFilterProp,
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return sortSelectOptions(next, props.filterSort)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
const createValue = computed(() => searchQuery.value.trim())
|
|
131
|
+
|
|
132
|
+
const canCreate = computed(() => {
|
|
133
|
+
if (!props.creatable) return false
|
|
134
|
+
const q = createValue.value
|
|
135
|
+
if (!q) return false
|
|
136
|
+
const lower = q.toLowerCase()
|
|
137
|
+
return !flatOptions.value.some(
|
|
138
|
+
(opt) =>
|
|
139
|
+
String(opt.value).toLowerCase() === lower || String(opt.label).toLowerCase() === lower,
|
|
140
|
+
)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const createOptionLabel = computed(() =>
|
|
144
|
+
t('select.createOption', '使用 “{query}”').replace('{query}', createValue.value),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
const virtualValues = computed(() => {
|
|
148
|
+
const values = flattenSelectValues(displayOptions.value)
|
|
149
|
+
if (!canCreate.value) return values
|
|
150
|
+
return [createValue.value, ...values.filter((item) => String(item) !== createValue.value)]
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
const selectedValues = computed(() => toSelectedTokens(model.value, Boolean(props.multiple)))
|
|
154
|
+
const hasValue = computed(() => selectedValues.value.length > 0)
|
|
155
|
+
|
|
156
|
+
const visibleTagTokens = computed(() => {
|
|
157
|
+
const all = selectedValues.value
|
|
158
|
+
if (!props.multiple || props.maxTagCount == null || all.length <= props.maxTagCount) {
|
|
159
|
+
return all
|
|
160
|
+
}
|
|
161
|
+
return all.slice(0, Math.max(0, props.maxTagCount))
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
const omittedTagCount = computed(() =>
|
|
165
|
+
Math.max(0, selectedValues.value.length - visibleTagTokens.value.length),
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
const omittedTagLabel = computed(() => {
|
|
169
|
+
const count = omittedTagCount.value
|
|
170
|
+
if (typeof props.maxTagPlaceholder === 'function') return props.maxTagPlaceholder(count)
|
|
171
|
+
if (props.maxTagPlaceholder) return props.maxTagPlaceholder
|
|
172
|
+
return t('select.maxTagPlaceholder', { count })
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
function optionFromToken(token: string): RsSelectOption | undefined {
|
|
176
|
+
return findSelectOption(normalizedOptions.value, String(token))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function tokenLabel(token: string): string {
|
|
180
|
+
if (props.labelInValue) {
|
|
181
|
+
for (const entry of selectModelEntries(model.value)) {
|
|
182
|
+
if (
|
|
183
|
+
isSelectLabeledValue(entry) &&
|
|
184
|
+
toComboboxValue(entry.value) === token &&
|
|
185
|
+
entry.label
|
|
186
|
+
) {
|
|
187
|
+
return entry.label
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const option = optionFromToken(token)
|
|
192
|
+
if (option) return optionDisplayLabel(option, props.optionLabelProp)
|
|
193
|
+
return labelMap.value.get(token) ?? fromComboboxValue(token)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const omittedTagTitle = computed(() => {
|
|
197
|
+
if (!props.maxTagTooltip) return omittedTagLabel.value
|
|
198
|
+
const omitted = selectedValues.value.slice(visibleTagTokens.value.length)
|
|
199
|
+
if (!omitted.length) return omittedTagLabel.value
|
|
200
|
+
return omitted.map((token) => tokenLabel(token)).join('、')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
function truncateTagLabel(label: string): string {
|
|
204
|
+
const max = props.maxTagTextLength
|
|
205
|
+
if (!max || max < 1 || label.length <= max) return label
|
|
206
|
+
return `${label.slice(0, max)}…`
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const atMultipleLimit = computed(
|
|
210
|
+
() =>
|
|
211
|
+
Boolean(props.multiple) &&
|
|
212
|
+
props.multipleLimit != null &&
|
|
213
|
+
props.multipleLimit > 0 &&
|
|
214
|
+
selectedValues.value.length >= props.multipleLimit,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
function isOptionLimited(option: RsSelectOption): boolean {
|
|
218
|
+
if (!atMultipleLimit.value) return false
|
|
219
|
+
return !selectedValues.value.includes(toComboboxValue(option.value))
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function isTokenDisabled(token: string): boolean {
|
|
223
|
+
if (disabledMap.value.get(String(token))) return true
|
|
224
|
+
const option = optionFromToken(token)
|
|
225
|
+
return option ? isOptionLimited(option) : false
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const singleDisplayLabel = computed(() => {
|
|
229
|
+
if (props.multiple || !hasValue.value) return ''
|
|
230
|
+
return tokenLabel(selectedValues.value[0]!)
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
function writeTokens(tokens: string[]): void {
|
|
234
|
+
const values = restoreSelectValues(tokens, normalizedOptions.value)
|
|
235
|
+
model.value = packSelectModel(
|
|
236
|
+
values,
|
|
237
|
+
normalizedOptions.value,
|
|
238
|
+
Boolean(props.multiple),
|
|
239
|
+
Boolean(props.labelInValue),
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function emitSelectionDiff(prev: string[], next: string[]): void {
|
|
244
|
+
const prevSet = new Set(prev)
|
|
245
|
+
const nextSet = new Set(next)
|
|
246
|
+
for (const token of next) {
|
|
247
|
+
if (prevSet.has(token)) continue
|
|
248
|
+
const value = restoreSelectValue(token, normalizedOptions.value)
|
|
249
|
+
emit('select', value, findSelectOption(normalizedOptions.value, token))
|
|
250
|
+
}
|
|
251
|
+
for (const token of prev) {
|
|
252
|
+
if (nextSet.has(token)) continue
|
|
253
|
+
const value = restoreSelectValue(token, normalizedOptions.value)
|
|
254
|
+
emit('deselect', value, findSelectOption(normalizedOptions.value, token))
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const comboboxModel = computed<string | string[] | undefined>({
|
|
259
|
+
get() {
|
|
260
|
+
if (props.multiple) return selectedValues.value.map((token) => token)
|
|
261
|
+
return hasValue.value ? selectedValues.value[0] : undefined
|
|
262
|
+
},
|
|
263
|
+
set(value) {
|
|
264
|
+
const prev = selectedValues.value
|
|
265
|
+
const tokens = comboboxBindingToTokens(value, Boolean(props.multiple))
|
|
266
|
+
emitSelectionDiff(prev, tokens)
|
|
267
|
+
writeTokens(tokens)
|
|
268
|
+
if (props.autoClearSearchValue) searchQuery.value = ''
|
|
269
|
+
},
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
function currentValues(): RsSelectValue[] {
|
|
273
|
+
return restoreSelectValues(selectedValues.value, normalizedOptions.value)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function commitCreatedValue(value: string): void {
|
|
277
|
+
const next = value.trim()
|
|
278
|
+
if (!next) return
|
|
279
|
+
const values = currentValues()
|
|
280
|
+
if (values.some((item) => String(item) === next)) return
|
|
281
|
+
if (props.multiple) {
|
|
282
|
+
if (atMultipleLimit.value) return
|
|
283
|
+
const prev = selectedValues.value
|
|
284
|
+
const packed = packSelectModel(
|
|
285
|
+
[...values, next],
|
|
286
|
+
normalizedOptions.value,
|
|
287
|
+
true,
|
|
288
|
+
Boolean(props.labelInValue),
|
|
289
|
+
)
|
|
290
|
+
model.value = packed
|
|
291
|
+
emitSelectionDiff(prev, toSelectedTokens(packed, true))
|
|
292
|
+
} else {
|
|
293
|
+
const prev = selectedValues.value
|
|
294
|
+
model.value = packSelectModel(
|
|
295
|
+
[next],
|
|
296
|
+
normalizedOptions.value,
|
|
297
|
+
false,
|
|
298
|
+
Boolean(props.labelInValue),
|
|
299
|
+
)
|
|
300
|
+
emitSelectionDiff(prev, toSelectedTokens(model.value, false))
|
|
301
|
+
open.value = false
|
|
302
|
+
}
|
|
303
|
+
if (props.autoClearSearchValue) searchQuery.value = ''
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function consumeTokenSeparators(raw: string): void {
|
|
307
|
+
if (!props.multiple && !props.creatable) return
|
|
308
|
+
const parts = splitByTokenSeparators(raw, props.tokenSeparators ?? [])
|
|
309
|
+
if (!parts) return
|
|
310
|
+
const rest = parts.pop() ?? ''
|
|
311
|
+
for (const part of parts) {
|
|
312
|
+
const token = part.trim()
|
|
313
|
+
if (!token) continue
|
|
314
|
+
const matched = flatOptions.value.find(
|
|
315
|
+
(opt) => String(opt.value) === token || String(opt.label) === token,
|
|
316
|
+
)
|
|
317
|
+
if (matched) {
|
|
318
|
+
const prev = selectedValues.value
|
|
319
|
+
const key = toComboboxValue(matched.value)
|
|
320
|
+
if (prev.includes(key)) continue
|
|
321
|
+
if (props.multiple && atMultipleLimit.value) continue
|
|
322
|
+
const nextTokens = props.multiple ? [...prev, key] : [key]
|
|
323
|
+
emitSelectionDiff(prev, nextTokens)
|
|
324
|
+
writeTokens(nextTokens)
|
|
325
|
+
if (!props.multiple) open.value = false
|
|
326
|
+
} else if (props.creatable) {
|
|
327
|
+
commitCreatedValue(token)
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (rest !== raw) searchQuery.value = rest
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
watch(searchQuery, (query) => {
|
|
334
|
+
if (props.remote) emit('search', query)
|
|
335
|
+
if (props.tokenSeparators?.length) consumeTokenSeparators(query)
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
watch(open, async (isOpen) => {
|
|
339
|
+
if (isOpen) {
|
|
340
|
+
const pending = searchQuery.value
|
|
341
|
+
await nextTick()
|
|
342
|
+
await nextTick()
|
|
343
|
+
// ComboboxInput 挂载时会 resetSearchTerm;受控 searchValue 需要写回
|
|
344
|
+
if (pending) searchQuery.value = pending
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
if (props.autoClearSearchValue) searchQuery.value = ''
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
function onSearchKeydown(event: KeyboardEvent): void {
|
|
351
|
+
if (event.key !== 'Enter' || event.isComposing) return
|
|
352
|
+
if (!canCreate.value) return
|
|
353
|
+
event.preventDefault()
|
|
354
|
+
event.stopPropagation()
|
|
355
|
+
commitCreatedValue(createValue.value)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function onClear(event: MouseEvent): void {
|
|
359
|
+
event.preventDefault()
|
|
360
|
+
event.stopPropagation()
|
|
361
|
+
emitSelectionDiff(selectedValues.value, [])
|
|
362
|
+
emit('clear')
|
|
363
|
+
model.value = props.multiple ? [] : ''
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function removeTag(value: string, event: MouseEvent): void {
|
|
367
|
+
event.preventDefault()
|
|
368
|
+
event.stopPropagation()
|
|
369
|
+
if (!props.multiple) return
|
|
370
|
+
const prev = selectedValues.value
|
|
371
|
+
const tokens = prev.filter((item) => item !== value)
|
|
372
|
+
emitSelectionDiff(prev, tokens)
|
|
373
|
+
writeTokens(tokens)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function setValue(value: unknown): void {
|
|
377
|
+
if (props.multiple) {
|
|
378
|
+
const list = Array.isArray(value) ? value : value !== '' && value != null ? [value] : []
|
|
379
|
+
writeTokens(toSelectedTokens(list, true))
|
|
380
|
+
return
|
|
381
|
+
}
|
|
382
|
+
writeTokens(toSelectedTokens(value, false))
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function restoreTokenValue(token: string): RsSelectValue {
|
|
386
|
+
return restoreSelectValue(token, normalizedOptions.value)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function optionOrCreate(token: string): RsSelectOption {
|
|
390
|
+
return (
|
|
391
|
+
optionFromToken(token) ?? {
|
|
392
|
+
label: fromComboboxValue(token),
|
|
393
|
+
value: restoreTokenValue(token),
|
|
394
|
+
}
|
|
395
|
+
)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return {
|
|
399
|
+
normalizedOptions,
|
|
400
|
+
resolvedPlaceholder,
|
|
401
|
+
resolvedSearchPlaceholder,
|
|
402
|
+
resolvedEmptyText,
|
|
403
|
+
resolvedLoadingText,
|
|
404
|
+
isSearchable,
|
|
405
|
+
labelMap,
|
|
406
|
+
useVirtual,
|
|
407
|
+
useManualFilter,
|
|
408
|
+
displayOptions,
|
|
409
|
+
createValue,
|
|
410
|
+
canCreate,
|
|
411
|
+
createOptionLabel,
|
|
412
|
+
virtualValues,
|
|
413
|
+
selectedValues,
|
|
414
|
+
hasValue,
|
|
415
|
+
visibleTagTokens,
|
|
416
|
+
omittedTagCount,
|
|
417
|
+
omittedTagLabel,
|
|
418
|
+
omittedTagTitle,
|
|
419
|
+
singleDisplayLabel,
|
|
420
|
+
comboboxModel,
|
|
421
|
+
tokenLabel,
|
|
422
|
+
truncateTagLabel,
|
|
423
|
+
optionFromToken,
|
|
424
|
+
optionOrCreate,
|
|
425
|
+
isOptionLimited,
|
|
426
|
+
isTokenDisabled,
|
|
427
|
+
restoreTokenValue,
|
|
428
|
+
onSearchKeydown,
|
|
429
|
+
onClear,
|
|
430
|
+
removeTag,
|
|
431
|
+
setValue,
|
|
432
|
+
resetSearch: () => {
|
|
433
|
+
searchQuery.value = ''
|
|
434
|
+
},
|
|
435
|
+
}
|
|
436
|
+
}
|
|
@@ -11,7 +11,20 @@ export function resolveVirtualListHeight(height?: number | string, fallback = 24
|
|
|
11
11
|
return typeof height === 'number' ? `${height}px` : height
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
/** height 为 0
|
|
14
|
+
/** height 为 0 / 百分比时由父级撑满,组件内用 ResizeObserver 测量可视高度 */
|
|
15
15
|
export function isVirtualListFillHeight(height?: number | string): boolean {
|
|
16
|
-
|
|
16
|
+
if (height === 0) return true
|
|
17
|
+
if (typeof height === 'string' && height.trim().endsWith('%')) return true
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 解析虚拟列表数值视口高度;百分比 / 填满模式返回 null,交由测量值处理 */
|
|
22
|
+
export function parseVirtualListHeightPx(height?: number | string): number | null {
|
|
23
|
+
if (typeof height === 'number' && height > 0) return height
|
|
24
|
+
if (typeof height === 'string') {
|
|
25
|
+
if (height.includes('%')) return null
|
|
26
|
+
const parsed = Number.parseInt(height, 10)
|
|
27
|
+
if (Number.isFinite(parsed) && parsed > 0) return parsed
|
|
28
|
+
}
|
|
29
|
+
return null
|
|
17
30
|
}
|