Skip to content

The open alternative to Salesforce, designed for AI.

6.5 KB
Raw Blame
import { type FormFieldInputVariant } from '@/ui/input/types/FormFieldInputVariant';
import { type SelectSizeVariant } from '@/ui/input/types/SelectSizeVariant';
import { getFormFieldBorderColor } from '@/ui/input/utils/getFormFieldBorderColor';
import { styled } from '@linaria/react';
import { type ReactNode } from 'react';
import { isDefined } from 'twenty-shared/utils';
import { TintedIconTile } from 'twenty-ui/components/data-display';
import { IconChevronDown } from 'twenty-ui/icon';
import { Tag } from 'twenty-ui/primitives/data-display';
import { type SelectOption } from 'twenty-ui/primitives/input';
import { OverflowingTextWithTooltip } from 'twenty-ui/primitives/typography';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';

export type SelectControlTextAccent = 'default' | 'placeholder';

// TODO: factorize this with https://github.com/twentyhq/core-team-issues/issues/752
export const StyledControlContainer = styled.div<{
  disabled?: boolean;
  hasIcon: boolean;
  selectSizeVariant?: SelectSizeVariant;
  textAccent: SelectControlTextAccent;
  hasRightElement?: boolean;
  isFocused?: boolean;
  $variant?: FormFieldInputVariant;
}>`
  align-items: center;
  background-color: ${({ $variant }) =>
    $variant === 'transparent'
      ? 'transparent'
      : themeCssVariables.background.transparent.lighter};
  border: ${({ $variant }) =>
    $variant === 'transparent'
      ? 'none'
      : `1px solid ${themeCssVariables.border.color.medium}`};
  border-bottom-left-radius: ${({ $variant }) =>
    $variant === 'transparent' ? '0' : themeCssVariables.border.radius.md};
  border-bottom-right-radius: ${({ hasRightElement, $variant }) =>
    $variant === 'transparent'
      ? '0'
      : hasRightElement
        ? '0'
        : themeCssVariables.border.radius.md};
  border-color: ${({ isFocused, hasRightElement, $variant }) =>
    getFormFieldBorderColor({ isFocused, hasRightElement, variant: $variant })};
  border-right: ${({ isFocused, hasRightElement, $variant }) =>
    $variant === 'transparent'
      ? 'none'
      : hasRightElement
        ? 'none'
        : `1px solid ${getFormFieldBorderColor({
            isFocused,
            hasRightElement,
            variant: $variant,
          })}`};
  border-top-left-radius: ${({ $variant }) =>
    $variant === 'transparent' ? '0' : themeCssVariables.border.radius.md};
  border-top-right-radius: ${({ hasRightElement, $variant }) =>
    $variant === 'transparent'
      ? '0'
      : hasRightElement
        ? '0'
        : themeCssVariables.border.radius.md};
  box-sizing: border-box;
  color: ${({ disabled, textAccent }) =>
    disabled
      ? themeCssVariables.font.color.tertiary
      : textAccent === 'default'
        ? themeCssVariables.font.color.primary
        : themeCssVariables.font.color.light};
  cursor: ${({ disabled }) => (disabled ? 'not-allowed' : 'pointer')};
  display: grid;
  font-weight: ${({ textAccent }) =>
    textAccent === 'placeholder'
      ? themeCssVariables.font.weight.medium
      : 'inherit'};

  gap: ${themeCssVariables.spacing[1]};
  grid-template-columns: ${({ hasIcon }) =>
    hasIcon ? 'auto 1fr auto' : '1fr auto'};
  height: ${({ selectSizeVariant, $variant }) =>
    $variant === 'transparent'
      ? themeCssVariables.spacing[6]
      : selectSizeVariant === 'small'
        ? themeCssVariables.spacing[6]
        : themeCssVariables.spacing[8]};

  max-width: 100%;
  padding: ${({ $variant }) =>
    $variant === 'transparent' ? '0' : `0 ${themeCssVariables.spacing[2]}`};
  text-align: left;
`;

const StyledLeadingContent = styled.div`
  align-items: center;
  display: flex;
  gap: ${themeCssVariables.spacing[1]};
`;

const StyledIconChevronDownWrapper = styled.div<{
  disabled?: boolean;
}>`
  color: ${({ disabled }) =>
    disabled
      ? themeCssVariables.font.color.extraLight
      : themeCssVariables.font.color.tertiary};
  display: flex;
`;

const StyledTagContainer = styled.div`
  display: flex;
  min-width: 0;
  overflow: hidden;
`;

export type SelectControlProps = {
  renderAsTag?: boolean;
  selectedOption: SelectOption<string | number | boolean | null>;
  LeftComponent?: ReactNode;
  isDisabled?: boolean;
  selectSizeVariant?: SelectSizeVariant;
  textAccent?: SelectControlTextAccent;
  hasRightElement?: boolean;
  isFocused?: boolean;
  variant?: FormFieldInputVariant;
};

export const SelectControl = ({
  selectedOption,
  renderAsTag = false,
  LeftComponent,
  isDisabled,
  selectSizeVariant,
  textAccent = 'default',
  hasRightElement,
  isFocused = false,
  variant = 'default',
}: SelectControlProps) => {
  const theme = useTheme();
  const hasLeadingContent =
    isDefined(LeftComponent) ||
    isDefined(selectedOption.Icon) ||
    isDefined(selectedOption.LeftComponent);
  return (
    <StyledControlContainer
      disabled={isDisabled}
      hasIcon={hasLeadingContent}
      selectSizeVariant={selectSizeVariant}
      textAccent={textAccent}
      hasRightElement={hasRightElement}
      isFocused={isFocused}
      $variant={variant}
      title={selectedOption.fullLabel}
    >
      {isDefined(LeftComponent) ? (
        LeftComponent
      ) : hasLeadingContent ? (
        <StyledLeadingContent>
          {isDefined(selectedOption?.Icon) ? (
            isDefined(selectedOption.iconThemeColor) ? (
              <TintedIconTile
                icon={
                  <selectedOption.Icon
                    size={theme.icon.size.md}
                    stroke={theme.icon.stroke.sm}
                  />
                }
                color={selectedOption.iconThemeColor}
              />
            ) : (
              <selectedOption.Icon
                color={
                  isDisabled ? theme.font.color.light : theme.font.color.primary
                }
                size={theme.icon.size.md}
                stroke={theme.icon.stroke.sm}
              />
            )
          ) : null}
          {selectedOption.LeftComponent}
        </StyledLeadingContent>
      ) : null}
      {renderAsTag && isDefined(selectedOption.color) ? (
        <StyledTagContainer>
          <Tag color={selectedOption.color}>{selectedOption.label}</Tag>
        </StyledTagContainer>
      ) : (
        <OverflowingTextWithTooltip
          text={
            selectedOption.contextualText
              ? `${selectedOption.label} · ${selectedOption.contextualText}`
              : selectedOption.label
          }
        />
      )}
      <StyledIconChevronDownWrapper disabled={isDisabled}>
        <IconChevronDown size={theme.icon.size.md} />
      </StyledIconChevronDownWrapper>
    </StyledControlContainer>
  );
};