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.

7.5 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 { VariantProps } from 'class-variance-authority';
import type { InputHTMLAttributes } from 'react';
import { CloseIcon } from '@univerjs/icons';
import { cva } from 'class-variance-authority';
import { forwardRef, useContext, useEffect, useRef, useState } from 'react';
import { borderClassName, inputStateClassName } from '../../helper/class-utilities';
import { clsx } from '../../helper/clsx';
import { ConfigContext } from '../config-provider/ConfigProvider';
import { FormLayoutContext } from '../form-layout/FormLayout';

type InputProps = InputHTMLAttributes<HTMLInputElement>;

export const inputVariants = cva(
    `
      univer-box-border univer-w-full univer-rounded-md univer-bg-gray-0 univer-text-gray-900 univer-transition-colors
      univer-duration-200
      placeholder:univer-text-gray-500
      dark:!univer-bg-gray-700 dark:!univer-text-gray-0
      dark:placeholder:univer-text-gray-300
    `,
    {
        variants: {
            size: {
                mini: 'univer-h-7 univer-px-1.5 univer-text-sm',
                small: 'univer-h-8 univer-px-2 univer-text-sm',
                middle: 'univer-h-10 univer-px-3 univer-text-base',
                large: 'univer-h-12 univer-px-4 univer-text-lg',
            },
        },
        defaultVariants: {
            size: 'small',
        },
    }
);

export interface IInputProps extends Omit<InputProps, 'onChange' | 'size' | 'slot' | 'value'>,
    VariantProps<typeof inputVariants> {
    autoFocus?: boolean;
    className?: string;
    style?: React.CSSProperties;
    type?: HTMLInputElement['type'];
    placeholder?: string;
    value?: string;
    allowClear?: boolean;
    disabled?: boolean;
    onClick?: (e: React.MouseEvent<HTMLInputElement>) => void;
    onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
    onChange?: (value: string) => void;
    inputClass?: string;
    inputStyle?: React.CSSProperties;
    slot?: React.ReactNode;
}

export const Input = forwardRef<HTMLInputElement, IInputProps>(
    ({
        autoFocus = false,
        className,
        style,
        type = 'text',
        placeholder,
        value,
        size,
        allowClear = false,
        disabled = false,
        onClick,
        onKeyDown,
        onChange,
        onFocus,
        onBlur,
        slot,
        inputClass,
        inputStyle,
        ...props
    }, ref) => {
        const { direction, locale } = useContext(ConfigContext);
        const invalid = useContext(FormLayoutContext);

        const handleClear = (e: React.MouseEvent) => {
            e.stopPropagation();
            onChange?.('');
        };

        const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
            onChange?.(e.target.value);
        };

        const hasSlotContent = (allowClear && value && !disabled) || slot;

        const [paddingRight, setPaddingRight] = useState(0);
        const slotRef = useRef<HTMLDivElement>(null);

        useEffect(() => {
            let observer: MutationObserver | null = null;
            if (slot && slotRef.current) {
                observer = new MutationObserver(() => {
                    if (slotRef.current) {
                        setPaddingRight(slotRef.current.offsetWidth + 4 * 2);
                    }
                });

                observer.observe(slotRef.current, { childList: true, subtree: true });

                setPaddingRight(slotRef.current.offsetWidth + 4 * 2);
            }

            return () => observer?.disconnect();
        }, [slot]);

        let trailingPadding = 0;
        if (slot) {
            trailingPadding = paddingRight;
        } else if (allowClear) {
            trailingPadding = 26;
        }
        const paddingSide = direction === 'rtl' ? 'paddingLeft' : 'paddingRight';
        const mergedInputStyle = direction === 'rtl' && trailingPadding === 0
            ? inputStyle
            : { ...inputStyle, [paddingSide]: trailingPadding };

        return (
            <div
                data-u-comp="input"
                className={clsx(
                    'univer-relative univer-inline-flex univer-w-full univer-items-center univer-rounded-md',
                    disabled && 'univer-cursor-not-allowed',
                    className
                )}
                style={style}
            >
                <input
                    ref={ref}
                    type={type}
                    className={clsx(
                        inputVariants({ size }),
                        borderClassName,
                        inputStateClassName,
                        disabled && `
                          univer-cursor-not-allowed univer-bg-gray-50 univer-text-gray-400
                          dark:!univer-text-gray-500
                        `,
                        (allowClear && !slot) && (direction === 'rtl' ? 'univer-pl-8' : 'univer-pr-8'),
                        inputClass
                    )}
                    placeholder={placeholder}
                    value={value}
                    disabled={disabled}
                    autoFocus={autoFocus}
                    onClick={onClick}
                    onKeyDown={onKeyDown}
                    onChange={handleChange}
                    onFocus={onFocus}
                    onBlur={onBlur}
                    style={mergedInputStyle}
                    {...props}
                    aria-invalid={props['aria-invalid'] ?? (invalid || undefined)}
                />
                {hasSlotContent && (
                    <div
                        className={clsx(
                            'univer-absolute univer-flex univer-items-center univer-gap-1 univer-rounded-full',
                            direction === 'rtl' ? 'univer-left-2' : 'univer-right-2'
                        )}
                        ref={slotRef}
                    >
                        {slot}
                        {allowClear && value && !disabled && (
                            <button
                                type="button"
                                aria-label={locale?.Accessibility.clearInput}
                                onClick={handleClear}
                                className={`
                                  univer-flex univer-size-4 univer-cursor-pointer univer-items-center
                                  univer-rounded-full univer-border-none univer-bg-transparent univer-p-1
                                  univer-text-gray-400 univer-transition-colors univer-duration-200
                                  hover:univer-text-gray-500
                                  focus:univer-outline-none focus:univer-ring-2 focus:univer-ring-primary-600
                                `}
                            >
                                <CloseIcon aria-hidden="true" className="univer-size-3" />
                            </button>
                        )}
                    </div>
                )}
            </div>
        );
    }
);