-
-
Notifications
You must be signed in to change notification settings - Fork 448
fix: migrate from defaultProps to default parameters in all components #587
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
Open
alowelter
wants to merge
5
commits into
reactjs:main
Choose a base branch
from
alowelter:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7cd797
fix: replace defaultProps with default parameters in Tabs component
alowelter baaee2d
fix: replace defaultProps with default parameters TabPanel
alowelter 239e822
fix: replace defaultProps with default parameters TabList
alowelter e523b00
fix: replace defaultProps with default parameters in Tab component
alowelter ab7eca2
fix: Check for conflicting props em Tabs component
Dobain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,39 +10,11 @@ import { getTabsCount } from '../helpers/count'; | |
|
||
const MODE_CONTROLLED = 0; | ||
const MODE_UNCONTROLLED = 1; | ||
|
||
const propTypes = { | ||
children: childrenPropType, | ||
onSelect: onSelectPropType, | ||
selectedIndex: selectedIndexPropType, | ||
/* | ||
Left for TS migration | ||
className: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.array, | ||
PropTypes.object, | ||
]), | ||
defaultFocus: PropTypes.bool, | ||
defaultIndex: PropTypes.number, | ||
direction: PropTypes.oneOf(['rtl', 'ltr']), | ||
disabledTabClassName: PropTypes.string, | ||
disableUpDownKeys: PropTypes.bool, | ||
disableLeftRightKeys: PropTypes.bool, | ||
domRef: PropTypes.func, | ||
environment: PropTypes.object, | ||
focusTabOnClick: PropTypes.bool, | ||
forceRenderTabPanel: PropTypes.bool, | ||
selectedTabClassName: PropTypes.string, | ||
selectedTabPanelClassName: PropTypes.string,*/ | ||
}; | ||
const defaultProps = { | ||
defaultFocus: false, | ||
focusTabOnClick: true, | ||
forceRenderTabPanel: false, | ||
selectedIndex: null, | ||
defaultIndex: null, | ||
environment: null, | ||
disableUpDownKeys: false, | ||
disableLeftRightKeys: false, | ||
}; | ||
|
||
const getModeFromProps = (props) => { | ||
|
@@ -69,23 +41,32 @@ For more information about controlled and uncontrolled mode of react-tabs see ht | |
* focus: Because we never remove focus from the Tabs this state is only used to indicate that we should focus the current tab. | ||
* 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) => { | ||
checkPropTypes(propTypes, props, 'prop', 'Tabs'); | ||
const { | ||
children, | ||
defaultFocus, | ||
defaultIndex, | ||
focusTabOnClick, | ||
onSelect, | ||
...attributes | ||
} = { | ||
...defaultProps, | ||
...props, | ||
}; | ||
const Tabs = ({ | ||
children, | ||
defaultFocus = false, | ||
defaultIndex = null, | ||
focusTabOnClick = true, | ||
forceRenderTabPanel = false, | ||
selectedIndex = null, | ||
environment = null, | ||
disableUpDownKeys = false, | ||
disableLeftRightKeys = false, | ||
onSelect, | ||
...attributes | ||
}) => { | ||
checkPropTypes(propTypes, { children, onSelect, selectedIndex }, 'prop', 'Tabs'); | ||
|
||
// Check for conflicting props | ||
if (process.env.NODE_ENV !== 'production' && selectedIndex !== null && defaultIndex !== null) { | ||
console.error( | ||
'The prop `selectedIndex` cannot be used together with `defaultIndex` in `Tabs`.\n' + | ||
'Either remove `selectedIndex` to let `Tabs` handle the selected tab internally or remove `defaultIndex` to handle it yourself.' | ||
); | ||
} | ||
Comment on lines
+60
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this added? Maybe this conversation from proptypes here should be done in a separate PR. |
||
|
||
const [focus, setFocus] = useState(defaultFocus); | ||
const [mode] = useState(getModeFromProps(attributes)); | ||
const [selectedIndex, setSelectedIndex] = useState( | ||
const [mode] = useState(getModeFromProps({ selectedIndex })); | ||
const [selectedIndexState, setSelectedIndexState] = useState( | ||
mode === MODE_UNCONTROLLED ? defaultIndex || 0 : null, | ||
); | ||
|
||
|
@@ -98,14 +79,14 @@ const Tabs = (props) => { | |
// Ensure that we handle removed tabs and don't let selectedIndex get out of bounds | ||
const tabsCount = getTabsCount(children); | ||
useEffect(() => { | ||
if (selectedIndex != null) { | ||
if (selectedIndexState != null) { | ||
const maxTabIndex = Math.max(0, tabsCount - 1); | ||
setSelectedIndex(Math.min(selectedIndex, maxTabIndex)); | ||
setSelectedIndexState(Math.min(selectedIndexState, maxTabIndex)); | ||
} | ||
}, [tabsCount]); | ||
} | ||
|
||
checkForIllegalModeChange(attributes, mode); | ||
checkForIllegalModeChange({ selectedIndex }, mode); | ||
|
||
const handleSelected = (index, last, event) => { | ||
// Call change event handler | ||
|
@@ -121,21 +102,35 @@ const Tabs = (props) => { | |
|
||
if (mode === MODE_UNCONTROLLED) { | ||
// Update selected index | ||
setSelectedIndex(index); | ||
setSelectedIndexState(index); | ||
} | ||
}; | ||
|
||
let subProps = { ...props, ...attributes }; | ||
let subProps = { | ||
children, | ||
defaultFocus, | ||
defaultIndex, | ||
focusTabOnClick, | ||
forceRenderTabPanel, | ||
selectedIndex, | ||
environment, | ||
disableUpDownKeys, | ||
disableLeftRightKeys, | ||
onSelect, | ||
...attributes | ||
}; | ||
|
||
subProps.focus = focus; | ||
subProps.onSelect = handleSelected; | ||
|
||
if (selectedIndex != null) { | ||
subProps.selectedIndex = selectedIndex; | ||
if (selectedIndexState != null) { | ||
subProps.selectedIndex = selectedIndexState; | ||
} | ||
|
||
delete subProps.defaultFocus; | ||
delete subProps.defaultIndex; | ||
delete subProps.focusTabOnClick; | ||
|
||
return <UncontrolledTabs {...subProps}>{children}</UncontrolledTabs>; | ||
}; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaultIndex seems missing here. OR was there a reason to no supply all props?