Select.tsx 3.34 KB
import { useState, type ReactNode } from 'react';
import { Select as TDesignSelect } from 'tdesign-react';
import type {
  SelectOption as TDesignSelectOption,
  SelectValue,
} from 'tdesign-react/es/select/type';

export interface SelectOption {
  label: ReactNode;
  value: string;
  disabled?: boolean;
}

interface SelectBaseProps {
  options: SelectOption[];
  placeholder?: string;
  allowClear?: boolean;
  disabled?: boolean;
  className?: string;
  'aria-label'?: string;
}

export interface SelectSingleProps extends SelectBaseProps {
  multiple?: false;
  value?: string;
  onChange?: (value: string | undefined, option: SelectOption | undefined) => void;
}

export interface SelectMultipleProps extends SelectBaseProps {
  multiple: true;
  value?: string[];
  onChange?: (value: string[], option: SelectOption | undefined) => void;
}

export type SelectProps = SelectSingleProps | SelectMultipleProps;

function toTDesignOptions(options: SelectOption[]): TDesignSelectOption[] {
  return options.map((option) => ({
    label: option.label,
    value: option.value,
    disabled: option.disabled,
  }));
}

function normalizeSingleValue(value: SelectValue): string | undefined {
  if (Array.isArray(value)) return typeof value[0] === 'string' ? value[0] : undefined;
  return typeof value === 'string' ? value : undefined;
}

function normalizeMultipleValue(value: SelectValue): string[] {
  if (!Array.isArray(value)) return typeof value === 'string' ? [value] : [];
  return value.filter((item): item is string => typeof item === 'string');
}

export function Select(props: SelectProps) {
  const {
    allowClear,
    className,
    disabled,
    options,
    placeholder = '请选择',
    multiple,
    value,
    onChange,
    'aria-label': ariaLabel,
  } = props;
  const isControlled = Object.prototype.hasOwnProperty.call(props, 'value');
  const [innerValue, setInnerValue] = useState<string | string[] | undefined>(multiple ? [] : undefined);
  const currentValue = isControlled ? value : innerValue;
  const selectedOption = !multiple && typeof currentValue === 'string'
    ? options.find((item) => item.value === currentValue)
    : undefined;

  return (
    <TDesignSelect
      aria-label={ariaLabel}
      className={className}
      clearable={allowClear}
      disabled={disabled}
      inputProps={{
        'aria-disabled': disabled,
        'aria-label': ariaLabel,
        role: 'combobox',
      } as never}
      multiple={multiple}
      options={toTDesignOptions(options)}
      placeholder={placeholder}
      value={currentValue}
      valueDisplay={!multiple ? selectedOption?.label : undefined}
      onChange={(nextValue, context) => {
        const changedOption = context.option as { value?: unknown } | undefined;
        const option = context.option
          ? options.find((item) => item.value === String(changedOption?.value))
          : undefined;

        if (multiple) {
          const normalizedValue = normalizeMultipleValue(nextValue);
          if (!isControlled) setInnerValue(normalizedValue);
          (onChange as SelectMultipleProps['onChange'] | undefined)?.(normalizedValue, option);
          return;
        }

        const normalizedValue = normalizeSingleValue(nextValue);
        if (!isControlled) setInnerValue(normalizedValue);
        (onChange as SelectSingleProps['onChange'] | undefined)?.(normalizedValue, option);
      }}
    />
  );
}