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 Dropdown to use functional component #23142

Merged
merged 1 commit into from
Jun 24, 2020

Conversation

sc81
Copy link
Contributor

@sc81 sc81 commented Jun 14, 2020

  • Refactor tests

  • Refactor DropdownMenu tests to get rid of weird errors

  • Add documentation for onToggle and onClose properties

Description

How has this been tested?

Screenshots

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@ZebulanStanphill ZebulanStanphill added [Type] Code Quality Issues or PRs that relate to code quality [Feature] UI Components Impacts or related to the UI component system [Package] Components /packages/components labels Jun 15, 2020
Comment on lines 30 to 44
const [ isOpen, setIsOpen ] = useState( false );
const isFirstRender = useRef( true );

this.toggle = this.toggle.bind( this );
this.close = this.close.bind( this );
this.closeIfFocusOutside = this.closeIfFocusOutside.bind( this );

this.containerRef = createRef();

this.state = {
isOpen: false,
};
}

componentWillUnmount() {
const { isOpen } = this.state;
const { onToggle } = this.props;
if ( isOpen && onToggle ) {
onToggle( false );
}
}

componentDidUpdate( prevProps, prevState ) {
const { isOpen } = this.state;
const { onToggle } = this.props;
if ( prevState.isOpen !== isOpen && onToggle ) {
useEffect( () => {
if ( isFirstRender.current ) {
isFirstRender.current = false;
} else if ( onToggle ) {
onToggle( isOpen );
}
}
return () => {
if ( isOpen && onToggle ) {
onToggle( false );
}
};
}, [ isFirstRender, isOpen, onToggle ] );
Copy link
Contributor

@talldan talldan Jun 16, 2020

Choose a reason for hiding this comment

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

I might be missing an implementation detail, but I think the goal here is to trigger onToggle when the state changes, and that could also be handled now as a custom hook, something like the following untested pseudocode:

// Custom hook.
function useObservableState( initialState, onStateChange ) {
    const [ value, setValue ] = useState( initialState );
    return [ value, ( newValue ) => { 
        setValue( newValue ); 
        onStateChange( newValue );
    } ];
}

// ...

// Usage in the component.
const [ isOpen, setIsOpen ] = useObservableState( false, onToggle );

(I would put this hook function above the component in the same file)

Would still also need some separate handling for unmounting, but that then becomes simpler:

// Fire `onToggle` when the component unmounts.
useEffect( () => () => onToggle( false ) );

Copy link
Contributor Author

@sc81 sc81 Jun 16, 2020

Choose a reason for hiding this comment

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

Yes. This should work. I'll check it later.

Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution @sc81. It's great that the docs and tests were also updated as part of this.

I notice that tests are failing at the moment. I think it's worth trying to rebase the PR against the master branch and pushing again, as I think some code that fixes broken tests was merged to master.

* Refactor tests

* Refactor DropdownMenu tests to get rid of weird errors

* Add documentation for onToggle and onClose properties

* Add useObservableState
@sc81 sc81 force-pushed the refactor/dropdown branch from c1b9c0b to ea519f2 Compare June 17, 2020 13:24
@youknowriad
Copy link
Contributor

@talldan Should we merge this?

@talldan
Copy link
Contributor

talldan commented Jun 24, 2020

Yep, restarted the tests and they look good now 👍

Thanks again for all your work here @sc81.

@talldan talldan merged commit 12b39d0 into WordPress:master Jun 24, 2020
@github-actions github-actions bot added this to the Gutenberg 8.5 milestone Jun 24, 2020
@sc81
Copy link
Contributor Author

sc81 commented Jun 24, 2020

No problem. And thank you @talldan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants