Skip to content
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

Merged
merged 7 commits into from
Jul 29, 2019
Merged

Conversation

montezume
Copy link
Contributor

Summary

Refactors the component PrimaryActionDropdown to use hooks

this.setState({ isOpen: false });
};
React.useEffect(() => {
window.addEventListener('click', handleGlobalClick);
Copy link
Contributor Author

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(() => {
Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool that it worked.

Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed here: b83d74f

isOpen: false,
};
const PrimaryActionDropdown = props => {
const ref = React.useRef();
Copy link
Contributor

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(() => {
Copy link
Contributor

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]);
});
Copy link
Contributor

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?

Copy link
Contributor Author

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]
Copy link
Member

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.

<DropdownHead
iconLeft={primaryOption.props.iconLeft}
isDisabled={primaryOption.props.isDisabled}
onClick={isOpen ? () => setIsOpen(false) : primaryOption.props.onClick}
Copy link
Member

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={
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here?


act(() => {
Copy link
Member

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

@emmenko emmenko force-pushed the ml-primary-action-hook branch from 3872bcf to b83d74f Compare July 29, 2019 08:30
@emmenko emmenko force-pushed the ml-primary-action-hook branch from b83d74f to 5dfdb54 Compare July 29, 2019 09:26
componentDidMount() {
window.addEventListener('click', this.handleGlobalClick);
}
const { onClick } = primaryOption.props;
Copy link
Member

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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra unrelated little change.

Copy link
Contributor

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.

Copy link
Member

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 }

Copy link
Contributor

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.

Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emmenko emmenko merged commit 9a03641 into master Jul 29, 2019
@emmenko emmenko deleted the ml-primary-action-hook branch July 29, 2019 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants