2022-05-07 14:31:54 +08:00
|
|
|
import type { InternalNamePath } from './interface';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-12-12 14:58:49 +08:00
|
|
|
// form item name black list. in form ,you can use form.id get the form item element.
|
|
|
|
// use object hasOwnProperty will get better performance if black list is longer.
|
|
|
|
const formItemNameBlackList = ['parentNode'];
|
|
|
|
|
|
|
|
// default form item id prefix.
|
|
|
|
const defaultItemNamePrefixCls: string = 'form_item';
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
export function toArray<T>(candidate?: T | T[] | false): T[] {
|
|
|
|
if (candidate === undefined || candidate === false) return [];
|
|
|
|
|
|
|
|
return Array.isArray(candidate) ? candidate : [candidate];
|
|
|
|
}
|
2019-07-04 15:00:11 +08:00
|
|
|
|
|
|
|
export function getFieldId(namePath: InternalNamePath, formName?: string): string | undefined {
|
|
|
|
if (!namePath.length) return undefined;
|
|
|
|
|
|
|
|
const mergedId = namePath.join('_');
|
2021-12-12 14:58:49 +08:00
|
|
|
|
|
|
|
if (formName) {
|
|
|
|
return `${formName}_${mergedId}`;
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:45:55 +08:00
|
|
|
const isIllegalName = formItemNameBlackList.includes(mergedId);
|
2021-12-12 14:58:49 +08:00
|
|
|
|
|
|
|
return isIllegalName ? `${defaultItemNamePrefixCls}_${mergedId}` : mergedId;
|
2019-07-04 15:00:11 +08:00
|
|
|
}
|