fix: fix Select field preview (#4507)
* fix: fix Select field preview Closes #4084 * fix: fix field preview utils tests
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
import { isString } from '@sniptt/guards';
|
||||
|
||||
import { FieldSelectValue } from '@/object-record/record-field/types/FieldMetadata';
|
||||
import { selectFieldValueSchema } from '@/object-record/record-field/validation-schemas/selectFieldValueSchema';
|
||||
|
||||
export const isFieldSelectValue = (
|
||||
fieldValue: unknown,
|
||||
): fieldValue is FieldSelectValue => isString(fieldValue);
|
||||
options?: string[],
|
||||
): fieldValue is FieldSelectValue =>
|
||||
selectFieldValueSchema(options).safeParse(fieldValue).success;
|
||||
|
||||
@ -12,7 +12,6 @@ import { isFieldLinkValue } from '@/object-record/record-field/types/guards/isFi
|
||||
import { isFieldNumber } from '@/object-record/record-field/types/guards/isFieldNumber';
|
||||
import { isFieldRating } from '@/object-record/record-field/types/guards/isFieldRating';
|
||||
import { isFieldRelation } from '@/object-record/record-field/types/guards/isFieldRelation';
|
||||
import { isFieldRelationValue } from '@/object-record/record-field/types/guards/isFieldRelationValue';
|
||||
import { isFieldSelect } from '@/object-record/record-field/types/guards/isFieldSelect';
|
||||
import { isFieldSelectValue } from '@/object-record/record-field/types/guards/isFieldSelectValue';
|
||||
import { isFieldText } from '@/object-record/record-field/types/guards/isFieldText';
|
||||
@ -24,9 +23,11 @@ const isValueEmpty = (value: unknown) => !isDefined(value) || value === '';
|
||||
export const isFieldValueEmpty = ({
|
||||
fieldDefinition,
|
||||
fieldValue,
|
||||
selectOptionValues,
|
||||
}: {
|
||||
fieldDefinition: Pick<FieldDefinition<FieldMetadata>, 'type'>;
|
||||
fieldValue: unknown;
|
||||
selectOptionValues?: string[];
|
||||
}) => {
|
||||
if (
|
||||
isFieldUuid(fieldDefinition) ||
|
||||
@ -35,18 +36,18 @@ export const isFieldValueEmpty = ({
|
||||
isFieldNumber(fieldDefinition) ||
|
||||
isFieldRating(fieldDefinition) ||
|
||||
isFieldEmail(fieldDefinition) ||
|
||||
isFieldBoolean(fieldDefinition)
|
||||
isFieldBoolean(fieldDefinition) ||
|
||||
isFieldRelation(fieldDefinition)
|
||||
//|| isFieldPhone(fieldDefinition)
|
||||
) {
|
||||
return isValueEmpty(fieldValue);
|
||||
}
|
||||
|
||||
if (isFieldRelation(fieldDefinition)) {
|
||||
return isFieldRelationValue(fieldValue) && isValueEmpty(fieldValue);
|
||||
}
|
||||
|
||||
if (isFieldSelect(fieldDefinition)) {
|
||||
return isFieldSelectValue(fieldValue) && !isDefined(fieldValue);
|
||||
return (
|
||||
!isFieldSelectValue(fieldValue, selectOptionValues) ||
|
||||
!isDefined(fieldValue)
|
||||
);
|
||||
}
|
||||
|
||||
if (isFieldCurrency(fieldDefinition)) {
|
||||
@ -68,6 +69,6 @@ export const isFieldValueEmpty = ({
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Entity field type not supported in isEntityFieldEditModeEmptyFamilySelector : ${fieldDefinition.type}}`,
|
||||
`Entity field type not supported in isFieldValueEmpty : ${fieldDefinition.type}}`,
|
||||
);
|
||||
};
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { FieldSelectValue } from '@/object-record/record-field/types/FieldMetadata';
|
||||
|
||||
export const selectFieldValueSchema = (
|
||||
options?: string[],
|
||||
): z.ZodType<FieldSelectValue> =>
|
||||
options?.length
|
||||
? z.enum(options as [string, ...string[]]).nullable()
|
||||
: z.string().nullable();
|
||||
Reference in New Issue
Block a user