Skip to content

Commit

Permalink
feature to reset value in select field (#6067)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudarsh1010 and lucasbordeau authored Jul 2, 2024
1 parent 8891529 commit ea7d52f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useContext } from 'react';
import { useRecoilCallback } from 'recoil';

import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
import { useSetRecordFieldValue } from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext';
import { recordStoreFamilySelector } from '@/object-record/record-store/states/selectors/recordStoreFamilySelector';
import { generateEmptyFieldValue } from '@/object-record/utils/generateEmptyFieldValue';

Expand All @@ -16,6 +17,8 @@ export const useClearField = () => {

const [updateRecord] = useUpdateRecord();

const setRecordFieldValue = useSetRecordFieldValue();

const clearField = useRecoilCallback(
({ snapshot, set }) =>
() => {
Expand Down Expand Up @@ -46,6 +49,8 @@ export const useClearField = () => {
emptyFieldValue,
);

setRecordFieldValue(entityId, fieldName, emptyFieldValue);

updateRecord?.({
variables: {
where: { id: entityId },
Expand All @@ -55,7 +60,7 @@ export const useClearField = () => {
},
});
},
[entityId, fieldDefinition, updateRecord],
[entityId, fieldDefinition, updateRecord, setRecordFieldValue],
);

return clearField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useRef, useState } from 'react';
import styled from '@emotion/styled';
import { Key } from 'ts-key-enum';

import { useClearField } from '@/object-record/record-field/hooks/useClearField';
import { useSelectField } from '@/object-record/record-field/meta-types/hooks/useSelectField';
import { FieldInputEvent } from '@/object-record/record-field/types/FieldInputEvent';
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu';
Expand Down Expand Up @@ -30,23 +31,33 @@ export const SelectFieldInput = ({
}: SelectFieldInputProps) => {
const { persistField, fieldDefinition, fieldValue, hotkeyScope } =
useSelectField();
const clearField = useClearField();

const [searchFilter, setSearchFilter] = useState('');
const containerRef = useRef<HTMLDivElement>(null);

const selectedOption = fieldDefinition.metadata.options.find(
(option) => option.value === fieldValue,
);

const optionsToSelect =
fieldDefinition.metadata.options.filter((option) => {
return (
option.value !== fieldValue &&
option.label.toLowerCase().includes(searchFilter.toLowerCase())
);
}) || [];

const optionsInDropDown = selectedOption
? [selectedOption, ...optionsToSelect]
: optionsToSelect;

// handlers
const handleClearField = () => {
clearField();
onCancel?.();
};

useListenClickOutside({
refs: [containerRef],
callback: (event) => {
Expand Down Expand Up @@ -85,7 +96,17 @@ export const SelectFieldInput = ({
autoFocus
/>
<DropdownMenuSeparator />

<DropdownMenuItemsContainer hasMaxHeight>
<MenuItemSelectTag
key={`No ${fieldDefinition.label}`}
selected={false}
text={`No ${fieldDefinition.label}`}
color="transparent"
variant="outline"
onClick={handleClearField}
/>

{optionsInDropDown.map((option) => {
return (
<MenuItemSelectTag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ type MenuItemSelectTagProps = {
selected: boolean;
className?: string;
onClick?: () => void;
color: ThemeColor;
color: ThemeColor | 'transparent';
text: string;
variant?: 'solid' | 'outline';
};

export const MenuItemSelectTag = ({
Expand All @@ -19,6 +20,7 @@ export const MenuItemSelectTag = ({
className,
onClick,
text,
variant = 'solid',
}: MenuItemSelectTagProps) => {
const theme = useTheme();

Expand All @@ -29,7 +31,7 @@ export const MenuItemSelectTag = ({
selected={selected}
>
<StyledMenuItemLeftContent>
<Tag color={color} text={text} />
<Tag variant={variant} color={color} text={text} />
</StyledMenuItemLeftContent>
{selected && <IconCheck size={theme.icon.size.sm} />}
</StyledMenuItemSelect>
Expand Down
18 changes: 14 additions & 4 deletions packages/twenty-ui/src/display/tag/components/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ const spacing1 = THEME_COMMON.spacing(1);

const StyledTag = styled.h3<{
theme: ThemeType;
color: ThemeColor;
color: TagColor;
weight: TagWeight;
variant: TagVariant;
preventShrink?: boolean;
}>`
align-items: center;
background: ${({ color, theme }) => theme.tag.background[color]};
background: ${({ color, theme }) =>
color === 'transparent' ? color : theme.tag.background[color]};
border-radius: ${BORDER_COMMON.radius.sm};
color: ${({ color, theme }) => theme.tag.text[color]};
color: ${({ color, theme }) =>
color === 'transparent' ? theme.tag.text['gray'] : theme.tag.text[color]};
display: inline-flex;
font-size: ${({ theme }) => theme.font.size.md};
font-style: normal;
Expand All @@ -35,6 +38,8 @@ const StyledTag = styled.h3<{
margin: 0;
overflow: hidden;
padding: 0 ${spacing2};
border: ${({ variant, theme }) =>
variant === 'outline' ? `2px dashed ${theme.tag.background['gray']}` : ''};
gap: ${spacing1};
Expand All @@ -58,14 +63,17 @@ const StyledIconContainer = styled.div`
`;

type TagWeight = 'regular' | 'medium';
type TagVariant = 'solid' | 'outline';
type TagColor = ThemeColor | 'transparent';

type TagProps = {
className?: string;
color: ThemeColor;
color: TagColor;
text: string;
Icon?: IconComponent;
onClick?: () => void;
weight?: TagWeight;
variant?: TagVariant;
preventShrink?: boolean;
};

Expand All @@ -77,6 +85,7 @@ export const Tag = ({
Icon,
onClick,
weight = 'regular',
variant = 'solid',
preventShrink,
}: TagProps) => {
const { theme } = useContext(ThemeContext);
Expand All @@ -88,6 +97,7 @@ export const Tag = ({
color={color}
onClick={onClick}
weight={weight}
variant={variant}
preventShrink={preventShrink}
>
{!!Icon && (
Expand Down

0 comments on commit ea7d52f

Please sign in to comment.