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.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 { ReactNode } from 'react';
import { useRef } from 'react';
import { clsx } from '../../helper/clsx';

export interface IRadioProps {
    children?: ReactNode;
    className?: string;
    name?: string;

    /**
     * Used for setting the currently selected value
     * @default false
     */
    checked?: boolean;

    /**
     * Used for setting the currently selected value
     */
    value?: string | number | boolean;

    /**
     * Specifies whether the radio is disabled
     * @default false
     */
    disabled?: boolean;

    /**
     * Set the handler to handle `click` event
     */
    onChange?: (value: string | number | boolean) => void;
}

/**
 * Radio Component
 */
export function Radio(props: IRadioProps) {
    const { children, className, name, checked, value, disabled = false, onChange } = props;

    const inputRef = useRef<HTMLInputElement>(null!);

    function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
        e.stopPropagation();

        if (!onChange || disabled) {
            return;
        }

        if (typeof value !== 'undefined') {
            onChange && onChange(value);
        } else {
            const checked = inputRef.current.checked;
            onChange && onChange(checked);
        }
    }

    return (
        <label
            data-u-comp="radio"
            className={clsx('univer-box-border univer-inline-flex univer-items-center univer-gap-2 univer-text-sm', {
                'univer-cursor-pointer univer-text-gray-900 dark:!univer-text-gray-0': !disabled,
                'univer-text-gray-400': disabled,
            }, className)}
        >
            <span className="univer-relative univer-block">
                <input
                    ref={inputRef}
                    className="univer-peer univer-sr-only"
                    type="radio"
                    name={name}
                    checked={checked}
                    disabled={disabled}
                    onChange={handleChange}
                />
                <span
                    className={clsx(`
                      univer-relative univer-box-border univer-flex univer-size-4 univer-items-center
                      univer-justify-center univer-overflow-hidden univer-rounded-full univer-border univer-border-solid
                      univer-transition-colors
                      peer-focus:univer-outline peer-focus:univer-outline-2 peer-focus:univer-outline-offset-2
                      peer-focus:univer-outline-primary-600
                      dark:peer-focus:!univer-outline-primary-300
                    `, {
                        'univer-opacity-50': disabled,
                        'univer-border-primary-600 univer-bg-primary-600 dark:!univer-bg-primary-600': checked,
                        'univer-border-gray-300 univer-bg-gray-50 dark:!univer-border-gray-500 dark:!univer-bg-gray-600': !checked,
                    })}
                >
                    {checked && (
                        <span
                            className={`
                              univer-absolute univer-left-1/2 univer-top-1/2 univer-block univer-size-2
                              -univer-translate-x-1/2 -univer-translate-y-1/2 univer-rounded-full univer-bg-gray-0
                            `}
                        />
                    )}
                </span>
            </span>

            <span>{children}</span>
        </label>
    );
}