mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-09-27 13:46:41 +08:00
feat(components): refactoring button configuration, supporting the merging and sorting of custom and preset buttons.
This commit is contained in:
parent
1ff4d82d19
commit
ac307b72ad
@ -1,40 +1,193 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { computed } from 'vue';
|
||||||
import { $t } from '@/locales';
|
import { $t } from '@/locales';
|
||||||
|
|
||||||
defineOptions({
|
defineOptions({
|
||||||
name: 'TableHeaderOperation'
|
name: 'TableHeaderOperation'
|
||||||
});
|
});
|
||||||
|
|
||||||
interface Props {
|
/** button item config type */
|
||||||
itemAlign?: NaiveUI.Align;
|
export interface TableButtonItem {
|
||||||
disabledDelete?: boolean;
|
/** button unique identifier */
|
||||||
loading?: boolean;
|
key: string;
|
||||||
|
/** button text, if not passed, use default internationalized text */
|
||||||
|
text?: string;
|
||||||
|
/** button icon */
|
||||||
|
icon: string;
|
||||||
|
/** button type */
|
||||||
|
type?: 'default' | 'primary' | 'info' | 'success' | 'warning' | 'error';
|
||||||
|
/** whether to be ghost button */
|
||||||
|
ghost?: boolean;
|
||||||
|
/** whether to disable button */
|
||||||
|
disabled?: boolean;
|
||||||
|
/** button order, the smaller the value, the closer to the front */
|
||||||
|
order?: number;
|
||||||
|
/** whether to need confirm */
|
||||||
|
needConfirm?: boolean;
|
||||||
|
/** confirm text */
|
||||||
|
confirmText?: string;
|
||||||
|
/** button extra props */
|
||||||
|
buttonProps?: Record<string, any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
defineProps<Props>();
|
/** preset button type */
|
||||||
|
export type PresetButtonKey = 'add' | 'delete' | 'refresh';
|
||||||
|
|
||||||
|
/** button type constant */
|
||||||
|
const ButtonType = {
|
||||||
|
DEFAULT: 'default',
|
||||||
|
PRIMARY: 'primary',
|
||||||
|
INFO: 'info',
|
||||||
|
SUCCESS: 'success',
|
||||||
|
WARNING: 'warning',
|
||||||
|
ERROR: 'error'
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
/** component props */
|
||||||
|
export interface TableHeaderOperationProps {
|
||||||
|
/** internal element alignment */
|
||||||
|
itemAlign?: 'start' | 'end' | 'center' | 'baseline' | 'stretch';
|
||||||
|
/** loading status */
|
||||||
|
loading?: boolean;
|
||||||
|
/** button config list */
|
||||||
|
buttons?: TableButtonItem[];
|
||||||
|
/** whether to show preset buttons */
|
||||||
|
showPresetButtons?: boolean;
|
||||||
|
/** disabled preset buttons */
|
||||||
|
disabledPresetButtons?: PresetButtonKey[];
|
||||||
|
/** hidden preset buttons */
|
||||||
|
hiddenPresetButtons?: PresetButtonKey[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = withDefaults(defineProps<TableHeaderOperationProps>(), {
|
||||||
|
itemAlign: 'center',
|
||||||
|
loading: false,
|
||||||
|
showPresetButtons: true,
|
||||||
|
disabledPresetButtons: () => [],
|
||||||
|
hiddenPresetButtons: () => [],
|
||||||
|
buttons: () => []
|
||||||
|
});
|
||||||
|
|
||||||
|
/** component events */
|
||||||
interface Emits {
|
interface Emits {
|
||||||
|
/** add button click event */
|
||||||
(e: 'add'): void;
|
(e: 'add'): void;
|
||||||
|
/** delete button click event */
|
||||||
(e: 'delete'): void;
|
(e: 'delete'): void;
|
||||||
|
/** refresh button click event */
|
||||||
(e: 'refresh'): void;
|
(e: 'refresh'): void;
|
||||||
|
/** custom button click event */
|
||||||
|
(e: 'click', key: string): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const emit = defineEmits<Emits>();
|
const emit = defineEmits<Emits>();
|
||||||
|
|
||||||
const columns = defineModel<NaiveUI.TableColumnCheck[]>('columns', {
|
const columns = defineModel<any[]>('columns', {
|
||||||
default: () => []
|
default: () => []
|
||||||
});
|
});
|
||||||
|
|
||||||
function add() {
|
/** get preset button config */
|
||||||
emit('add');
|
const getPresetButtons = (): TableButtonItem[] => {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
key: 'add',
|
||||||
|
text: $t('common.add'),
|
||||||
|
icon: 'ic-round-plus',
|
||||||
|
type: ButtonType.PRIMARY,
|
||||||
|
ghost: true,
|
||||||
|
order: 10,
|
||||||
|
disabled: props.disabledPresetButtons.includes('add')
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'delete',
|
||||||
|
text: $t('common.batchDelete'),
|
||||||
|
icon: 'ic-round-delete',
|
||||||
|
type: ButtonType.ERROR,
|
||||||
|
ghost: true,
|
||||||
|
order: 20,
|
||||||
|
disabled: props.disabledPresetButtons.includes('delete'),
|
||||||
|
needConfirm: true,
|
||||||
|
confirmText: $t('common.confirmDelete')
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'refresh',
|
||||||
|
text: $t('common.refresh'),
|
||||||
|
icon: 'mdi-refresh',
|
||||||
|
type: ButtonType.DEFAULT,
|
||||||
|
order: 30,
|
||||||
|
disabled: props.disabledPresetButtons.includes('refresh')
|
||||||
|
}
|
||||||
|
].filter(btn => !props.hiddenPresetButtons.includes(btn.key as PresetButtonKey));
|
||||||
|
};
|
||||||
|
|
||||||
|
/** merge button config */
|
||||||
|
const mergedButtons = computed(() => {
|
||||||
|
const result: TableButtonItem[] = [];
|
||||||
|
|
||||||
|
// add preset buttons
|
||||||
|
if (props.showPresetButtons) {
|
||||||
|
result.push(...getPresetButtons());
|
||||||
|
}
|
||||||
|
|
||||||
|
// add custom buttons
|
||||||
|
if (props.buttons?.length) {
|
||||||
|
// merge custom buttons with preset buttons
|
||||||
|
props.buttons.forEach(customBtn => {
|
||||||
|
const existingIndex = result.findIndex(btn => btn.key === customBtn.key);
|
||||||
|
|
||||||
|
if (existingIndex >= 0) {
|
||||||
|
// override preset buttons
|
||||||
|
result[existingIndex] = {
|
||||||
|
...result[existingIndex],
|
||||||
|
...customBtn
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// add new button
|
||||||
|
result.push({
|
||||||
|
order: 100, // custom button default order value
|
||||||
|
...customBtn
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort by order
|
||||||
|
return result.sort((a, b) => (a.order || 0) - (b.order || 0));
|
||||||
|
});
|
||||||
|
|
||||||
|
/** handle button click event */
|
||||||
|
function handleButtonClick(key: string) {
|
||||||
|
switch (key) {
|
||||||
|
case 'add':
|
||||||
|
emit('add');
|
||||||
|
break;
|
||||||
|
case 'delete':
|
||||||
|
emit('delete');
|
||||||
|
break;
|
||||||
|
case 'refresh':
|
||||||
|
emit('refresh');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
emit('click', key);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function batchDelete() {
|
/** get button text */
|
||||||
emit('delete');
|
function getButtonText(button: TableButtonItem): string {
|
||||||
}
|
if (button.text) return button.text;
|
||||||
|
|
||||||
function refresh() {
|
// use default internationalized text
|
||||||
emit('refresh');
|
switch (button.key) {
|
||||||
|
case 'add':
|
||||||
|
return $t('common.add');
|
||||||
|
case 'delete':
|
||||||
|
return $t('common.batchDelete');
|
||||||
|
case 'refresh':
|
||||||
|
return $t('common.refresh');
|
||||||
|
default:
|
||||||
|
return button.key;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -42,33 +195,53 @@ function refresh() {
|
|||||||
<NSpace :align="itemAlign" wrap justify="end" class="lt-sm:w-200px">
|
<NSpace :align="itemAlign" wrap justify="end" class="lt-sm:w-200px">
|
||||||
<slot name="prefix"></slot>
|
<slot name="prefix"></slot>
|
||||||
<slot name="default">
|
<slot name="default">
|
||||||
<NButton size="small" ghost type="primary" @click="add">
|
<template v-for="button in mergedButtons" :key="button.key">
|
||||||
<template #icon>
|
<!-- button that needs confirmation -->
|
||||||
<icon-ic-round-plus class="text-icon" />
|
<NPopconfirm v-if="button.needConfirm" @positive-click="handleButtonClick(button.key)">
|
||||||
</template>
|
<template #trigger>
|
||||||
{{ $t('common.add') }}
|
<NButton
|
||||||
</NButton>
|
size="small"
|
||||||
<NPopconfirm @positive-click="batchDelete">
|
:ghost="button.ghost"
|
||||||
<template #trigger>
|
:type="button.type"
|
||||||
<NButton size="small" ghost type="error" :disabled="disabledDelete">
|
:disabled="button.disabled"
|
||||||
<template #icon>
|
v-bind="button.buttonProps"
|
||||||
<icon-ic-round-delete class="text-icon" />
|
>
|
||||||
</template>
|
<template #icon>
|
||||||
{{ $t('common.batchDelete') }}
|
<SvgIcon
|
||||||
</NButton>
|
:icon="button.icon"
|
||||||
</template>
|
class="text-icon"
|
||||||
{{ $t('common.confirmDelete') }}
|
:class="{ 'animate-spin': loading && button.key === 'refresh' }"
|
||||||
</NPopconfirm>
|
/>
|
||||||
</slot>
|
</template>
|
||||||
<NButton size="small" @click="refresh">
|
{{ getButtonText(button) }}
|
||||||
<template #icon>
|
</NButton>
|
||||||
<icon-mdi-refresh class="text-icon" :class="{ 'animate-spin': loading }" />
|
</template>
|
||||||
|
{{ button.confirmText }}
|
||||||
|
</NPopconfirm>
|
||||||
|
|
||||||
|
<!-- normal button -->
|
||||||
|
<NButton
|
||||||
|
v-else
|
||||||
|
size="small"
|
||||||
|
:ghost="button.ghost"
|
||||||
|
:type="button.type"
|
||||||
|
:disabled="button.disabled"
|
||||||
|
v-bind="button.buttonProps"
|
||||||
|
@click="handleButtonClick(button.key)"
|
||||||
|
>
|
||||||
|
<template #icon>
|
||||||
|
<SvgIcon
|
||||||
|
:icon="button.icon"
|
||||||
|
class="text-icon"
|
||||||
|
:class="{ 'animate-spin': loading && button.key === 'refresh' }"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
{{ getButtonText(button) }}
|
||||||
|
</NButton>
|
||||||
</template>
|
</template>
|
||||||
{{ $t('common.refresh') }}
|
</slot>
|
||||||
</NButton>
|
|
||||||
<TableColumnSetting v-model:columns="columns" />
|
<TableColumnSetting v-model:columns="columns" />
|
||||||
<slot name="suffix"></slot>
|
<slot name="suffix"></slot>
|
||||||
</NSpace>
|
</NSpace>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped></style>
|
|
||||||
|
Loading…
Reference in New Issue
Block a user