Skip to content

Commit

Permalink
Fix: add value prop to tabs (#1952)
Browse files Browse the repository at this point in the history
Fix: add value prop to tabs
  • Loading branch information
leslielo-okta committed Aug 30, 2023
1 parent ee7d909 commit e38e4d4
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/odyssey-react-mui/src/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import React, {
ReactNode,
memo,
useCallback,
useEffect,
useState,
} from "react";
import { Tab as MuiTab } from "@mui/material";
Expand Down Expand Up @@ -53,17 +54,22 @@ export type TabsProps = {
*/
ariaLabel?: string;
/**
* The value of the Tab that should be selected by default
* @deprecated please use the `value` prop instead
* When `value` is provided, `initialValue` isn't used.
*/
initialValue?: string;
/**
* The TabItems to be included in the Tabs group
*/
tabs: TabItemProps[];
/**
* Identifier for the selected tab.
*/
value?: string;
};

const Tabs = ({ ariaLabel, initialValue = "0", tabs }: TabsProps) => {
const [tabState, setTabState] = useState(initialValue);
const Tabs = ({ ariaLabel, initialValue, tabs, value }: TabsProps) => {
const [tabState, setTabState] = useState(initialValue ?? value ?? "0");

const onChange = useCallback(
(_event: React.SyntheticEvent, newState: string) => {
Expand All @@ -72,6 +78,12 @@ const Tabs = ({ ariaLabel, initialValue = "0", tabs }: TabsProps) => {
[]
);

useEffect(() => {
if (value !== undefined) {
setTabState(value);
}
}, [value]);

return (
<MuiTabContext value={tabState}>
<MuiTabList onChange={onChange} aria-label={ariaLabel}>
Expand Down

0 comments on commit e38e4d4

Please sign in to comment.