Config Provider 全局配置
Config Provider 被用来提供全局的配置选项,让你的配置能够在全局都能够被访问到。
i18n 配置
通过 Config Provider 来配置多语言,让你的应用可以随时切换语言。
export function ConfigProviderLocale() { const [lang, setLang] = useState<"zh-cn" | "en">("zh-cn"); const locale = lang === "zh-cn" ? zhCn : en; return ( <div> <ElButton style={{ marginBottom: "8px" }} onClick={() => setLang((v) => (v === "zh-cn" ? "en" : "zh-cn"))} > Switch Language </ElButton> <br /> <ElConfigProvider locale={locale}> <ElTable data={[]} style={{ marginBottom: "4px" }} /> <ElPagination total={100} /> </ElConfigProvider> </div> );}
对按钮进行配置
export function ConfigProviderButton() { const [config, setConfig] = useState<ButtonConfigContext>({ autoInsertSpace: true, type: "default", plain: true, round: true, text: false, dashed: false, }); const buttonTypes = ["default", "primary", "success", "warning", "info", "danger"] as const; return ( <div> <div> <ElCheckbox checked={config.autoInsertSpace} onCheckedChange={(v) => setConfig((c) => ({ ...c, autoInsertSpace: v }))} > autoInsertSpace </ElCheckbox> <ElCheckbox checked={config.plain} onCheckedChange={(v) => setConfig((c) => ({ ...c, plain: v }))} > plain </ElCheckbox> <ElCheckbox checked={config.round} onCheckedChange={(v) => setConfig((c) => ({ ...c, round: v }))} > round </ElCheckbox> <ElCheckbox checked={config.dashed} onCheckedChange={(v) => setConfig((c) => ({ ...c, dashed: v }))} > dashed </ElCheckbox> <ElCheckbox checked={config.text} onCheckedChange={(v) => setConfig((c) => ({ ...c, text: v }))} > text </ElCheckbox> <ElSelect value={config.type ?? "default"} onChange={(v) => setConfig((c) => ({ ...c, type: v as ButtonConfigContext["type"] }))} options={buttonTypes.map((t) => ({ value: t, label: t }))} style={{ maxWidth: "150px", marginLeft: "20px" }} /> </div> <ElDivider /> <ElConfigProvider button={config}> <ElButton>中文</ElButton> </ElConfigProvider> </div> );}
对链接进行配置 2.9.11
export function ConfigProviderLink() { const [config, setConfig] = useState<LinkConfigContext>({ type: "success", underline: "always", }); const linkTypes = ["primary", "success", "warning", "info", "danger", "default"] as const; const underlineOptions = ["always", "never", "hover"] as const; return ( <div> <div style={row16}> <div style={column}> <span>Type:</span> <ElSelect value={config.type ?? "success"} onChange={(v) => setConfig((c) => ({ ...c, type: v as LinkConfigContext["type"] }))} options={linkTypes.map((t) => ({ value: t, label: t }))} /> </div> <div style={column}> <span>Underline:</span> <ElSelect value={config.underline ?? "always"} onChange={(v) => setConfig((c) => ({ ...c, underline: v as LinkConfigContext["underline"] })) } options={underlineOptions.map((t) => ({ value: t, label: t }))} /> </div> </div> <ElDivider /> <ElConfigProvider link={config}> <ElLink>Link desu!</ElLink> </ElConfigProvider> </div> );}
对 Card 进行配置 2.10.5
export function ConfigProviderCard() { const [shadow, setShadow] = useState<"always" | "hover" | "never">("always"); const config = useMemo(() => ({ shadow }), [shadow]); return ( <div> Shadow: <div style={{ display: "flex", flexDirection: "column", justifyContent: "center" }}> <ElRadioGroup value={shadow} onChange={(v) => setShadow(v as "always" | "hover" | "never")} > <ElRadio value="always" label="always" /> <ElRadio value="hover" label="hover" /> <ElRadio value="never" label="never" /> </ElRadioGroup> <ElDivider /> <ElConfigProvider card={config}> <ElCard>Card desu!</ElCard> </ElConfigProvider> </div> </div> );}
对 Dialog 进行配置 2.10.7
export function ConfigProviderDialog() { const [config, setConfig] = useState<DialogConfigContext>({ alignCenter: false, draggable: false, overflow: false, }); const [visible, setVisible] = useState(false); const [enableTransition, setEnableTransition] = useState(false); const [isObjectTransition, setIsObjectTransition] = useState(false); const dialogConfig = useMemo<DialogConfigContext>( () => ({ ...config, transition: enableTransition ? isObjectTransition ? undefined : "dialog-bounce" : undefined, }), [config, enableTransition, isObjectTransition], ); return ( <div style={{ display: "flex", flexDirection: "column", gap: "16px", justifyContent: "center", }} > <div style={row8}> <ElSwitch checked={config.alignCenter ?? false} onCheckedChange={(v) => setConfig((c) => ({ ...c, alignCenter: v }))} activeText="alignCenter" /> </div> <div style={row16}> <ElSwitch checked={config.draggable ?? false} onCheckedChange={(v) => setConfig((c) => ({ ...c, draggable: v }))} activeText="draggable" /> <ElSwitch checked={config.overflow ?? false} disabled={!config.draggable} onCheckedChange={(v) => setConfig((c) => ({ ...c, overflow: v }))} activeText="overflow" /> </div> <div style={row8}> <ElSwitch checked={enableTransition} onCheckedChange={setEnableTransition} activeText="enable transition" /> <ElSwitch checked={isObjectTransition} disabled={!enableTransition} onCheckedChange={setIsObjectTransition} activeText="transition: object" inactiveText="transition: string" /> </div> <div style={row8}> <ElButton type="primary" size="small" onClick={() => setVisible(true)}> Open Dialog </ElButton> </div> <ElConfigProvider dialog={dialogConfig}> <ElDialog visible={visible} onVisibleChange={setVisible} title="Dialog Title" destroyOnClose > Dialog Content </ElDialog> </ElConfigProvider> {enableTransition && ( <div style={{ fontSize: "12px", opacity: 0.7 }}> {isObjectTransition ? ( <div> Using JavaScript controlled animation:{" "} <code>{JSON.stringify({ css: false })}</code> </div> ) : ( <div> Using string transition name: <code>dialog-bounce</code> </div> )} </div> )} </div> );}
对消息进行配置
export function ConfigProviderMessage() { const config = useMemo(() => ({ max: 3, plain: true, placement: "bottom" }), []); return ( <ElConfigProvider message={config}> <ElButton onClick={() => message("This is a message from bottom.")}>OPEN</ElButton> </ElConfigProvider> );}
空值配置2.7.0
设置 empty-values 来配置组件的默认空值。默认值是 ['', null, undefined]。如果认为空字符串不是一个空值,可以设置成 [undefined, null]。
设置 value-on-clear 以设置清空选项的值。组件默认值是 undefined。在日期组件中是 null。如果想设置成 undefined,请使用 () => undefined。
export function ConfigProviderEmptyValues() { const [value1, setValue1] = useState<string | number | null | undefined>(""); const [value2, setValue2] = useState<string | number | null | undefined>(""); 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 = (v: unknown) => { if (v === null || v === undefined) { message.info(`The clear value is: ${v}`); } }; return ( <ElConfigProvider valueOnClear={null} emptyValues={[undefined, null]}> <div style={{ display: "flex", flexWrap: "wrap", gap: "16px", alignItems: "center" }}> <ElSelect value={value1 ?? undefined} onChange={(v) => { setValue1(v as string); handleChange(v); }} clearable placeholder="Select" options={options} style={{ width: "240px" }} /> <ElSelectV2 value={value2 ?? undefined} onChange={(v) => { setValue2(v as string); handleChange(v); }} valueOnClear={() => undefined} clearable placeholder="Select" options={options} style={{ width: "240px" }} /> </div> </ElConfigProvider> );}
表格配置 2.13.3
export function ConfigProviderTable() { const [config, setConfig] = useState<TableConfigContext>({ showOverflowTooltip: true, tooltipEffect: "dark", }); const tableData = [ { 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" }, ]; return ( <div> <div> <ElCheckbox checked={config.showOverflowTooltip} onCheckedChange={(v) => setConfig((c) => ({ ...c, showOverflowTooltip: v }))} > showOverflowTooltip </ElCheckbox> <ElSelect value={config.tooltipEffect ?? "dark"} onChange={(v) => setConfig((c) => ({ ...c, tooltipEffect: v as "dark" | "light" })) } options={[ { value: "dark", label: "dark" }, { value: "light", label: "light" }, ]} style={{ maxWidth: "150px", marginLeft: "20px" }} /> </div> <ElDivider /> <ElConfigProvider table={config}> <ElTable data={tableData} style={fullWidth}> <ElTableColumn type="selection" width="55" /> <ElTableColumn prop="date" label="Date" width="120" /> <ElTableColumn prop="name" label="Name" width="120" /> <ElTableColumn prop="address" label="Address (inherited from config-provider)" width="300" /> <ElTableColumn prop="address" label="Address (explicit false)" showOverflowTooltip={false} /> </ElTable> </ElConfigProvider> </div> );}
实验性功能
在本节中,您可以学习如何使用 Config Provider 来提供实验性功能。现在,我们还没有添加任何实验性功能,但在未来的规划中,我们将添加一些实验性功能。您可以使用此配置来管理这些功能。
Config Provider API
Config Provider Attributes
| 属性名 | 说明 | 类型 | 默认值 |
|---|
| locale | 语言包对象 | object | en |
| size | 全局组件尺寸 | enum | — |
| zIndex | 全局初始 zIndex | number | — |
| namespace | 全局类名前缀 | string | el |
| button | 按钮相关配置 | object | — |
| link | 链接相关配置 | object | — |
| card | 卡片相关配置 | object | — |
| dialog | 对话框相关配置 | object | — |
| message | 消息相关配置 | object | — |
| table | 表格相关配置 | object | — |
| experimental-features | 实验特性开关 | object | — |
| empty-values | 全局空值集合 | array | — |
| value-on-clear | 全局清空返回值 | string / number / boolean / Function / null | — |
| a11y | 无障碍特性开关 | boolean | true |
| keyboard-navigation | 键盘导航开关 | boolean | true |
| 属性名 | 说明 | 类型 | 默认值 |
|---|
| type | 按钮类型 | enum | — |
| autoInsertSpace | 两个汉字间自动插入空格 | boolean | false |
| plain | 朴素按钮 | boolean | false |
| text | 文字按钮 | boolean | false |
| round | 圆角按钮 | boolean | false |
| dashed | 虚线按钮 | boolean | false |
Link Attribute
| 属性名 | 说明 | 类型 | 默认值 |
|---|
| type | 链接类型 | enum | default |
| underline | 下划线时机 | enum | hover |
Card Attribute
| 属性名 | 说明 | 类型 | 默认值 |
|---|
| shadow | 阴影显示时机 | enum | — |
Dialog Attribute
| 属性名 | 说明 | 类型 | 默认值 |
|---|
| align-center | 水平垂直居中 | boolean | false |
| draggable | 可拖拽 | boolean | false |
| overflow | 拖拽时可溢出视口 | boolean | false |
| transition | 过渡名字符串 | string | — |
Message Attribute
| 属性名 | 说明 | 类型 | 默认值 |
|---|
| max | 同位置最多显示的条数 | number | — |
| grouping | 合并相同内容的消息 | boolean | — |
| duration | 显示时长(毫秒),0 表示不自动关闭 | number | — |
| show-close | 是否显示关闭按钮 | boolean | — |
| offset | 距视口边缘距离(px) | number | — |
| plain | 朴素样式 | boolean | — |
| placement | 显示位置 | enum | — |
Table Attribute
| 属性名 | 说明 | 类型 | 默认值 |
|---|
| show-overflow-tooltip | 全局为所有列开启溢出省略 | boolean | — |
| tooltip-effect | 溢出提示的主题 | enum | dark |
Config Provider Slots
| 名称 | 说明 | 类型 |
|---|
| default | 自定义默认内容 | config: 当前生效的全局配置 |