diff --git a/src/components/tabs.tsx b/src/components/tabs.tsx index 3237249..1db76b8 100644 --- a/src/components/tabs.tsx +++ b/src/components/tabs.tsx @@ -1,12 +1,13 @@ -import { Accessor, children, createContext, createEffect, createMemo, createSignal, For, JSX, onCleanup, ParentComponent, Setter, Show, useContext } from "solid-js"; +import { Accessor, children, createContext, createEffect, createMemo, createSignal, For, ParentComponent, Setter, Show, useContext } from "solid-js"; import { Command, CommandType, noop, useCommands } from "~/features/command"; import { AiOutlineClose } from "solid-icons/ai"; import css from "./tabs.module.css"; +type CloseTabCommandType = CommandType<(id: string) => any>; interface TabsContextType { activate(id: string | undefined): void; isActive(id: string): Accessor; - readonly onClose: Accessor | undefined> + readonly onClose: Accessor } const TabsContext = createContext(); @@ -21,11 +22,11 @@ const useTabs = () => { return context!; } -export const Tabs: ParentComponent<{ class?: string, active?: Setter, onClose?: CommandType<[string]> }> = (props) => { - const [active, setActive] = createSignal(undefined); +export const Tabs: ParentComponent<{ class?: string, active?: string, setActive?: Setter, onClose?: CloseTabCommandType }> = (props) => { + const [active, setActive] = createSignal(props.active); createEffect(() => { - props.active?.(active()); + props.setActive?.(active()); }); const ctx = { @@ -45,17 +46,13 @@ export const Tabs: ParentComponent<{ class?: string, active?: Setter; } -const _Tabs: ParentComponent<{ class?: string, active: string | undefined, onClose?: CommandType<[string]> }> = (props) => { +const _Tabs: ParentComponent<{ class?: string, active: string | undefined, onClose?: CloseTabCommandType }> = (props) => { const commandsContext = useCommands(); const tabsContext = useTabs(); const resolved = children(() => props.children); const tabs = createMemo(() => resolved.toArray().filter(c => c instanceof HTMLElement).map(({ id, dataset }, i) => ({ id, label: dataset.tabLabel ?? '', options: { closable: Boolean(dataset.tabClosable ?? 'false') } }))); - createEffect(() => { - tabsContext.activate(tabs().at(-1)?.id); - }); - const onClose = (e: Event) => { if (!commandsContext || !props.onClose) { return; @@ -67,7 +64,7 @@ const _Tabs: ParentComponent<{ class?: string, active: string | undefined, onClo return
{ - ({ id, label, options: { closable } }) => + ({ id, label, options: { closable } }) =>