2023-09-04 10:03:12 +08:00
|
|
|
import type { Meta } from 'rc-field-form/lib/interface';
|
|
|
|
|
|
|
|
import type { ValidateStatus } from './FormItem';
|
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 {
|
2023-01-16 09:55:52 +08:00
|
|
|
if (!namePath.length) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-07-04 15:00:11 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2023-09-04 10:03:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get merged status by meta or passed `validateStatus`.
|
|
|
|
*/
|
|
|
|
export function getStatus<DefaultValue>(
|
|
|
|
errors: React.ReactNode[],
|
|
|
|
warnings: React.ReactNode[],
|
|
|
|
meta: Meta,
|
|
|
|
defaultValidateStatus: ValidateStatus | DefaultValue,
|
|
|
|
hasFeedback?: boolean,
|
|
|
|
validateStatus?: ValidateStatus,
|
|
|
|
): ValidateStatus | DefaultValue {
|
|
|
|
let status = defaultValidateStatus;
|
|
|
|
|
|
|
|
if (validateStatus !== undefined) {
|
|
|
|
status = validateStatus;
|
|
|
|
} else if (meta.validating) {
|
|
|
|
status = 'validating';
|
|
|
|
} else if (errors.length) {
|
|
|
|
status = 'error';
|
|
|
|
} else if (warnings.length) {
|
|
|
|
status = 'warning';
|
|
|
|
} else if (meta.touched || (hasFeedback && meta.validated)) {
|
|
|
|
// success feedback should display when pass hasFeedback prop and current value is valid value
|
|
|
|
status = 'success';
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|