mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
* perf: change to import-x * feat: add rules * chore: fix rule * chore: fix * chore: fix * chore: fix * style: `pnpm lint:fix` * Revert "style: `pnpm lint:fix`" This reverts commitdb0116a288. * Revert "chore: fix" This reverts commit69c82a90c0. * chore: fix * style: `pnpm lint:fix` * fix: lint * chore: `pnpm format`
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import { computed, ref } from 'vue'
|
|
import { debugWarn, ensureArray, isArray } 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).map((prop) =>
|
|
isArray(prop) ? prop.join('.') : prop
|
|
)
|
|
return normalized.length > 0
|
|
? fields.filter(
|
|
(field) => field.propString && normalized.includes(field.propString)
|
|
)
|
|
: fields
|
|
}
|