Config Provider 全局配置
Config Provider 用于提供全局配置,使应用内任意位置都能访问这些配置。
国际化
通过 Config Provider 配置语言相关属性,即可实现语言切换。
使用 locale 与 i18n 等属性提供国际化配置。
暂无数据
- 1
- 2
- 3
- 4
- 5
- 6
- 10
页
共 100 条
vue
<template>
<div>
<u-button mb-2 @click="toggle">Switch Language</u-button>
<br />
<u-config-provider :locale="locale">
<u-table mb-1 :data="[]" />
<u-pagination :total="100" />
</u-config-provider>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue'
import zhCn from 'uniboot-ui/es/locale/lang/zh-cn'
import en from 'uniboot-ui/es/locale/lang/en'
const language = ref('zh-cn')
const locale = computed(() => (language.value === 'zh-cn' ? zhCn : en))
const toggle = () => {
language.value = language.value === 'zh-cn' ? 'en' : 'zh-cn'
}
</script>
隐藏源代码
按钮
vue
<template>
<div>
<div>
<u-checkbox v-model="config.autoInsertSpace">
autoInsertSpace
</u-checkbox>
<u-checkbox v-model="config.plain"> plain </u-checkbox>
<u-checkbox v-model="config.round"> round </u-checkbox>
<u-checkbox v-model="config.dashed"> dashed </u-checkbox>
<u-checkbox v-model="config.text"> text </u-checkbox>
<u-select v-model="config.type" class="ml-5" style="max-width: 150px">
<u-option
v-for="type in buttonTypes.filter(Boolean)"
:key="type"
:value="type"
/>
</u-select>
</div>
<u-divider />
<u-config-provider :button="config">
<u-button>中文</u-button>
</u-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { buttonTypes } from 'uniboot-ui'
import type { ButtonConfigContext } from 'uniboot-ui'
const config = reactive<ButtonConfigContext>({
autoInsertSpace: true,
type: 'default',
plain: true,
round: true,
text: false,
dashed: false,
})
</script>
隐藏源代码
链接
Type:
Underline:
vue
<template>
<div>
<div class="flex gap-4">
<div class="flex flex-col basis-150px gap-1">
<span>Type:</span>
<u-select v-model="config.type">
<u-option v-for="type in linkTypes" :key="type" :value="type" />
</u-select>
</div>
<div class="flex flex-col basis-150px gap-1">
<span>Underline:</span>
<u-select v-model="config.underline">
<u-option
v-for="type in underlineOptions"
:key="type"
:value="type"
/>
</u-select>
</div>
</div>
<u-divider />
<u-config-provider :link="config">
<u-link>Link desu!</u-link>
</u-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import type { LinkConfigContext } from 'uniboot-ui'
const linkTypes = ['primary', 'success', 'warning', 'info', 'danger', 'default']
const underlineOptions = ['always', 'never', 'hover']
const config = reactive<LinkConfigContext>({
type: 'success',
underline: 'always',
})
</script>
隐藏源代码
卡片
Shadow:
Card desu!
vue
<script lang="ts" setup>
import { reactive } from 'vue'
import type { CardConfigContext } from 'uniboot-ui'
const config = reactive<CardConfigContext>({
shadow: 'always',
})
</script>
<template>
Shadow:
<div class="flex flex-col justify-center">
<u-radio-group v-model="config.shadow">
<u-radio value="always">always</u-radio>
<u-radio value="hover">hover</u-radio>
<u-radio value="never">never</u-radio>
</u-radio-group>
<u-divider />
<u-config-provider :card="config">
<u-card>Card desu!</u-card>
</u-config-provider>
</div>
</template>
隐藏源代码
对话框
alignCenter
draggable
overflow
enable transition
transition: stringtransition: object
vue
<script lang="ts" setup>
import { computed, nextTick, ref, shallowReactive } from 'vue'
import type { ButtonInstance, DialogTransition } from 'uniboot-ui'
type GlobalConfig = {
alignCenter: boolean
draggable: boolean
overflow: boolean
transition?: DialogTransition
}
const config = shallowReactive<GlobalConfig>({
alignCenter: false,
draggable: false,
overflow: false,
})
const visible = ref(false)
const enableTransition = ref(false)
const isObjectTransition = ref(false)
const buttonRef = ref<ButtonInstance>()
const ANIMATION_DURATION = 300
const globalConfig = computed<GlobalConfig>(() => {
let transition: DialogTransition | undefined
if (enableTransition.value) {
if (isObjectTransition.value) {
transition = {
css: false,
onBeforeEnter(el) {
nextTick(() => {
if (buttonRef.value) {
const buttonRect = buttonRef.value.ref!.getBoundingClientRect()
const dialogEl = el.querySelector('.u-dialog') as HTMLElement
if (dialogEl) {
const dialogRect = dialogEl.getBoundingClientRect()
const offsetX =
buttonRect.left +
buttonRect.width / 2 -
(dialogRect.left + dialogRect.width / 2)
const offsetY =
buttonRect.top +
buttonRect.height / 2 -
(dialogRect.top + dialogRect.height / 2)
dialogEl.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(0.3)`
dialogEl.style.opacity = '0'
}
}
})
},
onEnter(el, done) {
nextTick(() => {
const dialogEl = el.querySelector('.u-dialog') as HTMLElement
if (dialogEl) {
// force reflow
dialogEl.offsetHeight
dialogEl.style.transition = `all ${ANIMATION_DURATION}ms cubic-bezier(0.4, 0, 1, 1)`
dialogEl.style.transform = 'translate(0, 0) scale(1)'
dialogEl.style.opacity = '1'
// wait for animation to complete, then cleanup inline styles to avoid affecting drag
setTimeout(() => {
dialogEl.style.transition = ''
dialogEl.style.transform = ''
dialogEl.style.opacity = ''
done()
}, ANIMATION_DURATION)
} else {
done()
}
})
},
onLeave(el, done) {
const dialogEl = el.querySelector('.u-dialog') as HTMLElement
if (dialogEl) {
if (buttonRef.value) {
const buttonRect = buttonRef.value.ref!.getBoundingClientRect()
const dialogRect = dialogEl.getBoundingClientRect()
const currentTransform = dialogEl.style.transform
let dragOffsetX = 0
let dragOffsetY = 0
// avoid draggable effect
if (currentTransform) {
const translateMatch = currentTransform.match(
/translate\(([^,]+),\s*([^)]+)\)/
)
if (translateMatch) {
dragOffsetX = Number.parseFloat(translateMatch[1])
dragOffsetY = Number.parseFloat(translateMatch[2])
}
}
const offsetX =
buttonRect.left +
buttonRect.width / 2 -
(dialogRect.left + dialogRect.width / 2) +
dragOffsetX
const offsetY =
buttonRect.top +
buttonRect.height / 2 -
(dialogRect.top + dialogRect.height / 2) +
dragOffsetY
dialogEl.style.transition = `all ${ANIMATION_DURATION}ms cubic-bezier(0.4, 0, 1, 1)`
dialogEl.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(0.3)`
dialogEl.style.opacity = '0'
// wait for animation to complete, then cleanup inline styles
setTimeout(() => {
dialogEl.style.transition = ''
dialogEl.style.transform = ''
dialogEl.style.opacity = ''
done()
}, ANIMATION_DURATION)
} else {
done()
}
} else {
done()
}
},
}
} else {
transition = 'dialog-bounce'
}
}
return {
alignCenter: config.alignCenter,
draggable: config.draggable,
overflow: config.overflow,
transition,
}
})
</script>
<template>
<div class="flex flex-col gap-4 justify-center">
<div class="flex items-center gap-2">
<u-switch v-model="config.alignCenter" active-text="alignCenter" />
</div>
<div class="flex items-center gap-4">
<u-switch v-model="config.draggable" active-text="draggable" />
<u-switch
v-model="config.overflow"
:disabled="!config.draggable"
active-text="overflow"
/>
</div>
<div class="flex items-center gap-2">
<u-switch v-model="enableTransition" active-text="enable transition" />
<u-switch
v-model="isObjectTransition"
:disabled="!enableTransition"
active-text="transition: object"
inactive-text="transition: string"
/>
</div>
<div class="flex items-center gap-2">
<u-button
ref="buttonRef"
type="primary"
size="small"
@click="visible = true"
>
Open Dialog
</u-button>
</div>
<u-config-provider :dialog="globalConfig">
<u-dialog v-model="visible" title="Dialog Title" destroy-on-close>
Dialog Content
</u-dialog>
</u-config-provider>
<div v-if="enableTransition" class="text-xs opacity-70">
<div v-if="isObjectTransition">
Using JavaScript controlled animation:
<code>{{ JSON.stringify(globalConfig.transition) }}</code>
</div>
<div v-else>
Using string transition name:
<code>{{ globalConfig.transition }}</code>
</div>
</div>
</div>
</template>
<style>
/* Bounce Animation */
.dialog-bounce-enter-active,
.dialog-bounce-leave-active,
.dialog-bounce-enter-active .u-dialog,
.dialog-bounce-leave-active .u-dialog {
transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.dialog-bounce-enter-from,
.dialog-bounce-leave-to {
opacity: 0;
}
.dialog-bounce-enter-from .u-dialog,
.dialog-bounce-leave-to .u-dialog {
transform: scale(0.3) translateY(-50px);
opacity: 0;
}
</style>
隐藏源代码
消息
vue
<template>
<div>
<u-config-provider :message="config">
<u-button @click="open">OPEN</u-button>
</u-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { UMessage } from 'uniboot-ui'
const config = reactive({
max: 3,
plain: true,
placement: 'bottom',
})
const open = () => {
UMessage('This is a message from bottom.')
}
</script>
隐藏源代码
空值与清空
支持的组件
- Cascader
- ColorPicker
- DatePicker
- Select
- SelectV2
- TimePicker
- TimeSelect
- TreeSelect
通过 empty-values 定义哪些值视为「空」。默认相当于 ['', null, undefined]。若认为空字符串是有意义的取值,可改为 [undefined, null]。
通过 value-on-clear 设置清空后的返回值。默认一般为 undefined;日期类组件中多为 null。若需要返回 undefined,可传入 () => undefined。
vue
<template>
<u-config-provider :value-on-clear="null" :empty-values="[undefined, null]">
<div class="flex flex-wrap gap-4 items-center">
<u-select
v-model="value1"
clearable
placeholder="Select"
style="width: 240px"
@change="handleChange"
>
<u-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</u-select>
<u-select-v2
v-model="value2"
clearable
placeholder="Select"
style="width: 240px"
:options="options"
:value-on-clear="() => undefined"
@change="handleChange"
/>
</div>
</u-config-provider>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { UMessage } from 'uniboot-ui'
const value1 = ref('')
const value2 = ref('')
const options = [
{
value: '',
label: 'All',
},
{
value: 'Option1',
label: 'Option1',
},
{
value: 'Option2',
label: 'Option2',
},
{
value: 'Option3',
label: 'Option3',
},
{
value: 'Option4',
label: 'Option4',
},
{
value: 'Option5',
label: 'Option5',
},
]
const handleChange = (value) => {
if ([undefined, null].includes(value)) {
UMessage.info(`The clear value is: ${value}`)
}
}
</script>
隐藏源代码
表格
vue
<template>
<div>
<div>
<u-checkbox v-model="config.showOverflowTooltip">
showOverflowTooltip
</u-checkbox>
<u-select
v-model="config.tooltipEffect"
class="ml-5"
style="max-width: 150px"
>
<u-option value="dark" label="dark" />
<u-option value="light" label="light" />
</u-select>
</div>
<u-divider />
<u-config-provider :table="config">
<u-table :data="tableData" style="width: 100%">
<u-table-column type="selection" width="55" />
<u-table-column label="Date" width="120">
<template #default="scope">{{ scope.row.date }}</template>
</u-table-column>
<u-table-column property="name" label="Name" width="120" />
<u-table-column
property="address"
label="Address (inherited from config-provider)"
width="300"
/>
<u-table-column
property="address"
label="Address (explicit false)"
:show-overflow-tooltip="false"
/>
</u-table>
</u-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import type { TableConfigContext } from 'uniboot-ui'
const config = reactive<TableConfigContext>({
showOverflowTooltip: true,
tooltipEffect: 'dark',
})
interface User {
date: string
name: string
address: string
}
const tableData: User[] = [
{
date: '2016-05-04',
name: 'Aleyna Kutzner',
address: 'Lohrbergstr. 86c, Süd Lilli, Saarland',
},
{
date: '2016-05-03',
name: 'Helen Jacobi',
address: '760 A Street, South Frankfield, Illinois',
},
{
date: '2016-05-02',
name: 'Brandon Deckert',
address: 'Arnold-Ohletz-Str. 41a, Alt Malinascheid, Thüringen',
},
{
date: '2016-05-01',
name: 'Margie Smith',
address: '23618 Windsor Drive, West Ricardoview, Idaho',
},
]
</script>
隐藏源代码
实验性功能
本节说明如何通过 Config Provider 开启实验特性。当前尚未内置具体实验项;后续路线图中会逐步加入,可用该配置统一管理是否启用。
API
Config Provider 属性
| 名称 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| locale | 语言包对象 | object 语言列表 | en |
| size | 全局组件尺寸 | enum | default |
| zIndex | 全局初始 z-index | number | — |
| namespace | 组件类名前缀(与 $namespace 配合) | string | el |
| button | 按钮相关默认配置,见下表 | object | 见下表 |
| link | 链接相关默认配置,见下表 | object | 见下表 |
| dialog | 对话框相关默认配置,见下表 | object | 见下表 |
| message | 消息相关默认配置,见下表 | object | 见下表 |
| experimental-features | 实验特性开关,未列出的项默认为 false | object | — |
| empty-values | 全局「空值」列表 | array | — |
| value-on-clear | 全局清空后的返回值 | string / number / boolean / Function | — |
| table | 表格相关默认配置,见下表 | object | 见下表 |
按钮默认项
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| type | 按钮类型;若同时设置 color,以后者为准 | enum | — |
| autoInsertSpace | 是否在两个汉字之间自动插入空格(仅当文案长度为 2 且均为中文时生效) | boolean | false |
| plain | 是否为朴素按钮 | boolean | false |
| text | 是否为文字按钮 | boolean | false |
| round | 是否为圆角按钮 | boolean | false |
| dashed | 是否为虚线按钮 | boolean | false |
链接默认项
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| type | 类型 | enum | default |
| underline | 下划线出现时机 | enum | hover |
卡片默认项
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| shadow | 阴影展示时机 | enum | — |
对话框默认项
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| align-center | 是否在水平、垂直方向居中 | boolean | false |
| draggable | 是否可拖拽 | boolean | false |
| overflow | 可拖拽时是否允许超出视口较长距离 | boolean | false |
| transition | 自定义过渡,可为过渡名称字符串或 Vue Transition 配置对象 | string / object | — |
消息默认项
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| max | 同时最多展示条数 | number | — |
| grouping | 是否合并相同内容(VNode 类型消息不支持) | boolean | — |
| duration | 显示时长(毫秒);为 0 时不自动关闭 | number | — |
| showClose | 是否显示关闭按钮 | boolean | — |
| offset | 距离视口顶部的偏移 | number | — |
| plain | 是否为朴素样式 | boolean | — |
| placement | 出现位置 | enum | — |
表格默认项
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| show-overflow-tooltip | 单元格内容过长时是否省略并以 Tooltip 展示;作用于全部列,详见 tooltip-options | boolean / [object] | — |
| tooltip-effect | 溢出提示的主题 | enum | dark |
| tooltip-options | 溢出提示的配置,参见 Tooltip 属性 | object | object |
| tooltip-formatter | 使用 show-overflow-tooltip 时自定义提示内容 | Function | — |
Config Provider 插槽
| 名称 | 说明 | 类型 |
|---|---|---|
| default | 自定义默认内容 | config:由上层继承而来的全局配置对象 |