import React, { useState, useCallback, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { EllipsisVerticalIcon } from '@heroicons/react/24/outline'; import { unstable_batchedUpdates } from 'react-dom'; import RemoveConfirm from '../removeConfirm'; import { Dialog, DialogContent, DialogFooter, DialogHeader } from '../ui/dialog'; import { Input } from '../ui/input'; import { Button } from '../ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '../ui/dropdown-menu'; function classNames(...classes: string[]) { return classes.filter(Boolean).join(' '); } export interface ITabOption { label: string; key: string; editable?: boolean; } interface EditableTabsProps { tabs: ITabOption[]; selectedKey: string; showRemove?: boolean; onSelected: (selectedKey: string, index: number) => void; onEditLabel?: (label: string, index: number) => void; onDuplicate?: (index: number) => void; onRemove?: (index: number) => void; } const Slider = (props: { className?: string; children: React.ReactNode }) => { const [x, setX] = useState(0); const ref = useRef(null); const parentDisposeRef = useRef<() => void>(null); const childDisposeRef = useRef<() => void>(null); const onWheel = useCallback((e: WheelEvent) => { e.preventDefault(); e.stopPropagation(); const parentWidth = ref.current?.getBoundingClientRect().width; const childWidth = ref.current?.children[0]?.getBoundingClientRect().width; if (parentWidth && childWidth) { setX((x) => Math.min(Math.max(0, childWidth - parentWidth), Math.max(0, x + e.deltaY + e.deltaX))); } return false; }, []); const refCB = useCallback( (node) => { parentDisposeRef.current?.(); if (node == null) { return; } const r = new ResizeObserver((es) => { for (const e of es) { const parentWidth = e.contentRect.width; const childWidth = ref.current?.children[0]?.getBoundingClientRect().width; if (parentWidth && childWidth) { setX((x) => Math.min(Math.max(0, childWidth - parentWidth), x)); } } }); r.observe(node); ref.current = node; node.addEventListener('wheel', onWheel, { passive: false }); parentDisposeRef.current = () => { r.disconnect(); ref.current?.removeEventListener('wheel', onWheel); }; }, [onWheel] ); const childRefCB = useCallback((node: HTMLDivElement) => { childDisposeRef.current?.(); if (node === null) { return; } const r = new ResizeObserver((es) => { for (const e of es) { const parentWidth = ref.current?.getBoundingClientRect().width; const childWidth = e.contentRect.width; if (parentWidth && childWidth) { setX((x) => Math.min(Math.max(0, childWidth - parentWidth), x)); } } }); childDisposeRef.current = () => { r.disconnect(); }; r.observe(node); }, []); return (
{ e.preventDefault(); e.stopPropagation(); e.nativeEvent.preventDefault(); setX((x) => Math.max(0, x + e.deltaY)); return false; }} className={props.className} >
{props.children}
); }; export default function EditableTabs(props: EditableTabsProps) { const { tabs, selectedKey, onSelected, onEditLabel, onRemove, onDuplicate, showRemove } = props; const [editingIndex, setEditingIndex] = useState(-1); const [name, setName] = useState(''); const { t } = useTranslation(); return (
-1} onOpenChange={() => { setEditingIndex(-1); }} > {t('main.tablist.chart_name')}
{ setName(e.target.value); }} />
); }