Skip to content

The open alternative to Salesforce, designed for AI.

Follow up on FieldInput fix (#16611)

Charles Bocheta60f750ed716
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/ArrayFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/ArrayFieldInput.tsx
index 7a5696edebe..d7b15d35ec4 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/ArrayFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/ArrayFieldInput.tsx
@@ -22,12 +22,13 @@ export const ArrayFieldInput = () => {
);
const parseStringArrayToArrayValue = (arrayItems: string[]) => {
const parseResponse = arraySchema.safeParse(arrayItems);
+
if (parseResponse.success) {
return parseResponse.data;
}
};
- const handleChange = (newValue: any[]) => {
+ const handleChange = (newValue: string[]) => {
if (!isDefined(newValue)) setDraftValue(null);
const nextValue = parseStringArrayToArrayValue(newValue);
@@ -38,10 +39,13 @@ export const ArrayFieldInput = () => {
};
const handleClickOutside = (
- _newValue: any,
+ newValue: string[],
event: MouseEvent | TouchEvent,
) => {
- onClickOutside?.({ newValue: draftValue, event });
+ onClickOutside?.({
+ newValue: parseStringArrayToArrayValue(newValue),
+ event,
+ });
};
const handleEscape = (newValue: string[]) => {
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/MultiItemFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/MultiItemFieldInput.tsx
index ce7e5a12437..9f757e90210 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/MultiItemFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/MultiItemFieldInput.tsx
@@ -87,6 +87,12 @@ export const MultiItemFieldInput = <T,>({
) {
return;
}
+ const { isValid } = validateInputAndComputeUpdatedItems();
+
+ if (!isValid && isInputDisplayed) {
+ return;
+ }
+
handleSubmitChanges();
onClickOutside(items, event);
},
@@ -172,21 +178,34 @@ export const MultiItemFieldInput = <T,>({
setIsInputDisplayed(true);
};
- const handleAutoEnter = () => {
- const sanitizedInput = inputValue.trim();
+ const handleEnter = () => {
+ const { isValid, updatedItems } = validateInputAndComputeUpdatedItems();
+ if (!isValid) {
+ return;
+ }
- const newItem = formatInput
- ? formatInput(sanitizedInput)
- : (sanitizedInput as unknown as T);
+ handleSubmitChanges();
+ if (shouldAutoEnterBecauseOnlyOneItemIsAllowed) {
+ onEnter(updatedItems);
+ }
+ setIsInputDisplayed(false);
+ setIsAddingNewItem(false);
+ setInputValue('');
+ };
- const updatedItems = isAddingNewItem
- ? [...items, newItem]
- : toSpliced(items, itemToEditIndex, 1, newItem);
+ const handleSubmitChanges = () => {
+ const { isValid, updatedItems } = validateInputAndComputeUpdatedItems();
+ if (!isValid) {
+ return;
+ }
- onEnter(updatedItems);
+ onChange(updatedItems);
};
- const handleSubmitChanges = () => {
+ const validateInputAndComputeUpdatedItems = (): {
+ isValid: boolean;
+ updatedItems: T[];
+ } => {
const sanitizedInput = inputValue.trim();
const newItem = formatInput
@@ -194,17 +213,22 @@ export const MultiItemFieldInput = <T,>({
: (sanitizedInput as unknown as T);
if (sanitizedInput === '' && isAddingNewItem) {
- return;
+ return { isValid: true, updatedItems: items };
}
if (sanitizedInput === '' && shouldAutoEnterBecauseOnlyOneItemIsAllowed) {
- onEnter([newItem]);
- return;
+ return {
+ isValid: true,
+ updatedItems: [],
+ };
}
if (sanitizedInput === '' && !isAddingNewItem) {
handleDeleteItem(itemToEditIndex);
- return;
+ return {
+ isValid: true,
+ updatedItems: toSpliced(items, itemToEditIndex, 1),
+ };
}
if (validateInput !== undefined) {
@@ -212,17 +236,16 @@ export const MultiItemFieldInput = <T,>({
if (!validationData.isValid) {
onError?.(true, items);
setErrorData(validationData);
- return;
+ return { isValid: false, updatedItems: items };
}
}
- const updatedItems = isAddingNewItem
- ? [...items, newItem]
- : toSpliced(items, itemToEditIndex, 1, newItem);
-
- onChange(updatedItems);
- setIsAddingNewItem(false);
- setIsInputDisplayed(false);
+ return {
+ isValid: true,
+ updatedItems: isAddingNewItem
+ ? [...items, newItem]
+ : toSpliced(items, itemToEditIndex, 1, newItem),
+ };
};
const handleSetPrimaryItem = (index: number) => {
@@ -285,23 +308,13 @@ export const MultiItemFieldInput = <T,>({
? handleInputChange(turnIntoEmptyStringIfWhitespacesOnly(value))
: handleInputChange('');
}}
- onEnter={() => {
- handleSubmitChanges();
- if (shouldAutoEnterBecauseOnlyOneItemIsAllowed) {
- handleAutoEnter();
- }
- }}
+ onEnter={handleEnter}
hasItem={!!items.length}
rightComponent={
items.length ? (
<LightIconButton
Icon={isAddingNewItem ? IconPlus : IconCheck}
- onClick={() => {
- handleSubmitChanges();
- if (shouldAutoEnterBecauseOnlyOneItemIsAllowed) {
- handleAutoEnter();
- }
- }}
+ onClick={handleEnter}
/>
) : null
}