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

feat(tabs): initial selected tab #216

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.3.4",
"version": "1.3.5",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
12 changes: 10 additions & 2 deletions src/tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
* If specified, the index of the selected tab is controlled by the parent component rather than the internal state.
*/
index?: number;
/**
* default index of the selected tab.
*/
defaultIndex?: number;
onChange?: (index: number) => void;
/**
* The orientation of the tabs. Defaults to horizontal
Expand All @@ -132,16 +136,20 @@
children,
className,
index,
defaultIndex,
onChange,
orientation = 'horizontal',
extra,
}: TabsProps) {
// Filter out the nulls from the children so that tabs can be mounted conditionally
children = Children.toArray(children).filter(child => child);
const tabs = parseTabList(children);

// Initialize the selected tab to the first non-hidden tab if there is no controlled value provided
const defaultValue = tabs.findIndex(tab => !tab.hidden);
const defaultValue =
typeof defaultIndex === 'number'
? defaultIndex
: tabs.findIndex(tab => !tab.hidden);
const [selectedIndex, setSelectedIndex] = useControlledState(
index,
defaultValue,
Expand Down Expand Up @@ -253,7 +261,7 @@
`}
>
{typeof children === 'function'
? children({ isSelected, index: index! })

Check warning on line 264 in src/tabs/Tabs.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 264 in src/tabs/Tabs.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 264 in src/tabs/Tabs.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and windows-latest

Forbidden non-null assertion

Check warning on line 264 in src/tabs/Tabs.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and windows-latest

Forbidden non-null assertion

Check warning on line 264 in src/tabs/Tabs.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and macOS-latest

Forbidden non-null assertion

Check warning on line 264 in src/tabs/Tabs.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and macOS-latest

Forbidden non-null assertion
: children}
</div>
);
Expand Down
Loading