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
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
expandSplitPane,
|
|
9
9
|
isSplitPaneCollapsed,
|
|
10
10
|
normalizeSplitSizes,
|
|
11
|
+
resolveSplitAutoFlags,
|
|
11
12
|
resolveSplitConstraints,
|
|
12
13
|
roundSize,
|
|
13
14
|
splitSizesEqual,
|
|
@@ -56,6 +57,9 @@ const sizes = ref<number[]>([])
|
|
|
56
57
|
const constraints = computed(() => resolveSplitConstraints(props.panes))
|
|
57
58
|
const resizerCount = computed(() => Math.max(0, props.panes.length - 1))
|
|
58
59
|
|
|
60
|
+
/** 内容自适应面板标记;首次拖拽/键盘调整时会物化为百分比并清空 */
|
|
61
|
+
const autoFlags = ref<boolean[]>(resolveSplitAutoFlags(props.panes))
|
|
62
|
+
|
|
59
63
|
/** 归一化初始值,作为双击复位的基准 */
|
|
60
64
|
let initialSizes: number[] = normalizeSplitSizes(props.panes, modelSeed())
|
|
61
65
|
sizes.value = initialSizes.slice()
|
|
@@ -72,15 +76,31 @@ watch(
|
|
|
72
76
|
() => paneKeySignature(),
|
|
73
77
|
() => {
|
|
74
78
|
const provided = sizes.value.length === props.panes.length ? sizes.value : undefined
|
|
79
|
+
autoFlags.value = resolveSplitAutoFlags(props.panes)
|
|
75
80
|
initialSizes = normalizeSplitSizes(props.panes, provided)
|
|
76
81
|
sizes.value = initialSizes.slice()
|
|
77
82
|
syncModel(sizes.value)
|
|
78
83
|
},
|
|
79
84
|
)
|
|
80
85
|
|
|
86
|
+
watch(
|
|
87
|
+
() => props.panes.map((pane) => pane.size).join('|'),
|
|
88
|
+
() => {
|
|
89
|
+
// panes 引用不变但 size 声明变化时同步 auto 标记与基准尺寸
|
|
90
|
+
autoFlags.value = resolveSplitAutoFlags(props.panes)
|
|
91
|
+
initialSizes = normalizeSplitSizes(props.panes, modelSeed())
|
|
92
|
+
if (!autoFlags.value.some(Boolean)) {
|
|
93
|
+
sizes.value = initialSizes.slice()
|
|
94
|
+
syncModel(sizes.value)
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
)
|
|
98
|
+
|
|
81
99
|
watch(model, (value) => {
|
|
82
100
|
if (!value || value.length !== props.panes.length) return
|
|
83
101
|
if (splitSizesEqual(value, sizes.value)) return
|
|
102
|
+
// 外部受控 sizes 一律视为百分比物化结果
|
|
103
|
+
autoFlags.value = props.panes.map(() => false)
|
|
84
104
|
sizes.value = normalizeSplitSizes(props.panes, value)
|
|
85
105
|
})
|
|
86
106
|
|
|
@@ -190,6 +210,10 @@ function finishDrag(index: number): void {
|
|
|
190
210
|
setDraggingUi(false, index)
|
|
191
211
|
const final = pendingDragSizes ?? sizes.value
|
|
192
212
|
pendingDragSizes = null
|
|
213
|
+
// 清掉拖拽期手写的 flex-grow,交回 Vue :style,避免残留内联样式干扰后续布局
|
|
214
|
+
paneElements().forEach((el) => {
|
|
215
|
+
el.style.removeProperty('flex-grow')
|
|
216
|
+
})
|
|
193
217
|
commit(final)
|
|
194
218
|
emitBoundaryTransitions(dragStartSizes, final)
|
|
195
219
|
emit('resize-end', final.slice())
|
|
@@ -213,6 +237,7 @@ function measureFlexPx(): number {
|
|
|
213
237
|
function onResizerPointerDown(index: number, event: PointerEvent): void {
|
|
214
238
|
if (props.disabled) return
|
|
215
239
|
if (event.pointerType === 'mouse' && event.button !== 0) return
|
|
240
|
+
materializeAutoPanes()
|
|
216
241
|
const target = event.currentTarget as HTMLElement
|
|
217
242
|
target.setPointerCapture?.(event.pointerId)
|
|
218
243
|
dragIndex = index
|
|
@@ -241,6 +266,7 @@ function endDrag(index: number, event: PointerEvent): void {
|
|
|
241
266
|
// —— 键盘无障碍 ——
|
|
242
267
|
function onResizerKeydown(index: number, event: KeyboardEvent): void {
|
|
243
268
|
if (props.disabled) return
|
|
269
|
+
materializeAutoPanes()
|
|
244
270
|
const horizontal = props.orientation === 'horizontal'
|
|
245
271
|
const step = props.keyboardStep
|
|
246
272
|
let delta = 0
|
|
@@ -281,6 +307,7 @@ function onResizerKeydown(index: number, event: KeyboardEvent): void {
|
|
|
281
307
|
|
|
282
308
|
function onResizerDblClick(index: number): void {
|
|
283
309
|
if (props.disabled) return
|
|
310
|
+
materializeAutoPanes()
|
|
284
311
|
const a = sizes.value[index]
|
|
285
312
|
const b = sizes.value[index + 1]
|
|
286
313
|
const initA = initialSizes[index]
|
|
@@ -324,12 +351,51 @@ function toggleCollapseAt(index: number): void {
|
|
|
324
351
|
emit('resize-end', sizes.value.slice())
|
|
325
352
|
}
|
|
326
353
|
|
|
354
|
+
/** 将 auto 面板按当前像素占比物化为百分比,之后走统一拖拽逻辑 */
|
|
355
|
+
function materializeAutoPanes(): void {
|
|
356
|
+
if (!autoFlags.value.some(Boolean)) return
|
|
357
|
+
const total = measureFlexPx()
|
|
358
|
+
if (total <= 0) return
|
|
359
|
+
const panes = paneElements()
|
|
360
|
+
const measured = panes.map((el) => (axisSize(el) / total) * 100)
|
|
361
|
+
const next = normalizeSplitSizes(
|
|
362
|
+
props.panes.map((pane) => ({
|
|
363
|
+
...pane,
|
|
364
|
+
size: typeof pane.size === 'number' ? pane.size : undefined,
|
|
365
|
+
})),
|
|
366
|
+
measured,
|
|
367
|
+
)
|
|
368
|
+
autoFlags.value = props.panes.map(() => false)
|
|
369
|
+
sizes.value = next
|
|
370
|
+
syncModel(next)
|
|
371
|
+
}
|
|
372
|
+
|
|
327
373
|
function paneStyle(index: number): Record<string, string> {
|
|
374
|
+
if (autoFlags.value[index]) {
|
|
375
|
+
const pane = props.panes[index]
|
|
376
|
+
const vertical = props.orientation === 'vertical'
|
|
377
|
+
// auto 面板必须按内容撑开;不能继承基类 min-height/min-width:0,否则在
|
|
378
|
+
// 「另一侧 flex-grow 吃满」的分栏里会被算成 0 高/宽(KeepAlive 切页后常见)。
|
|
379
|
+
const style: Record<string, string> = {
|
|
380
|
+
flexGrow: '0',
|
|
381
|
+
flexShrink: '0',
|
|
382
|
+
flexBasis: 'auto',
|
|
383
|
+
[vertical ? 'minHeight' : 'minWidth']: 'min-content',
|
|
384
|
+
}
|
|
385
|
+
if (typeof pane?.min === 'number' && pane.min > 0) {
|
|
386
|
+
style[vertical ? 'minHeight' : 'minWidth'] = `${pane.min}%`
|
|
387
|
+
}
|
|
388
|
+
if (typeof pane?.max === 'number' && pane.max < 100) {
|
|
389
|
+
style[vertical ? 'maxHeight' : 'maxWidth'] = `${pane.max}%`
|
|
390
|
+
}
|
|
391
|
+
return style
|
|
392
|
+
}
|
|
328
393
|
const size = sizes.value[index] ?? 0
|
|
329
394
|
return { flexGrow: String(size), flexShrink: '1', flexBasis: '0%' }
|
|
330
395
|
}
|
|
331
396
|
|
|
332
397
|
function paneCollapsed(index: number): boolean {
|
|
398
|
+
if (autoFlags.value[index]) return false
|
|
333
399
|
return isSplitPaneCollapsed(sizes.value[index] ?? 0, constraints.value[index])
|
|
334
400
|
}
|
|
335
401
|
|
|
@@ -368,6 +434,7 @@ function expand(key: string, toSize?: number): void {
|
|
|
368
434
|
}
|
|
369
435
|
|
|
370
436
|
function reset(): void {
|
|
437
|
+
autoFlags.value = resolveSplitAutoFlags(props.panes)
|
|
371
438
|
initialSizes = normalizeSplitSizes(props.panes)
|
|
372
439
|
commit(initialSizes.slice())
|
|
373
440
|
emit('resize-end', sizes.value.slice())
|
|
@@ -388,7 +455,10 @@ defineExpose({ collapse, expand, reset, getSizes: () => sizes.value.slice() })
|
|
|
388
455
|
<template v-for="(pane, index) in panes" :key="pane.key">
|
|
389
456
|
<div
|
|
390
457
|
class="rs-split__pane"
|
|
391
|
-
:class="{
|
|
458
|
+
:class="{
|
|
459
|
+
'rs-split__pane--collapsed': paneCollapsed(index),
|
|
460
|
+
'rs-split__pane--auto': autoFlags[index],
|
|
461
|
+
}"
|
|
392
462
|
:style="paneStyle(index)"
|
|
393
463
|
:data-pane-key="pane.key"
|
|
394
464
|
>
|
|
@@ -457,6 +527,25 @@ defineExpose({ collapse, expand, reset, getSizes: () => sizes.value.slice() })
|
|
|
457
527
|
overflow: hidden;
|
|
458
528
|
}
|
|
459
529
|
|
|
530
|
+
.rs-split__pane--auto {
|
|
531
|
+
/* 内容撑开;超出 max 时在面板内滚动 */
|
|
532
|
+
overflow: auto;
|
|
533
|
+
flex: 0 0 auto;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/* 覆盖基类 min-*:0,避免 auto 面板被弹性侧压成 0 */
|
|
537
|
+
.rs-split--vertical > .rs-split__pane--auto {
|
|
538
|
+
min-height: min-content;
|
|
539
|
+
height: auto;
|
|
540
|
+
max-height: none;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
.rs-split--horizontal > .rs-split__pane--auto {
|
|
544
|
+
min-width: min-content;
|
|
545
|
+
width: auto;
|
|
546
|
+
max-width: none;
|
|
547
|
+
}
|
|
548
|
+
|
|
460
549
|
.rs-split__pane--collapsed {
|
|
461
550
|
overflow: hidden;
|
|
462
551
|
}
|
|
@@ -66,8 +66,8 @@ withDefaults(
|
|
|
66
66
|
.rs-stat-card__value {
|
|
67
67
|
margin-top: var(--rs-space-sm);
|
|
68
68
|
color: var(--rs-text);
|
|
69
|
-
font-size:
|
|
70
|
-
font-weight:
|
|
69
|
+
font-size: var(--rs-font-size-3xl);
|
|
70
|
+
font-weight: var(--rs-font-weight-bold);
|
|
71
71
|
line-height: 1.1;
|
|
72
72
|
}
|
|
73
73
|
.rs-stat-card__description {
|
|
@@ -97,7 +97,7 @@ function selectStep(item: RsStepItem): void {
|
|
|
97
97
|
background: var(--rs-surface-hover);
|
|
98
98
|
color: var(--rs-muted);
|
|
99
99
|
font-size: var(--rs-font-size-xs);
|
|
100
|
-
font-weight:
|
|
100
|
+
font-weight: var(--rs-font-weight-bold);
|
|
101
101
|
}
|
|
102
102
|
.rs-steps--sm .rs-steps__indicator {
|
|
103
103
|
width: 1.5rem;
|
|
@@ -119,7 +119,7 @@ function selectStep(item: RsStepItem): void {
|
|
|
119
119
|
.rs-steps__title {
|
|
120
120
|
color: var(--rs-text);
|
|
121
121
|
font-size: var(--rs-font-size-sm);
|
|
122
|
-
font-weight:
|
|
122
|
+
font-weight: var(--rs-font-weight-semibold);
|
|
123
123
|
}
|
|
124
124
|
.rs-steps__description {
|
|
125
125
|
color: var(--rs-muted);
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, useId } from 'vue'
|
|
3
|
+
import type { RsComponentSize } from '../theme/types'
|
|
4
|
+
import { SwitchRoot, SwitchThumb } from './reka'
|
|
5
|
+
import { useResolvedRsComponentSize } from './resolve-size'
|
|
6
|
+
|
|
7
|
+
/** Switch v-model 取值:默认 boolean,也可为业务码如 'Y'/'N'、1/0 */
|
|
8
|
+
export type RsSwitchValue = boolean | string | number
|
|
9
|
+
|
|
10
|
+
const model = defineModel<RsSwitchValue>({ default: false })
|
|
11
|
+
|
|
12
|
+
const props = withDefaults(
|
|
13
|
+
defineProps<{
|
|
14
|
+
disabled?: boolean
|
|
15
|
+
size?: RsComponentSize
|
|
16
|
+
/** 无障碍名称;有默认插槽文案时可省略 */
|
|
17
|
+
ariaLabel?: string
|
|
18
|
+
id?: string
|
|
19
|
+
/** 打开时写入 v-model 的值,默认 true */
|
|
20
|
+
checkedValue?: RsSwitchValue
|
|
21
|
+
/** 关闭时写入 v-model 的值,默认 false */
|
|
22
|
+
uncheckedValue?: RsSwitchValue
|
|
23
|
+
}>(),
|
|
24
|
+
{
|
|
25
|
+
disabled: false,
|
|
26
|
+
checkedValue: true,
|
|
27
|
+
uncheckedValue: false,
|
|
28
|
+
},
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
const emit = defineEmits<{
|
|
32
|
+
change: [value: RsSwitchValue]
|
|
33
|
+
}>()
|
|
34
|
+
|
|
35
|
+
const autoId = useId()
|
|
36
|
+
const inputId = computed(() => props.id || autoId)
|
|
37
|
+
const resolvedSize = useResolvedRsComponentSize(() => props.size)
|
|
38
|
+
/** Reka Switch 只吃 boolean;对外 v-model 按 checkedValue / uncheckedValue 映射 */
|
|
39
|
+
const isChecked = computed(() => Object.is(model.value, props.checkedValue))
|
|
40
|
+
|
|
41
|
+
const rootClass = computed(() => [
|
|
42
|
+
'rs-switch',
|
|
43
|
+
`rs-switch--${resolvedSize.value}`,
|
|
44
|
+
{
|
|
45
|
+
'rs-switch--checked': isChecked.value,
|
|
46
|
+
'rs-switch--disabled': props.disabled,
|
|
47
|
+
},
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
function onUpdate(checked: boolean): void {
|
|
51
|
+
const next = checked ? props.checkedValue : props.uncheckedValue
|
|
52
|
+
if (Object.is(model.value, next)) return
|
|
53
|
+
model.value = next
|
|
54
|
+
emit('change', next)
|
|
55
|
+
}
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
<template>
|
|
59
|
+
<div :class="rootClass">
|
|
60
|
+
<SwitchRoot
|
|
61
|
+
:id="inputId"
|
|
62
|
+
class="rs-switch__root"
|
|
63
|
+
:model-value="isChecked"
|
|
64
|
+
:disabled="disabled"
|
|
65
|
+
:aria-label="ariaLabel"
|
|
66
|
+
@update:model-value="onUpdate"
|
|
67
|
+
>
|
|
68
|
+
<SwitchThumb class="rs-switch__thumb" />
|
|
69
|
+
</SwitchRoot>
|
|
70
|
+
<label v-if="$slots.default" class="rs-switch__label" :for="inputId">
|
|
71
|
+
<slot />
|
|
72
|
+
</label>
|
|
73
|
+
</div>
|
|
74
|
+
</template>
|
|
75
|
+
|
|
76
|
+
<style>
|
|
77
|
+
/**
|
|
78
|
+
* 轨道高度 = control-height * 2/3(对齐 Ant:Input SM 24 / Switch SM 16)。
|
|
79
|
+
* 宽高、内边距、滑块均由 token / calc 推导,不写死档位 px。
|
|
80
|
+
* 外层 min-height = control-height,保证与同排输入垂直居中对齐。
|
|
81
|
+
*/
|
|
82
|
+
.rs-switch {
|
|
83
|
+
--rs-switch-pad: calc(var(--rs-switch-track-h) * 0.12);
|
|
84
|
+
--rs-switch-track-w: calc(var(--rs-switch-track-h) * 1.75);
|
|
85
|
+
--rs-switch-thumb: calc(var(--rs-switch-track-h) - var(--rs-switch-pad) * 2 - 2px);
|
|
86
|
+
display: inline-flex;
|
|
87
|
+
align-items: center;
|
|
88
|
+
gap: var(--rs-space-xs);
|
|
89
|
+
vertical-align: middle;
|
|
90
|
+
line-height: 1;
|
|
91
|
+
cursor: pointer;
|
|
92
|
+
user-select: none;
|
|
93
|
+
color: var(--rs-text);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.rs-switch--disabled {
|
|
97
|
+
cursor: not-allowed;
|
|
98
|
+
opacity: 0.55;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
.rs-switch__root {
|
|
102
|
+
position: relative;
|
|
103
|
+
display: inline-flex;
|
|
104
|
+
flex-shrink: 0;
|
|
105
|
+
align-items: center;
|
|
106
|
+
box-sizing: border-box;
|
|
107
|
+
width: var(--rs-switch-track-w);
|
|
108
|
+
height: var(--rs-switch-track-h);
|
|
109
|
+
padding: var(--rs-switch-pad);
|
|
110
|
+
border: 1px solid var(--rs-border);
|
|
111
|
+
border-radius: var(--rs-radius-full);
|
|
112
|
+
background: var(--rs-surface-hover);
|
|
113
|
+
outline: none;
|
|
114
|
+
cursor: pointer;
|
|
115
|
+
transition:
|
|
116
|
+
background-color var(--rs-transition-fast),
|
|
117
|
+
border-color var(--rs-transition-fast);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.rs-switch--disabled .rs-switch__root {
|
|
121
|
+
cursor: not-allowed;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.rs-switch:hover:not(.rs-switch--disabled):not(.rs-switch--checked) .rs-switch__root {
|
|
125
|
+
border-color: var(--rs-primary);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.rs-switch__root:focus-visible {
|
|
129
|
+
box-shadow: 0 0 0 var(--rs-focus-ring-width, 2px) var(--rs-focus-ring);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
.rs-switch--checked .rs-switch__root {
|
|
133
|
+
background: var(--rs-primary);
|
|
134
|
+
border-color: var(--rs-primary);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
.rs-switch--ssm {
|
|
138
|
+
--rs-switch-track-h: calc(var(--rs-control-height-ssm) * 2 / 3);
|
|
139
|
+
min-height: var(--rs-control-height-ssm);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
.rs-switch--sm {
|
|
143
|
+
--rs-switch-track-h: calc(var(--rs-control-height-sm) * 2 / 3);
|
|
144
|
+
min-height: var(--rs-control-height-sm);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.rs-switch--md {
|
|
148
|
+
--rs-switch-track-h: calc(var(--rs-control-height-md) * 2 / 3);
|
|
149
|
+
min-height: var(--rs-control-height-md);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
.rs-switch--lg {
|
|
153
|
+
--rs-switch-track-h: calc(var(--rs-control-height-lg) * 2 / 3);
|
|
154
|
+
min-height: var(--rs-control-height-lg);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.rs-switch__thumb {
|
|
158
|
+
position: absolute;
|
|
159
|
+
top: 50%;
|
|
160
|
+
left: var(--rs-switch-pad);
|
|
161
|
+
display: block;
|
|
162
|
+
width: var(--rs-switch-thumb);
|
|
163
|
+
height: var(--rs-switch-thumb);
|
|
164
|
+
border-radius: var(--rs-radius-full);
|
|
165
|
+
background: var(--rs-surface);
|
|
166
|
+
box-shadow: var(--rs-shadow-sm);
|
|
167
|
+
transform: translateY(-50%);
|
|
168
|
+
transition:
|
|
169
|
+
left var(--rs-transition-fast),
|
|
170
|
+
right var(--rs-transition-fast);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
.rs-switch--checked .rs-switch__thumb {
|
|
174
|
+
left: auto;
|
|
175
|
+
right: var(--rs-switch-pad);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
.rs-switch__label {
|
|
179
|
+
font-size: var(--rs-font-size-sm);
|
|
180
|
+
line-height: var(--rs-line-height-normal);
|
|
181
|
+
cursor: pointer;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.rs-switch--disabled .rs-switch__label {
|
|
185
|
+
cursor: not-allowed;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
.rs-switch--ssm .rs-switch__label,
|
|
189
|
+
.rs-switch--sm .rs-switch__label {
|
|
190
|
+
font-size: var(--rs-font-size-xs);
|
|
191
|
+
}
|
|
192
|
+
</style>
|