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

[TabContext] Introduced new prop to allow deterministic idPrefix #38192

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions packages/mui-lab/src/TabContext/TabContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export interface TabContextProps {
* The value of the currently selected `Tab`.
*/
value: string;
/**
* The optional id prefix, internally used to render buttons
*/
idPrefix?: string;
}
/**
*
Expand Down
20 changes: 12 additions & 8 deletions packages/mui-lab/src/TabContext/TabContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ if (process.env.NODE_ENV !== 'production') {
Context.displayName = 'TabContext';
}

function useUniquePrefix() {
const [id, setId] = React.useState(null);
React.useEffect(() => {
setId(`mui-p-${Math.round(Math.random() * 1e5)}`);
}, []);
return id;
function useUniquePrefix(idPrefix) {
const ref = React.useRef()
if (!ref.current || (typeof idPrefix === 'string' && ref.current !== idPrefix)) {
ref.current = idPrefix || `mui-p-${Math.round(Math.random() * 1e5)}`
}
return ref.current;
}

export default function TabContext(props) {
const { children, value } = props;
const idPrefix = useUniquePrefix();
const { children, value, idPrefix: idPrefixProp } = props;
const idPrefix = useUniquePrefix(idPrefixProp);

const context = React.useMemo(() => {
return { idPrefix, value };
Expand All @@ -37,6 +37,10 @@ TabContext.propTypes /* remove-proptypes */ = {
* The content of the component.
*/
children: PropTypes.node,
/**
* The optional id prefix, internally used to render buttons
*/
idPrefix: PropTypes.string,
/**
* The value of the currently selected `Tab`.
*/
Expand Down
Loading