Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set active index correctly when default value is passed in #383

Merged
merged 6 commits into from
May 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: combine values
atomictangerine committed May 2, 2023
commit 64f21571d97675383705b315832e871e616a0e99
8 changes: 2 additions & 6 deletions ui/tabs/src/TabsList.tsx
Original file line number Diff line number Diff line change
@@ -54,15 +54,11 @@ export const TabsList = React.forwardRef<HTMLDivElement, TabsListProps>(
null
);

const { defaultValue, value } = React.useContext(TabsContext);
const { initialValue } = React.useContext(TabsContext);

React.useEffect(() => {
React.Children.map(children, (child: React.ReactNode, index: number) => {
if (React.isValidElement(child) && defaultValue === child.props.value) {
setActiveIndex(index);
}

if (React.isValidElement(child) && value === child.props.value) {
if (React.isValidElement(child) && initialValue === child.props.value) {
setActiveIndex(index);
}
});
7 changes: 3 additions & 4 deletions ui/tabs/src/TabsRoot.tsx
Original file line number Diff line number Diff line change
@@ -18,15 +18,14 @@ export type TabsRootProps = {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const TabsRoot = React.forwardRef<HTMLDivElement, TabsRootProps>(
({ children, defaultValue = "", ...props }: TabsRootProps, ref) => {
({ children, ...props }: TabsRootProps, ref) => {
return (
<TabsContext.Provider
value={{
defaultValue: defaultValue,
value: props.value,
initialValue: props.defaultValue || props.value,
}}
>
<StyledTabsRoot defaultValue={defaultValue} {...props} ref={ref}>
<StyledTabsRoot {...props} ref={ref}>
{children}
</StyledTabsRoot>
</TabsContext.Provider>
6 changes: 2 additions & 4 deletions ui/tabs/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as React from "react";

interface TabsContextInterface {
defaultValue?: string;
value?: string;
initialValue?: string;
}

const defaultState = {
defaultValue: "",
value: "",
initialValue: "",
};

export const TabsContext =