@geektr.cloud/acrux-ui 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +21 -2
- package/package.json +70 -3
- package/src/components/acrux-ui/actions/ConfirmPopover.vue +107 -0
- package/src/components/acrux-ui/actions/FormShell.vue +96 -0
- package/src/components/acrux-ui/actions/RemovalButton.vue +65 -0
- package/src/components/acrux-ui/actions/Stepper.vue +116 -0
- package/src/components/acrux-ui/actions/form.ts +36 -0
- package/src/components/acrux-ui/actions/index.ts +70 -0
- package/src/components/acrux-ui/actions/stepper.ts +65 -0
- package/src/components/acrux-ui/actions/utils.ts +138 -0
- package/src/components/acrux-ui/base/CopyBtn.vue +85 -0
- package/src/components/acrux-ui/base/EmptyValue.vue +25 -0
- package/src/components/acrux-ui/base/Icon.vue +44 -0
- package/src/components/acrux-ui/base/IconBtn.vue +51 -0
- package/src/components/acrux-ui/base/Text.vue +85 -0
- package/src/components/acrux-ui/base/VSeparator.vue +21 -0
- package/src/components/acrux-ui/base/async.ts +35 -0
- package/src/components/acrux-ui/base/index.ts +7 -0
- package/src/components/acrux-ui/display/AsyncError.vue +46 -0
- package/src/components/acrux-ui/display/CodeBlock.vue +91 -0
- package/src/components/acrux-ui/display/CopyTag.vue +51 -0
- package/src/components/acrux-ui/display/DataView.ts +47 -0
- package/src/components/acrux-ui/display/DateFormatter.vue +61 -0
- package/src/components/acrux-ui/display/Disclosure.vue +45 -0
- package/src/components/acrux-ui/display/Link.vue +81 -0
- package/src/components/acrux-ui/display/MultiLine.vue +29 -0
- package/src/components/acrux-ui/display/Notice.vue +71 -0
- package/src/components/acrux-ui/display/Pem.vue +165 -0
- package/src/components/acrux-ui/display/QrBtn.vue +117 -0
- package/src/components/acrux-ui/display/QrModal.vue +76 -0
- package/src/components/acrux-ui/display/Route.vue +52 -0
- package/src/components/acrux-ui/display/Token.vue +106 -0
- package/src/components/acrux-ui/display/UUID.vue +46 -0
- package/src/components/acrux-ui/display/index.ts +17 -0
- package/src/components/acrux-ui/display/pem.ts +100 -0
- package/src/components/acrux-ui/display/qr.ts +20 -0
- package/src/components/acrux-ui/fields/EntitySelect.vue +253 -0
- package/src/components/acrux-ui/fields/FieldError.vue +11 -0
- package/src/components/acrux-ui/fields/JsonTextArea.vue +105 -0
- package/src/components/acrux-ui/fields/MultiLineInput.vue +145 -0
- package/src/components/acrux-ui/fields/Select.vue +106 -0
- package/src/components/acrux-ui/fields/fieldAccessibility.ts +38 -0
- package/src/components/acrux-ui/fields/index.ts +4 -0
- package/src/components/acrux-ui/page/BackButton.vue +70 -0
- package/src/components/acrux-ui/page/PageDetail.vue +125 -0
- package/src/components/acrux-ui/page/PageEntry.vue +130 -0
- package/src/components/acrux-ui/page/PageState.vue +108 -0
- package/src/components/acrux-ui/page/index.ts +3 -0
- package/src/components/acrux-ui/page/pageState.ts +18 -0
- package/src/components/ui/alert/Alert.vue +17 -0
- package/src/components/ui/alert/AlertAction.vue +17 -0
- package/src/components/ui/alert/AlertDescription.vue +22 -0
- package/src/components/ui/alert/AlertTitle.vue +22 -0
- package/src/components/ui/alert/index.ts +25 -0
- package/src/components/ui/badge/Badge.vue +29 -0
- package/src/components/ui/badge/index.ts +25 -0
- package/src/components/ui/button/Button.vue +31 -0
- package/src/components/ui/button/index.ts +39 -0
- package/src/components/ui/card/Card.vue +29 -0
- package/src/components/ui/card/CardAction.vue +17 -0
- package/src/components/ui/card/CardContent.vue +14 -0
- package/src/components/ui/card/CardDescription.vue +14 -0
- package/src/components/ui/card/CardFooter.vue +17 -0
- package/src/components/ui/card/CardHeader.vue +22 -0
- package/src/components/ui/card/CardTitle.vue +14 -0
- package/src/components/ui/card/index.ts +7 -0
- package/src/components/ui/collapsible/Collapsible.vue +15 -0
- package/src/components/ui/collapsible/CollapsibleContent.vue +12 -0
- package/src/components/ui/collapsible/CollapsibleTrigger.vue +12 -0
- package/src/components/ui/collapsible/index.ts +3 -0
- package/src/components/ui/combobox/Combobox.vue +15 -0
- package/src/components/ui/combobox/ComboboxAnchor.vue +19 -0
- package/src/components/ui/combobox/ComboboxEmpty.vue +26 -0
- package/src/components/ui/combobox/ComboboxInput.vue +40 -0
- package/src/components/ui/combobox/ComboboxItem.vue +29 -0
- package/src/components/ui/combobox/ComboboxList.vue +38 -0
- package/src/components/ui/combobox/ComboboxViewport.vue +28 -0
- package/src/components/ui/combobox/index.ts +9 -0
- package/src/components/ui/empty/Empty.vue +22 -0
- package/src/components/ui/empty/EmptyContent.vue +17 -0
- package/src/components/ui/empty/EmptyDescription.vue +22 -0
- package/src/components/ui/empty/EmptyHeader.vue +14 -0
- package/src/components/ui/empty/EmptyMedia.vue +22 -0
- package/src/components/ui/empty/EmptyTitle.vue +14 -0
- package/src/components/ui/empty/index.ts +26 -0
- package/src/components/ui/field/Field.vue +27 -0
- package/src/components/ui/field/FieldContent.vue +17 -0
- package/src/components/ui/field/FieldDescription.vue +24 -0
- package/src/components/ui/field/FieldError.vue +50 -0
- package/src/components/ui/field/FieldGroup.vue +22 -0
- package/src/components/ui/field/FieldLabel.vue +24 -0
- package/src/components/ui/field/FieldLegend.vue +24 -0
- package/src/components/ui/field/FieldSeparator.vue +26 -0
- package/src/components/ui/field/FieldSet.vue +22 -0
- package/src/components/ui/field/FieldTitle.vue +22 -0
- package/src/components/ui/field/index.ts +30 -0
- package/src/components/ui/input/Input.vue +33 -0
- package/src/components/ui/input/index.ts +1 -0
- package/src/components/ui/input-group/InputGroup.vue +23 -0
- package/src/components/ui/input-group/InputGroupAddon.vue +39 -0
- package/src/components/ui/input-group/InputGroupButton.vue +22 -0
- package/src/components/ui/input-group/InputGroupInput.vue +21 -0
- package/src/components/ui/input-group/InputGroupText.vue +21 -0
- package/src/components/ui/input-group/InputGroupTextarea.vue +21 -0
- package/src/components/ui/input-group/index.ts +53 -0
- package/src/components/ui/label/Label.vue +26 -0
- package/src/components/ui/label/index.ts +1 -0
- package/src/components/ui/popover/Popover.vue +15 -0
- package/src/components/ui/popover/PopoverAnchor.vue +12 -0
- package/src/components/ui/popover/PopoverContent.vue +38 -0
- package/src/components/ui/popover/PopoverDescription.vue +14 -0
- package/src/components/ui/popover/PopoverHeader.vue +14 -0
- package/src/components/ui/popover/PopoverTitle.vue +14 -0
- package/src/components/ui/popover/PopoverTrigger.vue +12 -0
- package/src/components/ui/popover/index.ts +7 -0
- package/src/components/ui/select/Select.vue +15 -0
- package/src/components/ui/select/SelectContent.vue +53 -0
- package/src/components/ui/select/SelectGroup.vue +17 -0
- package/src/components/ui/select/SelectItem.vue +40 -0
- package/src/components/ui/select/SelectItemText.vue +12 -0
- package/src/components/ui/select/SelectLabel.vue +14 -0
- package/src/components/ui/select/SelectScrollDownButton.vue +32 -0
- package/src/components/ui/select/SelectScrollUpButton.vue +32 -0
- package/src/components/ui/select/SelectSeparator.vue +19 -0
- package/src/components/ui/select/SelectTrigger.vue +36 -0
- package/src/components/ui/select/SelectValue.vue +12 -0
- package/src/components/ui/select/index.ts +11 -0
- package/src/components/ui/separator/Separator.vue +27 -0
- package/src/components/ui/separator/index.ts +1 -0
- package/src/components/ui/skeleton/Skeleton.vue +14 -0
- package/src/components/ui/skeleton/index.ts +1 -0
- package/src/components/ui/spinner/Spinner.vue +14 -0
- package/src/components/ui/spinner/index.ts +1 -0
- package/src/components/ui/switch/Switch.vue +47 -0
- package/src/components/ui/switch/index.ts +1 -0
- package/src/components/ui/table/Table.vue +24 -0
- package/src/components/ui/table/TableBody.vue +14 -0
- package/src/components/ui/table/TableCaption.vue +14 -0
- package/src/components/ui/table/TableCell.vue +17 -0
- package/src/components/ui/table/TableEmpty.vue +32 -0
- package/src/components/ui/table/TableFooter.vue +14 -0
- package/src/components/ui/table/TableHead.vue +29 -0
- package/src/components/ui/table/TableHeader.vue +14 -0
- package/src/components/ui/table/TableRow.vue +22 -0
- package/src/components/ui/table/index.ts +9 -0
- package/src/components/ui/tags-input/TagsInput.vue +31 -0
- package/src/components/ui/tags-input/TagsInputInput.vue +20 -0
- package/src/components/ui/tags-input/TagsInputItem.vue +28 -0
- package/src/components/ui/tags-input/TagsInputItemDelete.vue +23 -0
- package/src/components/ui/tags-input/TagsInputItemText.vue +17 -0
- package/src/components/ui/tags-input/index.ts +5 -0
- package/src/components/ui/textarea/Textarea.vue +33 -0
- package/src/components/ui/textarea/index.ts +1 -0
- package/src/index.ts +5 -0
- package/src/testing/renderer.ts +99 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, ref, useAttrs, useId, watch, type HTMLAttributes } from "vue";
|
|
3
|
+
import { Textarea } from "../../ui/textarea";
|
|
4
|
+
import { cn } from "cn";
|
|
5
|
+
import FieldError from "./FieldError.vue";
|
|
6
|
+
import { useFieldAccessibility } from "./fieldAccessibility";
|
|
7
|
+
|
|
8
|
+
defineOptions({ name: "JsonTextArea", inheritAttrs: false });
|
|
9
|
+
|
|
10
|
+
const props = withDefaults(
|
|
11
|
+
defineProps<{
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
class?: HTMLAttributes["class"];
|
|
14
|
+
id?: string;
|
|
15
|
+
invalidText?: string;
|
|
16
|
+
}>(),
|
|
17
|
+
{
|
|
18
|
+
disabled: false,
|
|
19
|
+
class: undefined,
|
|
20
|
+
id: undefined,
|
|
21
|
+
invalidText: "JSON 格式无效",
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const model = defineModel<unknown>();
|
|
26
|
+
const emit = defineEmits<{ "update:invalid": [invalid: boolean] }>();
|
|
27
|
+
|
|
28
|
+
// The textarea is the control root: native attrs (aria-*, data-*, listeners) land there, class merges on the wrapper.
|
|
29
|
+
// Error descriptions use the same conditional id contract as the composite fields.
|
|
30
|
+
const attrs = useAttrs();
|
|
31
|
+
const generatedId = useId();
|
|
32
|
+
const fieldId = computed(() => props.id ?? `${generatedId}-json`);
|
|
33
|
+
const invalid = ref(false);
|
|
34
|
+
const { errorId, describedBy } = useFieldAccessibility(
|
|
35
|
+
() => invalid.value,
|
|
36
|
+
() => `${fieldId.value}-error`,
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
type AriaInvalid = boolean | "true" | "false" | "grammar" | "spelling";
|
|
40
|
+
|
|
41
|
+
const externalInvalid = computed<AriaInvalid | undefined>(() => {
|
|
42
|
+
const value = attrs["aria-invalid"];
|
|
43
|
+
if (
|
|
44
|
+
value === true ||
|
|
45
|
+
value === false ||
|
|
46
|
+
value === "true" ||
|
|
47
|
+
value === "false" ||
|
|
48
|
+
value === "grammar" ||
|
|
49
|
+
value === "spelling"
|
|
50
|
+
) {
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const invalidState = computed<AriaInvalid | undefined>(() => (invalid.value ? true : externalInvalid.value));
|
|
57
|
+
|
|
58
|
+
const stringify = (value: unknown) => JSON.stringify(value ?? null, null, 2) ?? "null";
|
|
59
|
+
const text = ref(stringify(model.value));
|
|
60
|
+
|
|
61
|
+
watch(model, (value) => {
|
|
62
|
+
// Skip if our current text already represents this value (user is mid-edit).
|
|
63
|
+
try {
|
|
64
|
+
if (JSON.stringify(JSON.parse(text.value)) === JSON.stringify(value)) return;
|
|
65
|
+
} catch {
|
|
66
|
+
// Text is currently invalid — always sync.
|
|
67
|
+
}
|
|
68
|
+
text.value = stringify(value);
|
|
69
|
+
invalid.value = false;
|
|
70
|
+
emit("update:invalid", false);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const onInput = (event: Event) => {
|
|
74
|
+
const raw = (event.target as HTMLTextAreaElement).value;
|
|
75
|
+
text.value = raw;
|
|
76
|
+
try {
|
|
77
|
+
model.value = JSON.parse(raw) as unknown;
|
|
78
|
+
invalid.value = false;
|
|
79
|
+
emit("update:invalid", false);
|
|
80
|
+
} catch {
|
|
81
|
+
// Keep the last valid value upstream; only flag the draft as unparseable.
|
|
82
|
+
invalid.value = true;
|
|
83
|
+
emit("update:invalid", true);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
</script>
|
|
87
|
+
|
|
88
|
+
<template>
|
|
89
|
+
<div data-slot="json-textarea" :class="cn('flex min-w-0 flex-col gap-1', props.class)">
|
|
90
|
+
<Textarea
|
|
91
|
+
:id="fieldId"
|
|
92
|
+
v-bind="attrs"
|
|
93
|
+
:model-value="text"
|
|
94
|
+
:disabled="disabled"
|
|
95
|
+
:aria-invalid="invalidState"
|
|
96
|
+
:aria-describedby="describedBy"
|
|
97
|
+
class="font-mono text-xs"
|
|
98
|
+
spellcheck="false"
|
|
99
|
+
@input="onInput"
|
|
100
|
+
/>
|
|
101
|
+
<FieldError v-if="invalid" :id="errorId" data-slot="json-textarea-error">
|
|
102
|
+
<slot name="error">{{ invalidText }}</slot>
|
|
103
|
+
</FieldError>
|
|
104
|
+
</div>
|
|
105
|
+
</template>
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { nextTick, ref, useId, watch, type HTMLAttributes } from "vue";
|
|
3
|
+
import { Plus, X } from "@lucide/vue";
|
|
4
|
+
import { Input } from "../../ui/input";
|
|
5
|
+
import { Button } from "../../ui/button";
|
|
6
|
+
import { IconBtn } from "../base";
|
|
7
|
+
import { cn } from "cn";
|
|
8
|
+
|
|
9
|
+
defineOptions({ name: "MultiLineInput" });
|
|
10
|
+
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
placeholder?: string;
|
|
14
|
+
addText?: string;
|
|
15
|
+
rowLabel?: (index: number) => string;
|
|
16
|
+
removeLabel?: (index: number) => string;
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
class?: HTMLAttributes["class"];
|
|
19
|
+
}>(),
|
|
20
|
+
{
|
|
21
|
+
placeholder: "",
|
|
22
|
+
addText: "添加",
|
|
23
|
+
rowLabel: (index: number) => `第 ${index + 1} 行`,
|
|
24
|
+
removeLabel: (index: number) => `删除第 ${index + 1} 行`,
|
|
25
|
+
disabled: false,
|
|
26
|
+
class: undefined,
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const model = defineModel<string[]>({ default: () => [] });
|
|
31
|
+
|
|
32
|
+
type Row = { id: string; value: string };
|
|
33
|
+
|
|
34
|
+
const uid = useId();
|
|
35
|
+
let sequence = 0;
|
|
36
|
+
const nextId = () => `${uid}-row-${++sequence}`;
|
|
37
|
+
const rows = ref<Row[]>([]);
|
|
38
|
+
const rowRefs = new Map<string, HTMLDivElement>();
|
|
39
|
+
|
|
40
|
+
const setRowRef = (id: string, el: unknown) => {
|
|
41
|
+
const node = el as HTMLDivElement | null;
|
|
42
|
+
if (node) rowRefs.set(id, node);
|
|
43
|
+
else rowRefs.delete(id);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
watch(
|
|
47
|
+
model,
|
|
48
|
+
(values) => {
|
|
49
|
+
if (values.length === rows.value.length && values.every((value, index) => value === rows.value[index]?.value)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
rows.value = values.map((value, index) => ({ id: rows.value[index]?.id ?? nextId(), value }));
|
|
53
|
+
},
|
|
54
|
+
{ immediate: true },
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const commit = (next: Row[]) => {
|
|
58
|
+
rows.value = next;
|
|
59
|
+
model.value = next.map((row) => row.value);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const addLine = () => {
|
|
63
|
+
commit([...rows.value, { id: nextId(), value: "" }]);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const removeLine = (index: number) => {
|
|
67
|
+
commit(rows.value.filter((_, rowIndex) => rowIndex !== index));
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const setLine = (index: number, value: string | number) => {
|
|
71
|
+
commit(rows.value.map((row, rowIndex) => (rowIndex === index ? { ...row, value: String(value) } : row)));
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const focusInput = async (id: string, moveCaretToEnd = false) => {
|
|
75
|
+
await nextTick();
|
|
76
|
+
const input = rowRefs.get(id)?.querySelector("input");
|
|
77
|
+
if (!input) return;
|
|
78
|
+
input.focus();
|
|
79
|
+
if (moveCaretToEnd) {
|
|
80
|
+
const position = input.value.length;
|
|
81
|
+
input.setSelectionRange(position, position);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const insertAfter = (index: number) => {
|
|
86
|
+
const row: Row = { id: nextId(), value: "" };
|
|
87
|
+
commit([...rows.value.slice(0, index + 1), row, ...rows.value.slice(index + 1)]);
|
|
88
|
+
void focusInput(row.id);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const onEnter = (index: number, event: KeyboardEvent) => {
|
|
92
|
+
if (event.isComposing) return;
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
insertAfter(index);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const onBackspace = (index: number, event: KeyboardEvent) => {
|
|
98
|
+
if (event.isComposing) return;
|
|
99
|
+
const target = event.target as HTMLInputElement | null;
|
|
100
|
+
if (!target || target.value.length > 0 || index <= 0) return;
|
|
101
|
+
event.preventDefault();
|
|
102
|
+
const previous = rows.value[index - 1];
|
|
103
|
+
commit(rows.value.filter((_, rowIndex) => rowIndex !== index));
|
|
104
|
+
if (previous) void focusInput(previous.id, true);
|
|
105
|
+
};
|
|
106
|
+
</script>
|
|
107
|
+
|
|
108
|
+
<template>
|
|
109
|
+
<div data-slot="multi-line-input" :class="cn('flex flex-col gap-2', props.class)">
|
|
110
|
+
<div
|
|
111
|
+
v-for="(row, index) in rows"
|
|
112
|
+
:key="row.id"
|
|
113
|
+
:ref="(el) => setRowRef(row.id, el)"
|
|
114
|
+
data-slot="multi-line-input-row"
|
|
115
|
+
class="flex min-w-0 items-center gap-2"
|
|
116
|
+
>
|
|
117
|
+
<Input
|
|
118
|
+
:id="row.id"
|
|
119
|
+
:model-value="row.value"
|
|
120
|
+
:placeholder="placeholder"
|
|
121
|
+
:disabled="disabled"
|
|
122
|
+
:aria-label="props.rowLabel(index)"
|
|
123
|
+
class="min-w-0 flex-1"
|
|
124
|
+
@update:model-value="(value) => setLine(index, value)"
|
|
125
|
+
@keydown.enter="onEnter(index, $event)"
|
|
126
|
+
@keydown.backspace="onBackspace(index, $event)"
|
|
127
|
+
/>
|
|
128
|
+
<IconBtn
|
|
129
|
+
:icon="X"
|
|
130
|
+
type="button"
|
|
131
|
+
size="icon-xs"
|
|
132
|
+
class="shrink-0 text-destructive hover:text-destructive"
|
|
133
|
+
:aria-label="props.removeLabel(index)"
|
|
134
|
+
:title="props.removeLabel(index)"
|
|
135
|
+
:disabled="disabled"
|
|
136
|
+
@click="removeLine(index)"
|
|
137
|
+
/>
|
|
138
|
+
</div>
|
|
139
|
+
|
|
140
|
+
<Button type="button" variant="outline" size="sm" class="w-fit" :disabled="disabled" @click="addLine">
|
|
141
|
+
<Plus class="size-4" aria-hidden="true" />
|
|
142
|
+
{{ addText }}
|
|
143
|
+
</Button>
|
|
144
|
+
</div>
|
|
145
|
+
</template>
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
<!-- eslint-disable vue/multi-word-component-names, vue/no-reserved-component-names -->
|
|
2
|
+
<script setup lang="ts" generic="T, V extends AcceptableValue = AcceptableValue">
|
|
3
|
+
import { computed, nextTick, ref, type ComponentPublicInstance, type HTMLAttributes } from "vue";
|
|
4
|
+
import { X } from "@lucide/vue";
|
|
5
|
+
import type { AcceptableValue } from "reka-ui";
|
|
6
|
+
import type { AsyncStatus } from "@geektr.cloud/acrux-core";
|
|
7
|
+
import { Select as SelectRoot, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../ui/select";
|
|
8
|
+
import { Skeleton } from "../../ui/skeleton";
|
|
9
|
+
import { IconBtn } from "../base";
|
|
10
|
+
import { cn } from "cn";
|
|
11
|
+
import FieldError from "./FieldError.vue";
|
|
12
|
+
import { useFieldAccessibility } from "./fieldAccessibility";
|
|
13
|
+
|
|
14
|
+
defineOptions({ name: "Select", inheritAttrs: false });
|
|
15
|
+
|
|
16
|
+
const props = withDefaults(
|
|
17
|
+
defineProps<{
|
|
18
|
+
items: T[];
|
|
19
|
+
status?: AsyncStatus;
|
|
20
|
+
preFilterFn?: (item: T) => boolean;
|
|
21
|
+
transformFn?: (item: T) => { value: V; label: string };
|
|
22
|
+
placeholder?: string;
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
clearable?: boolean;
|
|
25
|
+
clearText?: string;
|
|
26
|
+
errorText?: string;
|
|
27
|
+
class?: HTMLAttributes["class"];
|
|
28
|
+
id?: string;
|
|
29
|
+
}>(),
|
|
30
|
+
{
|
|
31
|
+
status: () => ({ loading: false, error: null }),
|
|
32
|
+
preFilterFn: undefined,
|
|
33
|
+
transformFn: undefined,
|
|
34
|
+
placeholder: "请选择",
|
|
35
|
+
disabled: false,
|
|
36
|
+
clearable: false,
|
|
37
|
+
clearText: "清除选择",
|
|
38
|
+
errorText: "加载选项失败",
|
|
39
|
+
class: undefined,
|
|
40
|
+
id: undefined,
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const selected = defineModel<V | null>({ default: null });
|
|
45
|
+
|
|
46
|
+
const defaultTransform = (item: T): { value: V; label: string } => {
|
|
47
|
+
const option = item as { value?: V; label?: unknown } | null;
|
|
48
|
+
return { value: option?.value as V, label: String(option?.label ?? "") };
|
|
49
|
+
};
|
|
50
|
+
// Resolve at render time so replacing transformFn after mount takes effect.
|
|
51
|
+
const toOption = (item: T): { value: V; label: string } => (props.transformFn ?? defaultTransform)(item);
|
|
52
|
+
|
|
53
|
+
const options = computed(() => {
|
|
54
|
+
const list = props.preFilterFn ? props.items.filter(props.preFilterFn) : props.items;
|
|
55
|
+
return list.map(toOption);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const loading = computed(() => props.status.loading);
|
|
59
|
+
const showClear = computed(() => props.clearable && selected.value != null && !props.disabled && !loading.value);
|
|
60
|
+
const { controlAttrs, rootAttrs, errorId, describedBy } = useFieldAccessibility(() => props.status.error);
|
|
61
|
+
const triggerRef = ref<ComponentPublicInstance | null>(null);
|
|
62
|
+
|
|
63
|
+
const clear = async () => {
|
|
64
|
+
selected.value = null;
|
|
65
|
+
await nextTick();
|
|
66
|
+
const element = triggerRef.value?.$el;
|
|
67
|
+
if (element instanceof HTMLElement) element.focus();
|
|
68
|
+
};
|
|
69
|
+
</script>
|
|
70
|
+
|
|
71
|
+
<template>
|
|
72
|
+
<div data-slot="select-field" :class="cn('relative min-w-0 w-full', props.class)" v-bind="rootAttrs">
|
|
73
|
+
<SelectRoot v-model="selected" :disabled="disabled || loading">
|
|
74
|
+
<!-- Keep a labelled, disabled trigger while loading so external label[for] still resolves. -->
|
|
75
|
+
<SelectTrigger
|
|
76
|
+
:id="props.id"
|
|
77
|
+
ref="triggerRef"
|
|
78
|
+
:disabled="disabled || loading"
|
|
79
|
+
v-bind="controlAttrs"
|
|
80
|
+
:aria-describedby="describedBy"
|
|
81
|
+
:class="cn('w-full', showClear && 'pr-14')"
|
|
82
|
+
>
|
|
83
|
+
<Skeleton v-if="loading" class="h-4 w-4/5" />
|
|
84
|
+
<SelectValue v-else :placeholder="placeholder" />
|
|
85
|
+
</SelectTrigger>
|
|
86
|
+
<SelectContent>
|
|
87
|
+
<SelectItem v-for="option in options" :key="String(option.value)" :value="option.value">
|
|
88
|
+
{{ option.label }}
|
|
89
|
+
</SelectItem>
|
|
90
|
+
</SelectContent>
|
|
91
|
+
</SelectRoot>
|
|
92
|
+
<IconBtn
|
|
93
|
+
v-if="showClear"
|
|
94
|
+
:icon="X"
|
|
95
|
+
type="button"
|
|
96
|
+
size="icon-xs"
|
|
97
|
+
class="absolute top-4 right-8 -translate-y-1/2 text-muted-foreground hover:text-foreground"
|
|
98
|
+
:aria-label="clearText"
|
|
99
|
+
:title="clearText"
|
|
100
|
+
@click="clear"
|
|
101
|
+
/>
|
|
102
|
+
<FieldError v-if="status.error" :id="errorId" data-slot="select-error" class="mt-1">
|
|
103
|
+
{{ errorText }}
|
|
104
|
+
</FieldError>
|
|
105
|
+
</div>
|
|
106
|
+
</template>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { computed, toValue, useAttrs, useId, type MaybeRefOrGetter } from "vue";
|
|
2
|
+
|
|
3
|
+
const controlAriaAttrs = ["aria-label", "aria-labelledby", "aria-invalid"] as const;
|
|
4
|
+
|
|
5
|
+
export const useFieldAccessibility = (
|
|
6
|
+
hasError: MaybeRefOrGetter<unknown>,
|
|
7
|
+
errorIdSource?: MaybeRefOrGetter<string>,
|
|
8
|
+
) => {
|
|
9
|
+
const attrs = useAttrs();
|
|
10
|
+
const generatedErrorId = `${useId()}-error`;
|
|
11
|
+
const errorId = computed(() => (errorIdSource ? toValue(errorIdSource) : generatedErrorId));
|
|
12
|
+
|
|
13
|
+
const controlAttrs = computed(() => {
|
|
14
|
+
const picked: Record<string, unknown> = {};
|
|
15
|
+
for (const name of controlAriaAttrs) {
|
|
16
|
+
if (attrs[name] !== undefined) picked[name] = attrs[name];
|
|
17
|
+
}
|
|
18
|
+
return picked;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const rootAttrs = computed(() => {
|
|
22
|
+
const rest: Record<string, unknown> = {};
|
|
23
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
24
|
+
if (name === "aria-describedby" || (controlAriaAttrs as readonly string[]).includes(name)) continue;
|
|
25
|
+
rest[name] = value;
|
|
26
|
+
}
|
|
27
|
+
return rest;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const describedBy = computed(() => {
|
|
31
|
+
const ids = [attrs["aria-describedby"], toValue(hasError) ? errorId.value : undefined].filter(
|
|
32
|
+
(id): id is string => typeof id === "string" && id.length > 0,
|
|
33
|
+
);
|
|
34
|
+
return ids.length > 0 ? ids.join(" ") : undefined;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return { controlAttrs, rootAttrs, errorId, describedBy };
|
|
38
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { HTMLAttributes } from "vue";
|
|
3
|
+
import { onBeforeUnmount, onMounted, ref, useAttrs, watch } from "vue";
|
|
4
|
+
import type { RouteLocationRaw } from "vue-router";
|
|
5
|
+
import { useRouter } from "vue-router";
|
|
6
|
+
import { ArrowLeft } from "@lucide/vue";
|
|
7
|
+
import { cn } from "cn";
|
|
8
|
+
import { Button } from "../../ui/button";
|
|
9
|
+
|
|
10
|
+
defineOptions({ inheritAttrs: false });
|
|
11
|
+
|
|
12
|
+
const props = withDefaults(
|
|
13
|
+
defineProps<{
|
|
14
|
+
to?: RouteLocationRaw;
|
|
15
|
+
label?: string;
|
|
16
|
+
class?: HTMLAttributes["class"];
|
|
17
|
+
}>(),
|
|
18
|
+
{
|
|
19
|
+
to: undefined,
|
|
20
|
+
label: "返回",
|
|
21
|
+
class: undefined,
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const attrs = useAttrs();
|
|
26
|
+
const router = useRouter();
|
|
27
|
+
const canGoBack = ref(typeof window !== "undefined" && Boolean(window.history.state?.back));
|
|
28
|
+
|
|
29
|
+
const syncHistory = () => {
|
|
30
|
+
if (typeof window === "undefined") return;
|
|
31
|
+
canGoBack.value = Boolean(window.history.state?.back);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const go = () => {
|
|
35
|
+
if (canGoBack.value) {
|
|
36
|
+
router.back();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (props.to) void router.push(props.to);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
onMounted(() => {
|
|
43
|
+
syncHistory();
|
|
44
|
+
window.addEventListener("popstate", syncHistory);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
onBeforeUnmount(() => {
|
|
48
|
+
window.removeEventListener("popstate", syncHistory);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
watch(
|
|
52
|
+
() => router.currentRoute.value.fullPath,
|
|
53
|
+
() => syncHistory(),
|
|
54
|
+
{ flush: "post" },
|
|
55
|
+
);
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
<template>
|
|
59
|
+
<Button
|
|
60
|
+
v-bind="attrs"
|
|
61
|
+
variant="ghost"
|
|
62
|
+
size="sm"
|
|
63
|
+
:disabled="!canGoBack && !to"
|
|
64
|
+
:class="cn('shrink-0', props.class)"
|
|
65
|
+
@click="go"
|
|
66
|
+
>
|
|
67
|
+
<ArrowLeft aria-hidden="true" />
|
|
68
|
+
<slot>{{ label }}</slot>
|
|
69
|
+
</Button>
|
|
70
|
+
</template>
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { HTMLAttributes } from "vue";
|
|
3
|
+
import { computed, getCurrentInstance, useAttrs } from "vue";
|
|
4
|
+
import type { RouteLocationRaw } from "vue-router";
|
|
5
|
+
import { RotateCcw } from "@lucide/vue";
|
|
6
|
+
import { cn } from "cn";
|
|
7
|
+
import { Button } from "../../ui/button";
|
|
8
|
+
import { Text } from "../base";
|
|
9
|
+
import BackButton from "./BackButton.vue";
|
|
10
|
+
import PageState from "./PageState.vue";
|
|
11
|
+
import { hasPageListener } from "./pageState";
|
|
12
|
+
|
|
13
|
+
defineOptions({ inheritAttrs: false });
|
|
14
|
+
|
|
15
|
+
const props = withDefaults(
|
|
16
|
+
defineProps<{
|
|
17
|
+
title: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
loading?: boolean;
|
|
20
|
+
error?: unknown;
|
|
21
|
+
empty?: boolean;
|
|
22
|
+
class?: HTMLAttributes["class"];
|
|
23
|
+
backTo?: RouteLocationRaw;
|
|
24
|
+
backLabel?: string;
|
|
25
|
+
retryLabel?: string;
|
|
26
|
+
loadingLabel?: string;
|
|
27
|
+
emptyTitle?: string;
|
|
28
|
+
emptyDescription?: string;
|
|
29
|
+
errorTitle?: string;
|
|
30
|
+
}>(),
|
|
31
|
+
{
|
|
32
|
+
description: undefined,
|
|
33
|
+
loading: false,
|
|
34
|
+
error: undefined,
|
|
35
|
+
empty: false,
|
|
36
|
+
class: undefined,
|
|
37
|
+
backTo: undefined,
|
|
38
|
+
backLabel: undefined,
|
|
39
|
+
retryLabel: "重试",
|
|
40
|
+
loadingLabel: "加载中",
|
|
41
|
+
emptyTitle: "暂无数据",
|
|
42
|
+
emptyDescription: undefined,
|
|
43
|
+
errorTitle: "获取失败",
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const emit = defineEmits<{
|
|
48
|
+
retry: [];
|
|
49
|
+
}>();
|
|
50
|
+
|
|
51
|
+
const attrs = useAttrs();
|
|
52
|
+
const instance = getCurrentInstance();
|
|
53
|
+
|
|
54
|
+
const hasError = computed(() => Boolean(props.error));
|
|
55
|
+
const hasRetry = () => hasPageListener(instance, "retry");
|
|
56
|
+
const stateListeners = () => ({
|
|
57
|
+
onRetry: hasRetry() ? () => emit("retry") : undefined,
|
|
58
|
+
});
|
|
59
|
+
</script>
|
|
60
|
+
|
|
61
|
+
<template>
|
|
62
|
+
<section
|
|
63
|
+
v-bind="attrs"
|
|
64
|
+
data-slot="page-detail"
|
|
65
|
+
:aria-busy="loading ? 'true' : undefined"
|
|
66
|
+
:class="cn('mx-auto flex w-full min-w-0 max-w-5xl flex-col gap-4 px-4 py-8', props.class)"
|
|
67
|
+
>
|
|
68
|
+
<header data-slot="page-detail-header" class="flex flex-col gap-3">
|
|
69
|
+
<BackButton class="self-start" :to="backTo" :label="backLabel" />
|
|
70
|
+
<div class="flex flex-wrap items-start justify-between gap-x-4 gap-y-2">
|
|
71
|
+
<div class="min-w-0 flex-1">
|
|
72
|
+
<Text variant="h1" class="break-words">{{ title }}</Text>
|
|
73
|
+
<Text v-if="description" variant="lead" class="mt-1 break-words">
|
|
74
|
+
{{ description }}
|
|
75
|
+
</Text>
|
|
76
|
+
</div>
|
|
77
|
+
<div
|
|
78
|
+
v-if="$slots.actions || (hasRetry() && !loading && !hasError)"
|
|
79
|
+
class="flex min-w-0 flex-wrap items-center justify-end gap-2"
|
|
80
|
+
>
|
|
81
|
+
<slot name="actions" />
|
|
82
|
+
<span
|
|
83
|
+
v-if="$slots.actions && hasRetry() && !loading && !hasError"
|
|
84
|
+
class="mx-1 h-6 w-px bg-border"
|
|
85
|
+
aria-hidden="true"
|
|
86
|
+
/>
|
|
87
|
+
<Button
|
|
88
|
+
v-if="hasRetry() && !loading && !hasError"
|
|
89
|
+
variant="secondary"
|
|
90
|
+
size="icon"
|
|
91
|
+
:aria-label="retryLabel"
|
|
92
|
+
:title="retryLabel"
|
|
93
|
+
@click="emit('retry')"
|
|
94
|
+
>
|
|
95
|
+
<RotateCcw aria-hidden="true" />
|
|
96
|
+
</Button>
|
|
97
|
+
</div>
|
|
98
|
+
</div>
|
|
99
|
+
</header>
|
|
100
|
+
|
|
101
|
+
<PageState
|
|
102
|
+
v-bind="stateListeners()"
|
|
103
|
+
name="page-detail"
|
|
104
|
+
:loading="loading"
|
|
105
|
+
:error="error"
|
|
106
|
+
:empty="empty"
|
|
107
|
+
:retry-label="retryLabel"
|
|
108
|
+
:loading-label="loadingLabel"
|
|
109
|
+
:empty-title="emptyTitle"
|
|
110
|
+
:empty-description="emptyDescription"
|
|
111
|
+
:error-title="errorTitle"
|
|
112
|
+
>
|
|
113
|
+
<template v-if="$slots.skeleton" #skeleton>
|
|
114
|
+
<slot name="skeleton" />
|
|
115
|
+
</template>
|
|
116
|
+
<template v-if="$slots.error" #error>
|
|
117
|
+
<slot name="error" />
|
|
118
|
+
</template>
|
|
119
|
+
<template v-if="$slots.empty" #empty>
|
|
120
|
+
<slot name="empty" />
|
|
121
|
+
</template>
|
|
122
|
+
<slot />
|
|
123
|
+
</PageState>
|
|
124
|
+
</section>
|
|
125
|
+
</template>
|