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.

2.7 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 { IFormulaEditorRef } from '@univerjs/sheets-formula-ui';
import type { IFormulaInputProps } from './interface';
import { borderClassName, clsx, FormLayout } from '@univerjs/design';
import { FormulaEditor } from '@univerjs/sheets-formula-ui';
import { useSidebarClick } from '@univerjs/ui';
import { useRef, useState } from 'react';

export function CustomFormulaInput(props: IFormulaInputProps) {
    const { unitId, subUnitId, value, onChange, showError, validResult } = props;
    const formula1Res = showError ? validResult?.formula1 : undefined;
    const formulaEditorRef = useRef<IFormulaEditorRef>(null);
    const [isFocusFormulaEditor, setIsFocusFormulaEditor] = useState(false);

    useSidebarClick((e: MouseEvent) => {
        const isOutSide = formulaEditorRef.current?.isClickOutSide(e);
        isOutSide && setIsFocusFormulaEditor(false);
    });

    return (
        <FormLayout error={formula1Res}>
            <FormulaEditor
                ref={formulaEditorRef}
                className={clsx(`
                  univer-box-border univer-h-8 univer-w-full univer-cursor-pointer univer-items-center univer-rounded-lg
                  univer-bg-gray-0 univer-pt-2 univer-transition-colors
                  hover:univer-border-primary-600
                  dark:!univer-bg-gray-700 dark:!univer-text-gray-0
                  [&>div:first-child]:univer-px-2.5
                  [&>div]:univer-h-5 [&>div]:univer-ring-transparent
                `, borderClassName)}
                initValue={value?.formula1 ?? '=' as any}
                unitId={unitId}
                subUnitId={subUnitId}
                isFocus={isFocusFormulaEditor}
                isSupportAcrossSheet
                onChange={(newValue) => {
                    const newFormula = (newValue ?? '').trim();
                    if (newFormula === value?.formula1) {
                        return;
                    }

                    onChange?.({
                        ...value,
                        formula1: newFormula,
                    });
                }}
                onFocus={() => setIsFocusFormulaEditor(true)}
            />
        </FormLayout>
    );
}