Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

6.1 KB
Raw Blame
import type { ReactNode } from 'react'
import { cn } from '@langgenius/dify-ui/cn'
import {
  Select,
  SelectItem,
  SelectItemIndicator,
  SelectItemText,
  SelectList,
  SelectPopup,
  SelectPortal,
  SelectPositioner,
  SelectTrigger,
} from '@langgenius/dify-ui/select'
import { useTranslation } from 'react-i18next'

type ItemValue = number | string

export type Item<T extends ItemValue = ItemValue> = {
  value: T
  name: string
  triggerName?: string
} & Record<string, unknown>

type Props<T extends ItemValue> = {
  id?: string
  disabled?: boolean
  label?: string
  'aria-labelledby'?: string
  className?: string
  panelClassName?: string
  showLeftIcon?: boolean
  leftIcon?: ReactNode
  showItemIndicator?: boolean
  value: T
  items: Item<T>[]
  onSelect: (item: Item<T>) => void
  onClear: () => void
}

function Chip<T extends ItemValue>({
  id,
  disabled = false,
  label,
  'aria-labelledby': ariaLabelledBy,
  className,
  panelClassName,
  showLeftIcon = true,
  leftIcon,
  showItemIndicator = true,
  value,
  items,
  onSelect,
  onClear,
}: Props<T>) {
  const { t } = useTranslation(['common'])
  const selectedItem = items.find((item) => Object.is(item.value, value))
  const triggerContent = selectedItem?.triggerName || selectedItem?.name || ''
  const triggerLabel = [label, triggerContent].filter(Boolean).join(' ')
  const hasValue = selectedItem !== undefined && value !== ''
  const clearLabel = triggerLabel
    ? `${t(($) => $['operation.clear'], { ns: 'common' })} ${triggerLabel}`
    : t(($) => $['operation.clear'], { ns: 'common' })

  return (
    <Select<T>
      disabled={disabled}
      value={selectedItem?.value ?? null}
      itemToStringLabel={(itemValue: T) =>
        items.find((item) => Object.is(item.value, itemValue))?.name ?? ''
      }
      itemToStringValue={(itemValue) => String(itemValue)}
      onValueChange={(nextValue) => {
        if (nextValue === null) return
        const selected = items.find((item) => Object.is(item.value, nextValue))
        if (selected) onSelect(selected)
      }}
    >
      <div className="relative w-fit max-w-full">
        <SelectTrigger
          id={id}
          aria-labelledby={ariaLabelledBy}
          aria-label={
            ariaLabelledBy
              ? undefined
              : triggerLabel || t(($) => $['placeholder.select'], { ns: 'common' })
          }
          className={cn(
            'h-auto min-h-8 w-fit max-w-full cursor-pointer items-center rounded-lg border-[0.5px] border-transparent bg-components-input-bg-normal px-2 py-1 hover:bg-state-base-hover-alt data-popup-open:bg-state-base-hover-alt! data-popup-open:hover:bg-state-base-hover-alt [&>*:last-child]:hidden',
            hasValue &&
              'border-components-button-secondary-border! bg-components-button-secondary-bg! pr-6 shadow-xs hover:border-components-button-secondary-border-hover hover:bg-components-button-secondary-bg-hover! data-popup-open:border-components-button-secondary-border-hover! data-popup-open:bg-components-button-secondary-bg-hover! data-popup-open:hover:border-components-button-secondary-border-hover data-popup-open:hover:bg-components-button-secondary-bg-hover!',
            className,
          )}
        >
          <span className="flex min-w-0 grow items-center gap-1 text-left">
            {showLeftIcon && (
              <span aria-hidden="true" className="p-0.5">
                {leftIcon || (
                  <span
                    aria-hidden
                    className={cn(
                      'i-ri-filter-3-line block size-4 text-text-tertiary',
                      hasValue && 'text-text-secondary',
                    )}
                  />
                )}
              </span>
            )}
            <span
              className={cn(
                'flex min-w-0 grow items-center gap-0.5 first-line:p-1',
                label && 'gap-1 px-1',
              )}
            >
              {label && (
                <span className="shrink-0 system-sm-regular text-text-tertiary">{label}</span>
              )}
              <span
                className={cn(
                  'system-sm-regular text-text-tertiary',
                  hasValue && 'text-text-secondary',
                  label && 'truncate system-sm-medium',
                )}
              >
                {triggerContent}
              </span>
            </span>
            {!hasValue && (
              <span
                aria-hidden
                className="i-ri-arrow-down-s-line block size-4 text-text-tertiary"
              />
            )}
          </span>
        </SelectTrigger>
        {hasValue && (
          <button
            type="button"
            disabled={disabled}
            aria-label={clearLabel}
            className="group/clear absolute top-1/2 right-1.5 flex size-5 -translate-y-1/2 cursor-pointer touch-manipulation items-center justify-center rounded-md border-none bg-transparent p-0 outline-hidden focus-visible:inset-ring-2 focus-visible:inset-ring-state-accent-solid disabled:cursor-not-allowed disabled:opacity-50"
            onClick={onClear}
          >
            <span
              aria-hidden
              className="i-ri-close-circle-fill block size-3.5 text-text-quaternary group-hover/clear:text-text-tertiary"
            />
          </button>
        )}
        <SelectPortal>
          <SelectPositioner>
            <SelectPopup
              className={cn(
                'relative w-60 rounded-xl border-[0.5px] bg-components-panel-bg-blur p-0 text-sm text-text-secondary shadow-lg outline-hidden backdrop-blur-[5px] focus:outline-hidden focus-visible:outline-hidden',
                panelClassName,
              )}
            >
              <SelectList className="max-h-72 p-1">
                {items.map((item) => (
                  <SelectItem<T> key={item.value} value={item.value}>
                    <SelectItemText title={item.name}>{item.name}</SelectItemText>
                    {showItemIndicator && <SelectItemIndicator />}
                  </SelectItem>
                ))}
              </SelectList>
            </SelectPopup>
          </SelectPositioner>
        </SelectPortal>
      </div>
    </Select>
  )
}

export default Chip