mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
* refactor(tokens): [form] - remove tokens/form * Move content in tokens/form to components/form. * Replace token imports in components/form. * chore: remove form/tokens and replace imports * refactor(components): [form/form-item] * Move `useForm` related hooks to components/form * Replace references to reduce circular dependencies.
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { computed, ref } from 'vue'
|
|
import { debugWarn, ensureArray } from '@element-plus/utils'
|
|
import type { Arrayable } from '@element-plus/utils'
|
|
import type { FormItemContext } from './types'
|
|
import type { FormItemProp } from './form-item'
|
|
|
|
const SCOPE = 'ElForm'
|
|
|
|
export function useFormLabelWidth() {
|
|
const potentialLabelWidthArr = ref<number[]>([])
|
|
|
|
const autoLabelWidth = computed(() => {
|
|
if (!potentialLabelWidthArr.value.length) return '0'
|
|
const max = Math.max(...potentialLabelWidthArr.value)
|
|
return max ? `${max}px` : ''
|
|
})
|
|
|
|
function getLabelWidthIndex(width: number) {
|
|
const index = potentialLabelWidthArr.value.indexOf(width)
|
|
if (index === -1 && autoLabelWidth.value === '0') {
|
|
debugWarn(SCOPE, `unexpected width ${width}`)
|
|
}
|
|
return index
|
|
}
|
|
|
|
function registerLabelWidth(val: number, oldVal: number) {
|
|
if (val && oldVal) {
|
|
const index = getLabelWidthIndex(oldVal)
|
|
potentialLabelWidthArr.value.splice(index, 1, val)
|
|
} else if (val) {
|
|
potentialLabelWidthArr.value.push(val)
|
|
}
|
|
}
|
|
|
|
function deregisterLabelWidth(val: number) {
|
|
const index = getLabelWidthIndex(val)
|
|
if (index > -1) {
|
|
potentialLabelWidthArr.value.splice(index, 1)
|
|
}
|
|
}
|
|
|
|
return {
|
|
autoLabelWidth,
|
|
registerLabelWidth,
|
|
deregisterLabelWidth,
|
|
}
|
|
}
|
|
|
|
export const filterFields = (
|
|
fields: FormItemContext[],
|
|
props: Arrayable<FormItemProp>
|
|
) => {
|
|
const normalized = ensureArray(props)
|
|
return normalized.length > 0
|
|
? fields.filter((field) => field.prop && normalized.includes(field.prop))
|
|
: fields
|
|
}
|