Skip to content

The open alternative to Salesforce, designed for AI.

fix(workflow): hide empty option for non-nullable select fields (#21075)

Brendan Erofeev1b3d9339001e
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/components/FormFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/components/FormFieldInput.tsx
index af8aa580909..8c1e73b62be 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/components/FormFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/components/FormFieldInput.tsx
@@ -121,6 +121,7 @@ export const FormFieldInput = ({
VariablePicker={VariablePicker}
options={field.metadata?.options}
readonly={readonly}
+ isNullable={field.metadata.isNullable}
/>
) : isFieldFullName(field) ? (
<FormFullNameFieldInput
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormSelectFieldInput.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormSelectFieldInput.tsx
index 6b189c5ed01..e8149052b15 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormSelectFieldInput.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/FormSelectFieldInput.tsx
@@ -13,6 +13,7 @@ import { useHotkeysOnFocusedElement } from '@/ui/utilities/hotkey/hooks/useHotke
import { isStandaloneVariableString } from '@/workflow/utils/isStandaloneVariableString';
import { useContext, useId, useState } from 'react';
import { Key } from 'ts-key-enum';
+import { isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';
import { IconCircleOff } from 'twenty-ui/icon';
import { type SelectOption } from 'twenty-ui/input';
@@ -26,6 +27,7 @@ type FormSelectFieldInputProps = {
VariablePicker?: VariablePickerComponent;
options: SelectOption[];
readonly?: boolean;
+ isNullable?: boolean;
callToActionButton?: CallToActionButton;
};
@@ -37,6 +39,7 @@ export const FormSelectFieldInput = ({
VariablePicker,
options,
readonly,
+ isNullable,
callToActionButton,
}: FormSelectFieldInputProps) => {
const { theme } = useContext(ThemeContext);
@@ -68,16 +71,16 @@ export const FormSelectFieldInput = ({
},
);
- const onSelect = (option: string) => {
+ const onSelect = (selectedValue: string) => {
setDraftValue({
type: 'static',
- value: option,
+ value: selectedValue,
editingMode: 'view',
});
removeFocusItemFromFocusStackById({ focusId: instanceId });
- onChange(option);
+ onChange(isNonEmptyString(selectedValue) ? selectedValue : null);
};
const onCancel = () => {
@@ -93,16 +96,20 @@ export const FormSelectFieldInput = ({
removeFocusItemFromFocusStackById({ focusId: instanceId });
};
- const selectedOption = options.find(
- (option) => option.value === draftValue.value,
- );
-
- const defaultEmptyOption = {
+ const emptyOption: SelectOption = {
label: label ? t`No ${label}` : t`No value`,
value: '',
- icon: IconCircleOff,
+ Icon: IconCircleOff,
};
+ const optionsWithEmptyOption = isNullable
+ ? [emptyOption, ...options]
+ : options;
+
+ const selectedOption = optionsWithEmptyOption.find(
+ (option) => option.value === draftValue.value,
+ );
+
const handleUnlinkVariable = () => {
setDraftValue({
type: 'static',
@@ -137,10 +144,9 @@ export const FormSelectFieldInput = ({
{draftValue.type === 'static' ? (
<Select
dropdownId={`${instanceId}-select-display`}
- options={options}
+ options={optionsWithEmptyOption}
value={selectedOption?.value}
onChange={onSelect}
- emptyOption={defaultEmptyOption}
callToActionButton={callToActionButton}
fullWidth
hasRightElement={isDefined(VariablePicker) && !readonly}
diff --git a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__stories__/FormSelectFieldInput.stories.tsx b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__stories__/FormSelectFieldInput.stories.tsx
index aa47dafa41e..b10ea34429c 100644
--- a/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__stories__/FormSelectFieldInput.stories.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-field/ui/form-types/components/__stories__/FormSelectFieldInput.stories.tsx
@@ -1,6 +1,6 @@
import { FormSelectFieldInput } from '@/object-record/record-field/ui/form-types/components/FormSelectFieldInput';
import { type Meta, type StoryObj } from '@storybook/react-vite';
-import { expect, fn, userEvent, within } from 'storybook/test';
+import { expect, fn, userEvent, waitFor, within } from 'storybook/test';
import { WorkflowStepDecorator } from '~/testing/decorators/WorkflowStepDecorator';
import { MOCKED_STEP_ID } from '~/testing/mock-data/workflow';
@@ -46,9 +46,83 @@ export const Default: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
- const selectedOption = await canvas.findByText('Work Policy');
+ const label = await canvas.findByText('Work Policy');
+ expect(label).toBeVisible();
- expect(selectedOption).toBeVisible();
+ const selectControl = await canvas.findByText('Work Policy 1');
+ expect(selectControl).toBeVisible();
+ },
+};
+
+export const Nullable: Story = {
+ args: {
+ label: 'Work Policy',
+ defaultValue: 'WORK_POLICY_1',
+ options: [
+ {
+ label: 'Work Policy 1',
+ value: 'WORK_POLICY_1',
+ color: 'blue',
+ },
+ {
+ label: 'Work Policy 2',
+ value: 'WORK_POLICY_2',
+ color: 'green',
+ },
+ ],
+ onChange: fn(),
+ isNullable: true,
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ const selectControl = await canvas.findByText('Work Policy 1');
+ await userEvent.click(selectControl);
+
+ const dropdown = within(canvasElement.ownerDocument.body);
+
+ await waitFor(() => {
+ expect(dropdown.getByText('No Work Policy')).toBeVisible();
+ });
+
+ await userEvent.click(dropdown.getByText('No Work Policy'));
+
+ expect(args.onChange).toHaveBeenCalledWith(null);
+ },
+};
+
+export const NonNullable: Story = {
+ args: {
+ label: 'Work Policy',
+ defaultValue: 'WORK_POLICY_1',
+ options: [
+ {
+ label: 'Work Policy 1',
+ value: 'WORK_POLICY_1',
+ color: 'blue',
+ },
+ {
+ label: 'Work Policy 2',
+ value: 'WORK_POLICY_2',
+ color: 'green',
+ },
+ ],
+ onChange: fn(),
+ isNullable: false,
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ const selectControl = await canvas.findByText('Work Policy 1');
+ await userEvent.click(selectControl);
+
+ const dropdown = within(canvasElement.ownerDocument.body);
+
+ await waitFor(() => {
+ expect(dropdown.getByText('Work Policy 2')).toBeVisible();
+ });
+
+ expect(dropdown.queryByText('No Work Policy')).not.toBeInTheDocument();
},
};