Skip to content

Commit

Permalink
Fixed nesting Tabs components (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanrcollings authored Nov 20, 2024
1 parent 272566b commit c076cd9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 268 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-tomatoes-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atomicjolt/atomic-elements": major
---

Migrated internal collections APIs, made collection components (Table, Select, Tabs, etc...) more flexible
251 changes: 1 addition & 250 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ export const CheckBox = React.forwardRef<HTMLInputElement, CheckBoxProps>(
const { direction } = useLocale();
const { inputProps, labelProps } = useCheckbox(props, state, internalRef);

console.log(props);

return (
<CheckboxWrapper
className={cn("aje-checkbox", className)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
useTabList,
useTabPanel,
} from "@react-aria/tabs";
import { TabListState, useTabListState, Node } from "react-stately";
import { useTabListState, Node } from "react-stately";
import {
BaseCollection,
Collection,
CollectionBuilder,
createHideableComponent,
createLeafComponent,
} from "@react-aria/collections";
import { useObjectRef } from "@react-aria/utils";

import { useContextPropsV2 } from "@hooks/useContextProps";
import { useRenderProps } from "@hooks";
Expand Down Expand Up @@ -165,30 +167,35 @@ interface TabPanelProps extends AriaTabPanelProps {
children?: React.ReactNode;
}

function TabPanel(props: TabPanelProps) {
function TabPanelInner(
props: TabPanelProps,
forwardedRef: React.ForwardedRef<HTMLDivElement>
) {
const state = useContext(TabsStateContext);
const ref = useObjectRef(forwardedRef);
const { tabPanelProps } = useTabPanel(props, state, ref);

if (!state) return null;
if (state.selectedKey !== props.id) return null;

return <TabPanelInner {...props} state={state} />;
}

interface TabPanelInnerProps extends TabPanelProps {
state: TabListState<any>;
}
const isSelected = state?.selectedKey === props.id;

function TabPanelInner(props: TabPanelInnerProps) {
const { state } = props;
const ref = useRef(null);
const { tabPanelProps } = useTabPanel(props, state, ref);
if (!isSelected) return null;

return (
<TabContentWrapper {...tabPanelProps} ref={ref}>
{props.children}
{/* May contain other tabs, so clear out the
providers below this in the tree */}
<Provider
values={[
[TabsContext.Provider, {}],
[TabsStateContext.Provider, null],
]}
>
{props.children}
</Provider>
</TabContentWrapper>
);
}

const TabPanel = createHideableComponent(TabPanelInner);
// @ts-expect-error
TabPanel.displayName = "Tabs.Panel";
Tabs.Panel = TabPanel;

0 comments on commit c076cd9

Please sign in to comment.