From 39da4f696e719fbaaebafa6d13704f32426b2197 Mon Sep 17 00:00:00 2001 From: Chris Kruining Date: Mon, 17 Feb 2025 11:53:45 +1100 Subject: [PATCH] convert memo to selector --- src/components/tabs.tsx | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/components/tabs.tsx b/src/components/tabs.tsx index 1db76b8..2906476 100644 --- a/src/components/tabs.tsx +++ b/src/components/tabs.tsx @@ -1,4 +1,4 @@ -import { Accessor, children, createContext, createEffect, createMemo, createSignal, For, ParentComponent, Setter, Show, useContext } from "solid-js"; +import { Accessor, children, createContext, createEffect, createMemo, createSelector, 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"; @@ -6,7 +6,7 @@ import css from "./tabs.module.css"; type CloseTabCommandType = CommandType<(id: string) => any>; interface TabsContextType { activate(id: string | undefined): void; - isActive(id: string): Accessor; + isActive(id: string): boolean; readonly onClose: Accessor } @@ -24,6 +24,7 @@ const useTabs = () => { export const Tabs: ParentComponent<{ class?: string, active?: string, setActive?: Setter, onClose?: CloseTabCommandType }> = (props) => { const [active, setActive] = createSignal(props.active); + const isActive = createSelector(active, (id, active) => id === active); createEffect(() => { props.setActive?.(active()); @@ -34,9 +35,11 @@ export const Tabs: ParentComponent<{ class?: string, active?: string, setActive? setActive(id); }, - isActive(id: string) { - return createMemo(() => active() === id); - }, + isActive, + + // isActive(id: string) { + // return createMemo(() => active() === id); + // }, onClose: createMemo(() => props.onClose), }; @@ -91,7 +94,6 @@ const _Tabs: ParentComponent<{ class?: string, active: string | undefined, onClo export const Tab: ParentComponent<{ id: string, label: string, closable?: boolean }> = (props) => { const context = useTabs(); const resolved = children(() => props.children); - const isActive = context.isActive(props.id); return
- + {resolved()}
;