Files
element-plus/packages/utils/vue/install.ts
rzzf de984256ab feat(utils): add setPropsDefaults method (#23612)
* feat(components): add `setPropsDefaults` method

* chore: cleanup

* test: add test

* docs: add Custom Defaults

* fix: apply coderabbit suggest

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* feat: support array props

* chore: fix type error

* refactor: use fromPairs replace zipObject

* fix: correct type

* refactor: apply coderabbit suggest

* docs: detail tip

* docs: add warning

* Update docs/en-US/guide/custom-defaults.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* chore: revert main.ts

* docs: update

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2026-02-14 13:37:05 +01:00

87 lines
2.1 KiB
TypeScript

import { hasOwn, isArray } from '@vue/shared'
import { fromPairs, isPlainObject } from 'lodash-unified'
import { NOOP } from '../functions'
import type { App, Directive } from 'vue'
import type { SFCInstallWithContext, SFCWithInstall } from './typescript'
export const withPropsDefaultsSetter = (target: any) => {
const _p = target.props
const props = isArray(_p) ? fromPairs(_p.map((key) => [key, {}])) : _p
target.setPropsDefaults = (defaults: Record<string, any>) => {
if (!props) {
return
}
for (const [key, value] of Object.entries(defaults)) {
const prop = props[key]
if (!hasOwn(props, key)) {
continue
}
if (isPlainObject(prop)) {
// e.g. { type: String }
props[key] = {
...prop,
default: value,
}
continue
}
props[key] = {
type: prop,
default: value,
}
}
target.props = props
}
}
export const withInstall = <T, E extends Record<string, any>>(
main: T,
extra?: E
) => {
;(main as SFCWithInstall<T>).install = (app): void => {
for (const comp of [main, ...Object.values(extra ?? {})]) {
app.component(comp.name, comp)
}
}
if (extra) {
for (const [key, comp] of Object.entries(extra)) {
;(main as any)[key] = comp
}
}
withPropsDefaultsSetter(main)
return main as SFCWithInstall<T> & E
}
export const withInstallFunction = <T>(fn: T, name: string) => {
;(fn as SFCWithInstall<T>).install = (app: App) => {
;(fn as SFCInstallWithContext<T>)._context = app._context
app.config.globalProperties[name] = fn
}
return fn as SFCInstallWithContext<T>
}
export const withInstallDirective = <T extends Directive>(
directive: T,
name: string
) => {
;(directive as SFCWithInstall<T>).install = (app: App): void => {
app.directive(name, directive)
}
return directive as SFCWithInstall<T>
}
export const withNoopInstall = <T>(component: T) => {
;(component as SFCWithInstall<T>).install = NOOP
withPropsDefaultsSetter(component)
return component as SFCWithInstall<T>
}