Skip to content
Open
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
3 changes: 2 additions & 1 deletion docs/pages/material-ui/api/tab-context.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": { "name": "union", "description": "number<br>&#124;&nbsp;string" },
"required": true
},
"children": { "type": { "name": "node" } }
"children": { "type": { "name": "node" } },
"idGenerator": { "type": { "name": "func" } }
},
"name": "TabContext",
"imports": [
Expand Down
3 changes: 3 additions & 0 deletions docs/translations/api-docs/tab-context/tab-context.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"componentDescription": "",
"propDescriptions": {
"children": { "description": "The content of the component." },
"idGenerator": {
"description": "The optional id generator, internally used to render buttons, falls back to Math.random"
},
"value": { "description": "The value of the currently selected <code>Tab</code>." }
},
"classDescriptions": {}
Expand Down
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 | number;
/**
* The optional id generator, internally used to render buttons, falls back to Math.random
*/
idGenerator?: () => number;
}
/**
*
Expand Down
14 changes: 9 additions & 5 deletions packages/mui-lab/src/TabContext/TabContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ if (process.env.NODE_ENV !== 'production') {
Context.displayName = 'TabContext';
}

function useUniquePrefix() {
function useUniquePrefix(idGenerator = Math.random) {
const [id, setId] = React.useState(null);
React.useEffect(() => {
setId(`mui-p-${Math.round(Math.random() * 1e5)}`);
}, []);
setId(`mui-p-${Math.round(idGenerator() * 1e5)}`);
}, [idGenerator]);
return id;
}

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

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