'use client' import { Button } from '@langgenius/dify-ui/button' import { Checkbox } from '@langgenius/dify-ui/checkbox' import { CheckboxGroup } from '@langgenius/dify-ui/checkbox-group' import { cn } from '@langgenius/dify-ui/cn' import { Field, FieldDescription, FieldItem, FieldLabel } from '@langgenius/dify-ui/field' import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset' import { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import Badge from '@/app/components/base/badge' import { SearchInput } from '@/app/components/base/search-input' import SearchMenu from '@/assets/search-menu.svg' type CheckboxListOption = { label: string value: string disabled?: boolean } type CheckboxListProps = { name?: string title?: string label?: string description?: string options: CheckboxListOption[] value?: string[] onChange?: (value: string[]) => void disabled?: boolean containerClassName?: string showSelectAll?: boolean showCount?: boolean showSearch?: boolean maxHeight?: string | number } export const CheckboxList = ({ name, title = '', label, description, options, value = [], onChange, disabled = false, containerClassName, showSelectAll = true, showCount = true, showSearch = true, maxHeight, }: CheckboxListProps) => { const { t } = useTranslation() const [searchQuery, setSearchQuery] = useState('') const filteredOptions = useMemo(() => { if (!searchQuery?.trim()) return options const query = searchQuery.toLowerCase() return options.filter( (option) => option.label.toLowerCase().includes(query) || option.value.toLowerCase().includes(query), ) }, [options, searchQuery]) const selectedCount = value.length const selectableOptionValues = useMemo( () => options.filter((option) => !option.disabled).map((option) => option.value), [options], ) return (
onChange?.(nextValue)} allValues={selectableOptionValues} disabled={disabled} className="flex flex-col gap-1" /> } > {label && {label}} {description && ( {description} )}
{(showSelectAll || title || showSearch) && (
{!searchQuery && showSelectAll && ( {t(($) => $['operation.selectAll'], { ns: 'common' })} )} {!searchQuery ? (
{title && ( {title} )} {showCount && selectedCount > 0 && ( {t(($) => $['operation.selectCount'], { ns: 'common', count: selectedCount })} )}
) : (
{filteredOptions.length > 0 ? t(($) => $['operation.searchCount'], { ns: 'common', count: filteredOptions.length, content: title, }) : t(($) => $['operation.noSearchCount'], { ns: 'common', content: title })}
)} {showSearch && ( $['placeholder.search'], { ns: 'common' })} className="w-40" /> )}
)}
{!filteredOptions.length ? (
{searchQuery ? (
search menu {t(($) => $['operation.noSearchResults'], { ns: 'common', content: title })}
) : ( t(($) => $.noData, { ns: 'common' }) )}
) : ( filteredOptions.map((option) => ( {option.label} )) )}
) }