Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

4.9 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { ReactNode } from 'react';
import { MoreDownIcon } from '@univerjs/icons';
import { useContext, useMemo, useState } from 'react';
import type { IDropdownMenuProps } from '../dropdown-menu/DropdownMenu';
import { clsx } from '../../helper/clsx';
import { Badge } from '../badge/Badge';
import { DropdownMenu } from '../dropdown-menu/DropdownMenu';
import { FormLayoutContext } from '../form-layout/FormLayout';
import { selectClassName } from './Select';

interface IOption {
    label?: string | ReactNode;
    value?: string;
    disabled?: boolean;
}

export interface IMultipleSelectProps {
    className?: string;
    size?: 'small' | 'large';

    /**
     * The value of select
     */
    value: string[];

    /**
     * Whether the select is disabled
     * @default false
     */
    disabled?: boolean;

    /**
     * The options of select
     * @default []
     */
    options?: IOption[];

    /**
     * The style of select
     * @default false
     */
    borderless?: boolean;

    /**
     * The callback function that is triggered when the value is changed
     */
    onChange: (values: string[]) => void;
}

export function MultipleSelect(props: IMultipleSelectProps) {
    const {
        className,
        size = 'small',
        value = [],
        disabled = false,
        options = [],
        borderless = false,
        onChange,
    } = props;

    const [open, setOpen] = useState(false);
    const invalid = useContext(FormLayoutContext);

    function handleOpenChange(open: boolean) {
        setOpen(open);
    }

    const items: IDropdownMenuProps['items'] = useMemo(() => {
        return options.map((option) => {
            return {
                type: 'checkbox',
                value: option.value!,
                label: option.label,
                disabled: option.disabled,
                checked: value.includes(option.value!),
                onSelect: (item: string) => {
                    const newValue = value.includes(item) ? value.filter((v) => v !== item) : [...value, item];

                    onChange(newValue);
                },
            };
        });
    }, [onChange, options, value]);

    function handleClose(item: string) {
        if (disabled) return;
        const newValue = value.filter((v) => v !== item);
        onChange(newValue);
    }

    const displayValue = options
        .filter((option) => value.includes(option.value!))
        .map((option) => (
            <Badge
                key={option.value}
                className="univer-max-w-32"
                closable={!disabled && !option.disabled}
                onClose={() => handleClose(option.value!)}
            >
                {option.label}
            </Badge>
        ));

    return (
        <DropdownMenu
            className="univer-w-[--radix-popper-anchor-width] univer-min-w-36"
            align="start"
            open={open}
            items={items}
            disabled={disabled}
            onOpenChange={handleOpenChange}
        >
            <div
                data-u-comp="multiple-select"
                aria-invalid={invalid || undefined}
                className={clsx(selectClassName, size === 'large'
                    ? 'univer-h-auto univer-min-h-12 univer-text-base'
                    : 'univer-text-sm', {
                    '!univer-border-primary-600 univer-outline-none univer-ring-2 univer-ring-primary-50 dark:!univer-border-primary-300 dark:!univer-ring-primary-900': open && !borderless,
                    'univer-border-transparent univer-bg-transparent hover:univer-border-transparent': borderless,
                    'univer-cursor-not-allowed univer-opacity-50': disabled,
                    'hover:univer-border-primary-600': !disabled && !borderless,
                    'univer-cursor-pointer': !disabled && !open,
                }, className)}
            >
                <div
                    className="univer-box-border univer-flex univer-w-[calc(100%-16px)] univer-gap-2 univer-pr-2"
                >
                    {displayValue}
                </div>
                <MoreDownIcon
                    className={`
                      univer-flex-shrink-0
                      dark:!univer-text-gray-0
                    `}
                />
            </div>
        </DropdownMenu>
    );
}