Files
element-plus/packages/components/form/src/utils.ts
Noblet Ouways 2f17df1209 style(eslint-config): newline before import type (#21036)
* 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 commit db0116a288.

* Revert "chore: fix"

This reverts commit 69c82a90c0.

* chore: fix

* style: `pnpm lint:fix`

* fix: lint

* chore: `pnpm format`
2025-06-16 15:37:12 +08:00

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
}