@ -29,6 +29,7 @@ export {
|
||||
IconChevronsRight,
|
||||
IconChevronUp,
|
||||
IconCircleDot,
|
||||
IconCoins,
|
||||
IconColorSwatch,
|
||||
IconMessageCircle as IconComment,
|
||||
IconCopy,
|
||||
|
||||
@ -6,6 +6,7 @@ import { useFieldInitialValue } from '../../hooks/useFieldInitialValue';
|
||||
import { entityFieldsFamilySelector } from '../../states/selectors/entityFieldsFamilySelector';
|
||||
import { assertFieldMetadata } from '../../types/guards/assertFieldMetadata';
|
||||
import { isFieldText } from '../../types/guards/isFieldText';
|
||||
import { isFieldTextValue } from '../../types/guards/isFieldTextValue';
|
||||
|
||||
export const useTextField = () => {
|
||||
const { entityId, fieldDefinition, hotkeyScope } = useContext(FieldContext);
|
||||
@ -20,16 +21,17 @@ export const useTextField = () => {
|
||||
fieldName: fieldName,
|
||||
}),
|
||||
);
|
||||
const fieldTextValue = isFieldTextValue(fieldValue) ? fieldValue : '';
|
||||
|
||||
const fieldInitialValue = useFieldInitialValue();
|
||||
|
||||
const initialValue = fieldInitialValue?.isEmpty
|
||||
? ''
|
||||
: fieldInitialValue?.value ?? fieldValue;
|
||||
: fieldInitialValue?.value ?? fieldTextValue;
|
||||
|
||||
return {
|
||||
fieldDefinition,
|
||||
fieldValue,
|
||||
fieldValue: fieldTextValue,
|
||||
initialValue,
|
||||
setFieldValue,
|
||||
hotkeyScope,
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
import { selectorFamily } from 'recoil';
|
||||
|
||||
import { assertNotNull } from '~/utils/assert';
|
||||
|
||||
import { FieldDefinition } from '../../types/FieldDefinition';
|
||||
import { FieldMetadata } from '../../types/FieldMetadata';
|
||||
import { isFieldBoolean } from '../../types/guards/isFieldBoolean';
|
||||
@ -8,6 +10,8 @@ import { isFieldDate } from '../../types/guards/isFieldDate';
|
||||
import { isFieldDoubleTextChip } from '../../types/guards/isFieldDoubleTextChip';
|
||||
import { isFieldEmail } from '../../types/guards/isFieldEmail';
|
||||
import { isFieldMoney } from '../../types/guards/isFieldMoney';
|
||||
import { isFieldMoneyAmountV2 } from '../../types/guards/isFieldMoneyAmountV2';
|
||||
import { isFieldMoneyAmountV2Value } from '../../types/guards/isFieldMoneyAmountV2Value';
|
||||
import { isFieldNumber } from '../../types/guards/isFieldNumber';
|
||||
import { isFieldPhone } from '../../types/guards/isFieldPhone';
|
||||
import { isFieldProbability } from '../../types/guards/isFieldProbability';
|
||||
@ -17,6 +21,8 @@ import { isFieldText } from '../../types/guards/isFieldText';
|
||||
import { isFieldURL } from '../../types/guards/isFieldURL';
|
||||
import { entityFieldsFamilyState } from '../entityFieldsFamilyState';
|
||||
|
||||
const isValueEmpty = (value: unknown) => !assertNotNull(value) || value === '';
|
||||
|
||||
export const isEntityFieldEmptyFamilySelector = selectorFamily({
|
||||
key: 'isEntityFieldEmptyFamilySelector',
|
||||
get: ({
|
||||
@ -44,32 +50,30 @@ export const isEntityFieldEmptyFamilySelector = selectorFamily({
|
||||
const fieldName = fieldDefinition.metadata.fieldName;
|
||||
const fieldValue = get(entityFieldsFamilyState(entityId))?.[
|
||||
fieldName
|
||||
] as string | null;
|
||||
] as string | number | boolean | null;
|
||||
|
||||
return (
|
||||
fieldValue === null || fieldValue === undefined || fieldValue === ''
|
||||
);
|
||||
} else if (isFieldRelation(fieldDefinition)) {
|
||||
return isValueEmpty(fieldValue);
|
||||
}
|
||||
|
||||
if (isFieldRelation(fieldDefinition)) {
|
||||
const fieldName = fieldDefinition.metadata.fieldName;
|
||||
|
||||
const fieldValue = get(entityFieldsFamilyState(entityId))?.[fieldName];
|
||||
|
||||
if (isFieldRelationValue(fieldValue)) {
|
||||
return fieldValue === null || fieldValue === undefined;
|
||||
}
|
||||
} else if (isFieldChip(fieldDefinition)) {
|
||||
return isFieldRelationValue(fieldValue) && isValueEmpty(fieldValue);
|
||||
}
|
||||
|
||||
if (isFieldChip(fieldDefinition)) {
|
||||
const contentFieldName = fieldDefinition.metadata.contentFieldName;
|
||||
|
||||
const contentFieldValue = get(entityFieldsFamilyState(entityId))?.[
|
||||
contentFieldName
|
||||
] as string | null;
|
||||
|
||||
return (
|
||||
contentFieldValue === null ||
|
||||
contentFieldValue === undefined ||
|
||||
contentFieldValue === ''
|
||||
);
|
||||
} else if (isFieldDoubleTextChip(fieldDefinition)) {
|
||||
return isValueEmpty(contentFieldValue);
|
||||
}
|
||||
|
||||
if (isFieldDoubleTextChip(fieldDefinition)) {
|
||||
const firstValueFieldName =
|
||||
fieldDefinition.metadata.firstValueFieldName;
|
||||
|
||||
@ -85,20 +89,24 @@ export const isEntityFieldEmptyFamilySelector = selectorFamily({
|
||||
)?.[secondValueFieldName] as string | null;
|
||||
|
||||
return (
|
||||
(contentFieldFirstValue === null ||
|
||||
contentFieldFirstValue === undefined ||
|
||||
contentFieldFirstValue === '') &&
|
||||
(contentFieldSecondValue === null ||
|
||||
contentFieldSecondValue === undefined ||
|
||||
contentFieldSecondValue === '')
|
||||
);
|
||||
} else {
|
||||
throw new Error(
|
||||
`Entity field type not supported in isEntityFieldEmptyFamilySelector : ${fieldDefinition.type}}`,
|
||||
isValueEmpty(contentFieldFirstValue) &&
|
||||
isValueEmpty(contentFieldSecondValue)
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
if (isFieldMoneyAmountV2(fieldDefinition)) {
|
||||
const fieldName = fieldDefinition.metadata.fieldName;
|
||||
const fieldValue = get(entityFieldsFamilyState(entityId))?.[fieldName];
|
||||
|
||||
return (
|
||||
!isFieldMoneyAmountV2Value(fieldValue) ||
|
||||
isValueEmpty(fieldValue?.amount)
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Entity field type not supported in isEntityFieldEmptyFamilySelector : ${fieldDefinition.type}}`,
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { FieldMoneyValue } from '../FieldMetadata';
|
||||
import { FieldMoneyAmountV2Value } from '../FieldMetadata';
|
||||
|
||||
const moneyAmountV2Schema = z.object({
|
||||
currency: z.string(),
|
||||
@ -9,5 +9,5 @@ const moneyAmountV2Schema = z.object({
|
||||
|
||||
export const isFieldMoneyAmountV2Value = (
|
||||
fieldValue: unknown,
|
||||
): fieldValue is FieldMoneyValue =>
|
||||
): fieldValue is FieldMoneyAmountV2Value =>
|
||||
moneyAmountV2Schema.safeParse(fieldValue).success;
|
||||
|
||||
Reference in New Issue
Block a user