MCPcopy
hub / github.com/reactjs/react-tabs / Tabs

Function Tabs

src/components/Tabs.jsx:72–140  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

70 * It is initialized from the prop defaultFocus, and after the first render it is reset back to false. Later it can become true again when using keys to navigate the tabs.
71 */
72const Tabs = (props) => {
73 checkPropTypes(propTypes, props, 'prop', 'Tabs');
74 const {
75 children,
76 defaultFocus,
77 defaultIndex,
78 focusTabOnClick,
79 onSelect,
80 ...attributes
81 } = {
82 ...defaultProps,
83 ...props,
84 };
85
86 const [focus, setFocus] = useState(defaultFocus);
87 const [mode] = useState(getModeFromProps(attributes));
88 const [selectedIndex, setSelectedIndex] = useState(
89 mode === MODE_UNCONTROLLED ? defaultIndex || 0 : null,
90 );
91
92 useEffect(() => {
93 // Reset focus after initial render, see comment above
94 setFocus(false);
95 }, []);
96
97 if (mode === MODE_UNCONTROLLED) {
98 // Ensure that we handle removed tabs and don't let selectedIndex get out of bounds
99 const tabsCount = getTabsCount(children);
100 useEffect(() => {
101 if (selectedIndex != null) {
102 const maxTabIndex = Math.max(0, tabsCount - 1);
103 setSelectedIndex(Math.min(selectedIndex, maxTabIndex));
104 }
105 }, [tabsCount]);
106 }
107
108 checkForIllegalModeChange(attributes, mode);
109
110 const handleSelected = (index, last, event) => {
111 // Call change event handler
112 if (typeof onSelect === 'function') {
113 // Check if the change event handler cancels the tab change
114 if (onSelect(index, last, event) === false) return;
115 }
116
117 // Always set focus on tabs unless it is disabled
118 if (focusTabOnClick) {
119 setFocus(true);
120 }
121
122 if (mode === MODE_UNCONTROLLED) {
123 // Update selected index
124 setSelectedIndex(index);
125 }
126 };
127
128 let subProps = { ...props, ...attributes };
129

Callers

nothing calls this directly

Calls 3

getTabsCountFunction · 0.90
getModeFromPropsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…