-
Notifications
You must be signed in to change notification settings - Fork 26
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
refactor(primary-action-dropdown): use hooks #965
Conversation
this.setState({ isOpen: false }); | ||
}; | ||
React.useEffect(() => { | ||
window.addEventListener('click', handleGlobalClick); |
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.
this replaces the componentDidMount
and componentWillUnmount
|
||
act(() => { |
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.
needed this or else got warnings in the test
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.
Cool that it worked.
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.
Since RTL already includes this, can you maybe try to click on the rendered container?
fireEvent.click(rendered.container)
There should be no need for using act
directly
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.
Fixed here: b83d74f
src/components/dropdowns/primary-action-dropdown/primary-action-dropdown.js
Show resolved
Hide resolved
isOpen: false, | ||
}; | ||
const PrimaryActionDropdown = props => { | ||
const ref = React.useRef(); |
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.
Can we name the hook better?
|
||
act(() => { |
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.
Cool that it worked.
@@ -230,7 +230,7 @@ const PrimaryActionDropdown = props => { | |||
return () => { | |||
window.removeEventListener('click', handleGlobalClick); | |||
}; | |||
}, [handleGlobalClick]); | |||
}); |
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.
We want it to run once not on each render right. So empty array?
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.
good call
setIsOpen(false); | ||
} | ||
}, | ||
[ref] |
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.
I think I saw an issue or something related to defining DOM elements (refs) as dependencies. I’ll see if I can find more about it but maybe we should double check if it makes sense to do so.
src/components/dropdowns/primary-action-dropdown/primary-action-dropdown.js
Show resolved
Hide resolved
src/components/dropdowns/primary-action-dropdown/primary-action-dropdown.js
Show resolved
Hide resolved
<DropdownHead | ||
iconLeft={primaryOption.props.iconLeft} | ||
isDisabled={primaryOption.props.isDisabled} | ||
onClick={isOpen ? () => setIsOpen(false) : primaryOption.props.onClick} |
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.
Nitpick: memoized callback?
chevron={ | ||
<DropdownChevron | ||
ref={ref} | ||
onClick={ |
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.
Same here?
|
||
act(() => { |
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.
Since RTL already includes this, can you maybe try to click on the rendered container?
fireEvent.click(rendered.container)
There should be no need for using act
directly
3872bcf
to
b83d74f
Compare
b83d74f
to
5dfdb54
Compare
componentDidMount() { | ||
window.addEventListener('click', this.handleGlobalClick); | ||
} | ||
const { onClick } = primaryOption.props; |
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.
Same drill here again
src/index.js
Outdated
@@ -141,3 +140,5 @@ export { default as version } from './version'; | |||
export { | |||
default as ContentNotification, | |||
} from './components/notifications/content-notification'; | |||
|
|||
export * from './hooks'; |
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.
Extra unrelated little change.
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.
Does UIKit really want to export it's hooks already? This would maybe even a feature releasea as a result.
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.
It was already exposed actually, I just refactored it to export *
instead of export { default as useToggleState }
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.
Hm. I, personal opinion ahead, liked it before more then. The library opts into what it wants to expose instead of defaulting to everything.
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.
Sure my bad, I'll revert this change then
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.
Summary
Refactors the component
PrimaryActionDropdown
to use hooks