Skip to content

Commit

Permalink
fix: refactor defaultProps to disable warning from react (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Tschinder <231804+danez@users.noreply.github.com>
  • Loading branch information
leventekk and danez authored Apr 21, 2023
1 parent 63ef752 commit dc34bdb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/components/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ const Tab = (props) => {
tabIndex,
tabRef,
...attributes
} = props;
} = {
...defaultProps,
...props,
};

useEffect(() => {
if (selected && focus) {
Expand Down Expand Up @@ -78,8 +81,8 @@ const Tab = (props) => {
</li>
);
};
Tab.propTypes = propTypes;

Tab.propTypes = propTypes;
Tab.tabsRole = 'Tab';
Tab.defaultProps = defaultProps;

export default Tab;
7 changes: 5 additions & 2 deletions src/components/TabList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const propTypes = {
]),
};
const TabList = (props) => {
const { children, className, ...attributes } = props;
const { children, className, ...attributes } = {
...defaultProps,
...props,
};

return (
<ul {...attributes} className={cx(className)} role="tablist">
Expand All @@ -25,5 +28,5 @@ const TabList = (props) => {

TabList.tabsRole = 'TabList';
TabList.propTypes = propTypes;
TabList.defaultProps = defaultProps;

export default TabList;
7 changes: 5 additions & 2 deletions src/components/TabPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const TabPanel = (props) => {
selected,
selectedClassName,
...attributes
} = props;
} = {
...defaultProps,
...props,
};

return (
<div
Expand All @@ -48,5 +51,5 @@ const TabPanel = (props) => {

TabPanel.tabsRole = 'TabPanel';
TabPanel.propTypes = propTypes;
TabPanel.defaultProps = defaultProps;

export default TabPanel;
20 changes: 14 additions & 6 deletions src/components/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,20 @@ For more information about controlled and uncontrolled mode of react-tabs see ht
* 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.
*/
const Tabs = (props) => {
const { children, defaultFocus, defaultIndex, focusTabOnClick, onSelect } =
props;
const {
children,
defaultFocus,
defaultIndex,
focusTabOnClick,
onSelect,
...attributes
} = {
...defaultProps,
...props,
};

const [focus, setFocus] = useState(defaultFocus);
const [mode] = useState(getModeFromProps(props));
const [mode] = useState(getModeFromProps(attributes));
const [selectedIndex, setSelectedIndex] = useState(
mode === MODE_UNCONTROLLED ? defaultIndex || 0 : null,
);
Expand All @@ -93,7 +102,7 @@ const Tabs = (props) => {
}, [tabsCount]);
}

checkForIllegalModeChange(props, mode);
checkForIllegalModeChange(attributes, mode);

const handleSelected = (index, last, event) => {
// Call change event handler
Expand All @@ -113,7 +122,7 @@ const Tabs = (props) => {
}
};

let subProps = { ...props };
let subProps = { ...props, ...attributes };

subProps.focus = focus;
subProps.onSelect = handleSelected;
Expand All @@ -128,7 +137,6 @@ const Tabs = (props) => {
};

Tabs.propTypes = propTypes;
Tabs.defaultProps = defaultProps;
Tabs.tabsRole = 'Tabs';

export default Tabs;
8 changes: 6 additions & 2 deletions src/components/UncontrolledTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ const UncontrolledTabs = (props) => {
disableUpDownKeys, // unused
disableLeftRightKeys, // unused
...attributes
} = props;
} = {
...defaultProps,
...props,
};
return (
<div
{...attributes}
Expand All @@ -400,6 +403,7 @@ const UncontrolledTabs = (props) => {
</div>
);
};
UncontrolledTabs.defaultProps = defaultProps;

UncontrolledTabs.propTypes = propTypes;

export default UncontrolledTabs;

0 comments on commit dc34bdb

Please sign in to comment.