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.

11.0 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 { ISheetLocation } from '@univerjs/sheets';
import type { IPopupWithExtraProps } from '@univerjs/ui';
import type { CSSProperties } from 'react';
import type { LocaleKey } from '../../locale/types';
import type { IBaseDropdownProps } from './type';
import { ColorKit, LocaleService, ThemeService } from '@univerjs/core';
import { borderClassName, borderTopClassName, clsx, Input, scrollbarClassName } from '@univerjs/design';
import { CheckMarkIcon } from '@univerjs/icons';
import {
    RangeProtectionPermissionEditPoint,
    serializeListOptions,
    SheetPermissionCheckController,
    WorkbookEditablePermission,
    WorksheetEditPermission,
} from '@univerjs/sheets';
import { useDependency, useObservable } from '@univerjs/ui';
import { useEffect, useMemo, useRef, useState } from 'react';
import { getListDropdownValue } from './list-dropdown/utils';

interface ISelectListProps {
    value: string[];
    onChange: (val: string[]) => void;
    multiple?: boolean;
    options: { label: string; value: string; color?: string }[];
    onEdit?: () => void;
    style?: CSSProperties;
    location: ISheetLocation;
    showEdit?: boolean;
    showSearch?: boolean;
}

function SelectList(props: ISelectListProps) {
    const {
        value,
        onChange,
        multiple,
        options,
        onEdit,
        style,
        location,
        showEdit: showEditOnDropdown,
        showSearch: showSearchOnDropdown,
    } = props;
    const localeService = useDependency(LocaleService);
    const themeService = useDependency(ThemeService);
    useObservable(themeService.currentTheme$);
    const { row, col, unitId, subUnitId } = location;

    const searchRef = useRef<HTMLInputElement>(null);
    useEffect(() => {
        let timer: NodeJS.Timeout | null = null;

        if (showSearchOnDropdown) {
            timer = setTimeout(() => {
                searchRef.current?.focus();
            }, 0);
        }

        return () => {
            if (timer) {
                clearTimeout(timer);
            }
        };
    }, [showSearchOnDropdown]);

    const [lowerFilter, setLowerFilter] = useState('');
    const filteredOptions = useMemo(() => {
        return options.filter((item) => lowerFilter ? item.label.toLowerCase().includes(lowerFilter) : true);
    }, [options, lowerFilter]);

    const handleSearchChange = (value: string) => {
        setLowerFilter(value.toLowerCase());
    };

    const sheetPermissionCheckController = useDependency(SheetPermissionCheckController);
    const hasPermission = useMemo(() => sheetPermissionCheckController.permissionCheckWithRanges(
        {
            workbookTypes: [WorkbookEditablePermission],
            rangeTypes: [RangeProtectionPermissionEditPoint],
            worksheetTypes: [WorksheetEditPermission],
        },
        [{ startColumn: col, startRow: row, endColumn: col, endRow: row }],
        unitId,
        subUnitId
    ), [sheetPermissionCheckController, col, row, unitId, subUnitId]);

    return (
        <div
            data-u-comp="sheets-dropdown-list"
            className={clsx(`
              univer-box-border univer-rounded-lg univer-bg-gray-0 univer-py-1 univer-text-gray-900 univer-shadow
              dark:!univer-bg-gray-1000 dark:!univer-text-gray-0
            `, borderClassName)}
            style={style}
        >
            {showSearchOnDropdown && (
                <div className="univer-box-border univer-flex univer-w-full univer-px-2">
                    <Input
                        ref={searchRef}
                        type="text"
                        value={lowerFilter}
                        autoFocus
                        onChange={handleSearchChange}
                    />
                </div>
            )}
            <div className="univer-px-3.5 univer-py-1 univer-pt-2 univer-text-xs">
                {localeService.t<LocaleKey>(multiple
                    ? 'sheets-ui.data-validation.listMultiple.dropdown'
                    : 'sheets-ui.data-validation.list.dropdown')}
            </div>
            <div
                key={lowerFilter}
                className={clsx(`
                  univer-flex univer-max-h-52 univer-flex-col univer-gap-1 univer-overflow-y-auto univer-px-2
                  univer-py-1
                `, scrollbarClassName)}
            >
                {filteredOptions.map((item, i) => {
                    const selected = value.indexOf(item.value) > -1;
                    const handleClick = () => {
                        let set: Set<string>;
                        if (selected) {
                            set = new Set(value.filter((sub) => sub !== item.value));
                        } else {
                            set = new Set(multiple ? [...value, item.value] : [item.value]);
                        }
                        const newValue: string[] = [];
                        options.forEach((opt) => {
                            if (set.has(opt.value)) {
                                newValue.push(opt.value);
                            }
                        });

                        onChange(newValue);
                    };

                    const index = item.label.toLocaleLowerCase().indexOf(lowerFilter!);

                    const isThemeColor = Boolean(item.color?.includes('.') && themeService.isValidThemeColor(item.color));
                    const backgroundColor = isThemeColor
                        ? themeService.getColorFromTheme(item.color!)
                        : item.color;
                    const color = new ColorKit(backgroundColor);
                    const hasBackground = Boolean(backgroundColor) && backgroundColor !== 'transparent' && color.getAlpha() > 0;
                    const isDark = color.isDark();

                    return (
                        <div
                            key={i}
                            className={`
                              univer-flex univer-cursor-pointer univer-flex-row univer-items-center
                              univer-justify-between univer-rounded-md univer-px-1.5 univer-py-1
                              hover:univer-bg-gray-50
                              dark:hover:!univer-bg-gray-700
                            `}
                            onClick={handleClick}
                        >
                            <div
                                className={clsx(`
                                  univer-inline-flex univer-h-4 univer-w-fit univer-items-center univer-truncate
                                  univer-rounded-full univer-px-1.5 univer-text-xs
                                `, {
                                    'univer-text-gray-900': hasBackground && (isThemeColor || !isDark),
                                    'univer-text-gray-0': hasBackground && !isThemeColor && isDark,
                                })}
                                style={{ background: backgroundColor }}
                            >
                                {lowerFilter && item.label.toLowerCase().includes(lowerFilter)
                                    ? (
                                        <>
                                            <span>{item.label.substring(0, index)}</span>
                                            <span className="univer-font-semibold">
                                                {item.label.substring(index, index + lowerFilter.length)}
                                            </span>
                                            <span>{item.label.substring(index + lowerFilter.length)}</span>
                                        </>
                                    )
                                    : item.label}
                            </div>
                            <div
                                className={`
                                  univer-ml-3 univer-size-4 univer-flex-shrink-0 univer-flex-grow-0 univer-text-base
                                  univer-text-primary-500
                                `}
                            >
                                {selected && <CheckMarkIcon className="univer-text-primary-600" />}
                            </div>
                        </div>
                    );
                })}
            </div>
            {(showEditOnDropdown && hasPermission) && (
                <div className={clsx('univer-box-border univer-px-2 univer-pt-1', borderTopClassName)}>
                    <a
                        className={`
                          univer-block univer-cursor-pointer univer-rounded univer-px-1.5 univer-py-1 univer-text-xs
                          hover:univer-bg-gray-100
                          dark:hover:!univer-bg-gray-800
                        `}
                        onClick={onEdit}
                    >
                        {localeService.t<LocaleKey>('sheets-ui.data-validation.list.edit')}
                    </a>
                </div>
            )}
        </div>
    );
};

export interface IListDropdownProps {
    multiple?: boolean;
    onEdit?: () => void;
    onChange?: (value: string[]) => boolean | Promise<boolean>;
    options: { label: string; value: string; color?: string }[];
    defaultValue?: string;
    showEdit?: boolean;
    showSearch?: boolean;
}

export function ListDropDown(props: { popup: IPopupWithExtraProps<IListDropdownProps & IBaseDropdownProps> }) {
    const { popup: { extraProps, anchorRect } } = props;
    const { location, hideFn, onChange, onEdit, options, defaultValue, multiple, showSearch, showEdit } = extraProps;

    const { worksheet } = location;
    const [localValue, setLocalValue] = useState(defaultValue);

    if (!worksheet) {
        return null;
    }
    const cellWidth = (anchorRect?.right ?? 0) - (anchorRect?.left ?? 0);

    return (
        <SelectList
            style={{ minWidth: cellWidth, maxWidth: Math.max(cellWidth, 200) }}
            value={getListDropdownValue(localValue, multiple)}
            multiple={multiple}
            onChange={async (newValue) => {
                const str = multiple ? serializeListOptions(newValue) : (newValue[0] ?? '');
                setLocalValue(str);
                const success = await onChange?.(newValue);
                if (!(success === false)) {
                    hideFn();
                }
            }}
            options={options}
            onEdit={onEdit}
            location={location}
            showEdit={showEdit}
            showSearch={showSearch}
        />
    );
}

ListDropDown.componentKey = 'sheets.dropdown.list';