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: allow overriding the selectedTab on Tabs component #186

Merged
merged 4 commits into from
Jan 22, 2024

Conversation

tbehailu
Copy link
Contributor

@tbehailu tbehailu commented Jan 22, 2024

Brings to parity with our current Tabs component in the arizeweb library which lets you control the selectedTab. Without this, you can't change the selectedTab outside of just clicking the tab itself, example as a callback on a button.

https://github.com/Arize-ai/arize/blob/main/arizeweb/src/app/components/shared/Tabs.tsx#L86-L88

Copy link

github-actions bot commented Jan 22, 2024

size-limit report 📦

Path Size
dist/components.cjs.production.min.js 195.22 KB (+0.01% 🔺)
dist/components.esm.js 142.18 KB (+0.02% 🔺)

/**
* If specified, the index of the selected tab is controlled by the parent component rather than the internal state.
*/
selectedTabIndex?: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/tabs/Tabs.tsx Outdated Show resolved Hide resolved
Comment on lines 146 to 147
const selectedIndex =
typeof selectedTabIndex == "number" ? selectedTabIndex : selectedStateIndex;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking but you could encapsulate all this in a hook like useControlledState

https://github.com/reach/reach-ui/blob/43f450db7bcb25a743121fe31355f2294065a049/packages/utils/src/use-controlled-state.ts#L14

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

const [selectedIndex, setSelectedIndex] = useControlledState(
index,
defaultValue,
() => {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simplify this even further

export function useControlledState<T, C = T>(value: Exclude<T, undefined>, defaultValue: Exclude<T, undefined> | undefined, onChange?: (v: C, ...args: any[]) => void): [T, (value: T) => void];
Suggested change
() => {}
onChange

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should allow you to fully remove the explicit call to onChange below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, amazing

Copy link
Collaborator

@mikeldking mikeldking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup!

@tbehailu tbehailu merged commit a0ad2d1 into main Jan 22, 2024
5 checks passed
@tbehailu tbehailu deleted the tabs-selectedTabIndex branch January 22, 2024 21:34
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants