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 { FieldSelectValue } from '@/object-record/record-field/types/FieldMetadata';
|
||||||
|
import { selectFieldValueSchema } from '@/object-record/record-field/validation-schemas/selectFieldValueSchema';
|
||||||
|
|
||||||
export const isFieldSelectValue = (
|
export const isFieldSelectValue = (
|
||||||
fieldValue: unknown,
|
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 { isFieldNumber } from '@/object-record/record-field/types/guards/isFieldNumber';
|
||||||
import { isFieldRating } from '@/object-record/record-field/types/guards/isFieldRating';
|
import { isFieldRating } from '@/object-record/record-field/types/guards/isFieldRating';
|
||||||
import { isFieldRelation } from '@/object-record/record-field/types/guards/isFieldRelation';
|
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 { isFieldSelect } from '@/object-record/record-field/types/guards/isFieldSelect';
|
||||||
import { isFieldSelectValue } from '@/object-record/record-field/types/guards/isFieldSelectValue';
|
import { isFieldSelectValue } from '@/object-record/record-field/types/guards/isFieldSelectValue';
|
||||||
import { isFieldText } from '@/object-record/record-field/types/guards/isFieldText';
|
import { isFieldText } from '@/object-record/record-field/types/guards/isFieldText';
|
||||||
@ -24,9 +23,11 @@ const isValueEmpty = (value: unknown) => !isDefined(value) || value === '';
|
|||||||
export const isFieldValueEmpty = ({
|
export const isFieldValueEmpty = ({
|
||||||
fieldDefinition,
|
fieldDefinition,
|
||||||
fieldValue,
|
fieldValue,
|
||||||
|
selectOptionValues,
|
||||||
}: {
|
}: {
|
||||||
fieldDefinition: Pick<FieldDefinition<FieldMetadata>, 'type'>;
|
fieldDefinition: Pick<FieldDefinition<FieldMetadata>, 'type'>;
|
||||||
fieldValue: unknown;
|
fieldValue: unknown;
|
||||||
|
selectOptionValues?: string[];
|
||||||
}) => {
|
}) => {
|
||||||
if (
|
if (
|
||||||
isFieldUuid(fieldDefinition) ||
|
isFieldUuid(fieldDefinition) ||
|
||||||
@ -35,18 +36,18 @@ export const isFieldValueEmpty = ({
|
|||||||
isFieldNumber(fieldDefinition) ||
|
isFieldNumber(fieldDefinition) ||
|
||||||
isFieldRating(fieldDefinition) ||
|
isFieldRating(fieldDefinition) ||
|
||||||
isFieldEmail(fieldDefinition) ||
|
isFieldEmail(fieldDefinition) ||
|
||||||
isFieldBoolean(fieldDefinition)
|
isFieldBoolean(fieldDefinition) ||
|
||||||
|
isFieldRelation(fieldDefinition)
|
||||||
//|| isFieldPhone(fieldDefinition)
|
//|| isFieldPhone(fieldDefinition)
|
||||||
) {
|
) {
|
||||||
return isValueEmpty(fieldValue);
|
return isValueEmpty(fieldValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFieldRelation(fieldDefinition)) {
|
|
||||||
return isFieldRelationValue(fieldValue) && isValueEmpty(fieldValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isFieldSelect(fieldDefinition)) {
|
if (isFieldSelect(fieldDefinition)) {
|
||||||
return isFieldSelectValue(fieldValue) && !isDefined(fieldValue);
|
return (
|
||||||
|
!isFieldSelectValue(fieldValue, selectOptionValues) ||
|
||||||
|
!isDefined(fieldValue)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFieldCurrency(fieldDefinition)) {
|
if (isFieldCurrency(fieldDefinition)) {
|
||||||
@ -68,6 +69,6 @@ export const isFieldValueEmpty = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(
|
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();
|
||||||
@ -55,11 +55,13 @@ export const useFieldPreview = ({
|
|||||||
})
|
})
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
const selectOptionValues = selectOptions?.map((option) => option.value);
|
||||||
const isValueFromFirstRecord =
|
const isValueFromFirstRecord =
|
||||||
firstRecord &&
|
firstRecord &&
|
||||||
!isFieldValueEmpty({
|
!isFieldValueEmpty({
|
||||||
fieldDefinition: { type: fieldMetadataItem.type },
|
fieldDefinition: { type: fieldMetadataItem.type },
|
||||||
fieldValue: fieldPreviewValueFromFirstRecord,
|
fieldValue: fieldPreviewValueFromFirstRecord,
|
||||||
|
selectOptionValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { records: relationRecords } = useFindManyRecords({
|
const { records: relationRecords } = useFindManyRecords({
|
||||||
|
|||||||
@ -37,7 +37,7 @@ describe('getFieldDefaultPreviewValue', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(result).toEqual(selectOptions[1]);
|
expect(result).toEqual(selectOptions[1].value);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the first select option if no default option was found', () => {
|
it('returns the first select option if no default option was found', () => {
|
||||||
@ -46,7 +46,7 @@ describe('getFieldDefaultPreviewValue', () => {
|
|||||||
const fieldMetadataItem = mockedCompanyObjectMetadataItem.fields.find(
|
const fieldMetadataItem = mockedCompanyObjectMetadataItem.fields.find(
|
||||||
({ name }) => name === 'industry',
|
({ name }) => name === 'industry',
|
||||||
)!;
|
)!;
|
||||||
const selectOptions = [
|
const selectOptions: SettingsObjectFieldSelectFormValues = [
|
||||||
{
|
{
|
||||||
color: 'purple' as const,
|
color: 'purple' as const,
|
||||||
label: '🏭 Industry',
|
label: '🏭 Industry',
|
||||||
@ -67,7 +67,7 @@ describe('getFieldDefaultPreviewValue', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(result).toEqual(selectOptions[0]);
|
expect(result).toEqual(selectOptions[0].value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,7 @@ describe('getFieldPreviewValueFromRecord', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(result).toEqual(selectOptions[2]);
|
expect(result).toEqual(selectOptions[2].value);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns undefined if the select option was not found', () => {
|
it('returns undefined if the select option was not found', () => {
|
||||||
|
|||||||
@ -26,7 +26,9 @@ export const getFieldDefaultPreviewValue = ({
|
|||||||
fieldMetadataItem.type === FieldMetadataType.Select &&
|
fieldMetadataItem.type === FieldMetadataType.Select &&
|
||||||
isDefined(selectOptions)
|
isDefined(selectOptions)
|
||||||
) {
|
) {
|
||||||
return selectOptions.find(({ isDefault }) => isDefault) || selectOptions[0];
|
const defaultSelectOption =
|
||||||
|
selectOptions.find(({ isDefault }) => isDefault) || selectOptions[0];
|
||||||
|
return defaultSelectOption.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Relation field
|
// Relation field
|
||||||
|
|||||||
@ -18,7 +18,7 @@ export const getFieldPreviewValueFromRecord = ({
|
|||||||
if (fieldMetadataItem.type === FieldMetadataType.Select) {
|
if (fieldMetadataItem.type === FieldMetadataType.Select) {
|
||||||
return selectOptions?.find(
|
return selectOptions?.find(
|
||||||
(selectOption) => selectOption.value === recordFieldValue,
|
(selectOption) => selectOption.value === recordFieldValue,
|
||||||
);
|
)?.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Relation fields (to many)
|
// Relation fields (to many)
|
||||||
|
|||||||
Reference in New Issue
Block a user