The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { SelectOptionIcon } from '@/ui/input/components/SelectOptionIcon';
import { type SelectProps } from '@/ui/input/types/SelectProps';
import { DropdownRoot } from '@/ui/layout/dropdown/components/DropdownRoot';
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
import { GenericDropdownContentWidth } from '@/ui/layout/dropdown/constants/GenericDropdownContentWidth';
import { hasFullFormFieldBorder } from '@/ui/input/utils/hasFullFormFieldBorder';
import { styled } from '@linaria/react';
import { useMemo, useRef, useState } from 'react';
import { Dropdown } from 'twenty-ui/components/navigation';
import { Tag } from 'twenty-ui/primitives/data-display';
import { type SelectValue } from '@/ui/input/components/internal/select/types';
import { getSelectOptionIdentifier } from '@/ui/input/components/internal/select/utils/getSelectOptionIdentifier';
import { isFocusMovingWithinSelect } from '@/ui/input/components/internal/select/utils/isFocusMovingWithinSelect';
import { isSelectOptionMatchingSearch } from '@/ui/input/components/internal/select/utils/isSelectOptionMatchingSearch';
import { SelectControl } from '@/ui/input/components/SelectControl';
import { isNonEmptyArray, isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';
import { themeCssVariables } from 'twenty-ui/theme';
import { normalizeSearchText } from 'twenty-ui/utilities';
import { useLingui } from '@lingui/react/macro';
const StyledContainer = styled.div<{ fullWidth?: boolean }>`
width: ${({ fullWidth }) => (fullWidth ? '100%' : 'auto')};
`;
const StyledSelectTrigger = styled.div<{ hasOutline: boolean }>`
outline: ${({ hasOutline }) => (hasOutline ? 'revert' : 'none')};
`;
const StyledLabel = styled.span`
color: ${themeCssVariables.font.color.light};
display: block;
font-size: ${themeCssVariables.font.size.xs};
font-weight: ${themeCssVariables.font.weight.semiBold};
margin-bottom: ${themeCssVariables.spacing[1]};
`;
const StyledDescription = styled.span`
color: ${themeCssVariables.font.color.light};
font-size: ${themeCssVariables.font.size.sm};
`;
export const Select = <TValue extends SelectValue>({
className,
disabled: disabledFromProps,
selectSizeVariant,
dropdownId,
dropdownWidth = GenericDropdownContentWidth.Medium,
dropdownWidthAuto = false,
emptyOption,
fullWidth,
label,
description,
onChange,
onBlur,
options,
value,
withSearchInput,
needIconCheck,
pinnedOption,
callToActionButton,
dropdownSideOffset = 0,
hasRightElement,
showContextualTextInControl = true,
showIconInControl = true,
variant = 'default',
renderAsTag = false,
}: SelectProps<TValue>) => {
const { t } = useLingui();
const dropdownContentRef = useRef<HTMLDivElement>(null);
const [searchInputValue, setSearchInputValue] = useState('');
const [isTriggerFocused, setIsTriggerFocused] = useState(false);
const selectedOption = useMemo(() => {
if (isDefined(pinnedOption) && pinnedOption.value === value) {
return pinnedOption;
}
const fromMatchingOption = options.find(
({ value: optionValue }) => optionValue === value,
);
if (isDefined(fromMatchingOption)) {
return fromMatchingOption;
}
if (isDefined(emptyOption)) {
return emptyOption;
}
if (isNonEmptyArray(options)) {
return options[0];
}
return null;
}, [emptyOption, options, pinnedOption, value]);
const normalizedSearchInputValue = normalizeSearchText(searchInputValue);
const filteredOptions = useMemo(
() =>
options.filter((option) =>
isSelectOptionMatchingSearch({ option, normalizedSearchInputValue }),
),
[options, normalizedSearchInputValue],
);
const isDisabled =
disabledFromProps ||
(options.length <= 1 &&
!isDefined(pinnedOption) &&
!isDefined(callToActionButton) &&
(!isDefined(emptyOption) || selectedOption !== emptyOption));
const shouldShowPinnedOption =
isDefined(pinnedOption) &&
isSelectOptionMatchingSearch({
option: pinnedOption,
normalizedSearchInputValue,
});
const controlSelectedOption = useMemo(() => {
if (!isDefined(selectedOption)) {
return selectedOption;
}
return {
...selectedOption,
contextualText: showContextualTextInControl
? selectedOption.contextualText
: undefined,
Icon: showIconInControl ? selectedOption.Icon : undefined,
};
}, [selectedOption, showContextualTextInControl, showIconInControl]);
const isShowingEmptyPrompt =
isDefined(emptyOption) &&
selectedOption === emptyOption &&
!isDefined(emptyOption.Icon);
if (!isDefined(controlSelectedOption)) {
return <></>;
}
const selectableOptions = isDefined(pinnedOption)
? [pinnedOption, ...options]
: options;
const isControlOptionSelected = controlSelectedOption.value === value;
return (
<StyledContainer
className={className}
fullWidth={fullWidth}
onBlur={(event) => {
if (
isFocusMovingWithinSelect({
event,
dropdownContent: dropdownContentRef.current,
})
) {
return;
}
onBlur?.();
}}
>
{isNonEmptyString(label) && <StyledLabel>{label}</StyledLabel>}
{isDisabled ? (
<SelectControl
renderAsTag={renderAsTag}
selectedOption={controlSelectedOption}
textAccent={isShowingEmptyPrompt ? 'placeholder' : 'default'}
isDisabled={isDisabled}
selectSizeVariant={selectSizeVariant}
hasRightElement={hasRightElement}
variant={variant}
/>
) : (
<DropdownRoot
dropdownId={dropdownId}
type="picker"
onOpenChange={(open) => {
if (open) {
setSearchInputValue('');
}
}}
>
<Dropdown.Trigger
render={
<StyledSelectTrigger
hasOutline={
!hasFullFormFieldBorder({ hasRightElement, variant })
}
/>
}
nativeButton={false}
onFocus={(event) =>
setIsTriggerFocused(
event.target === event.currentTarget &&
event.currentTarget.matches(':focus-visible'),
)
}
onBlur={() => setIsTriggerFocused(false)}
>
<SelectControl
renderAsTag={renderAsTag}
selectedOption={controlSelectedOption}
textAccent={isShowingEmptyPrompt ? 'placeholder' : 'default'}
isDisabled={isDisabled}
selectSizeVariant={selectSizeVariant}
hasRightElement={hasRightElement}
isFocused={isTriggerFocused}
variant={variant}
/>
</Dropdown.Trigger>
<DropdownContent
ref={dropdownContentRef}
width={dropdownWidthAuto ? 'var(--anchor-width)' : dropdownWidth}
align="start"
sideOffset={dropdownSideOffset}
aria-label={isNonEmptyString(label) ? label : undefined}
>
{withSearchInput === true && (
<Dropdown.Search
value={searchInputValue}
onValueChange={setSearchInputValue}
placeholder={t`Search`}
aria-label={t`Search`}
/>
)}
{withSearchInput === true && isNonEmptyArray(filteredOptions) && (
<Dropdown.Separator />
)}
<Dropdown.SelectionGroup
aria-label={isNonEmptyString(label) ? label : t`Options`}
value={
isControlOptionSelected
? getSelectOptionIdentifier(controlSelectedOption.value)
: null
}
onValueChange={(selectedIdentifier) => {
const option = selectableOptions.find(
({ value: optionValue }) =>
getSelectOptionIdentifier(optionValue) ===
selectedIdentifier,
);
if (!isDefined(option)) {
return;
}
onChange?.(option.value);
onBlur?.();
}}
>
{shouldShowPinnedOption && (
<Dropdown.Section>
<Dropdown.OptionItem
value={getSelectOptionIdentifier(pinnedOption.value)}
disabled={pinnedOption.disabled}
indicator={needIconCheck ? 'check' : 'none'}
description={pinnedOption.contextualText}
startIcon={
<>
<SelectOptionIcon
Icon={pinnedOption.Icon}
color={pinnedOption.iconThemeColor}
/>
{pinnedOption.LeftComponent}
</>
}
>
{pinnedOption.label}
</Dropdown.OptionItem>
</Dropdown.Section>
)}
{shouldShowPinnedOption && isNonEmptyArray(filteredOptions) && (
<Dropdown.Separator />
)}
{isNonEmptyArray(filteredOptions) && (
<Dropdown.Section scrollable>
{filteredOptions.map((option) => {
const tagColor = renderAsTag ? option.color : undefined;
const isColoredTag = isDefined(tagColor);
return (
<Dropdown.OptionItem
key={`${option.value}-${option.label}`}
value={getSelectOptionIdentifier(option.value)}
disabled={option.disabled}
indicator={
isColoredTag || needIconCheck ? 'check' : 'none'
}
description={
isColoredTag ? undefined : option.contextualText
}
startIcon={
isColoredTag ? undefined : (
<>
<SelectOptionIcon
Icon={option.Icon}
color={option.iconThemeColor}
/>
{option.LeftComponent}
</>
)
}
>
{isColoredTag ? (
<Tag color={tagColor}>{option.label}</Tag>
) : (
option.label
)}
</Dropdown.OptionItem>
);
})}
</Dropdown.Section>
)}
</Dropdown.SelectionGroup>
{isDefined(callToActionButton) &&
isNonEmptyArray(filteredOptions) && <Dropdown.Separator />}
{isDefined(callToActionButton) && (
<Dropdown.Section>
<Dropdown.ActionItem
onClick={callToActionButton.onClick}
closeOnClick={false}
startIcon={
<SelectOptionIcon Icon={callToActionButton.Icon} />
}
>
{callToActionButton.text}
</Dropdown.ActionItem>
</Dropdown.Section>
)}
</DropdownContent>
</DropdownRoot>
)}
{isNonEmptyString(description) && (
<StyledDescription>{description}</StyledDescription>
)}
</StyledContainer>
);
};