-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
PanelBody refactored into functional component #23065
Conversation
I've got 2 more tests to fix before this passes. Not sure how I can get the state from a functional component |
This line is a test of the variables inside the function. @youknowriad I'd be happy to give me your idea. |
Ideally, we avoid testing the internal state, we can test the DOM output instead maybe? |
I've added a new DOM attribute to expose the open state of the panel. In the original tests we can test if the panel is open by seeing if the Here's an example of a test where the internal state is false, but the component is forced open via a prop.
panelBody now has
I had to update some snapshots but this passes the tests now. I'd like some feedback on this approach since I'm not sure if this is the best way to do this |
In hindsight a simpler approach is to remove the state check from the "opened" prop tests since the final state of the panel depends on the opened prop and not the state |
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.
Some minor stylistic nitpicks, but otherwise this looks good to go.
initialOpen, | ||
onToggle, | ||
} ) { | ||
const [ isOpenedState, setOpenedState ] = useState( | ||
initialOpen === undefined ? true : initialOpen | ||
); |
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 can be simplified using a default parameter.
initialOpen, | |
onToggle, | |
} ) { | |
const [ isOpenedState, setOpenedState ] = useState( | |
initialOpen === undefined ? true : initialOpen | |
); | |
initialOpen = true, | |
onToggle, | |
} ) { | |
const [ isOpenedState, setOpenedState ] = useState( initialOpen ); |
const [ isOpenedState, setOpenedState ] = useState( | ||
initialOpen === undefined ? true : initialOpen | ||
); | ||
const isOpened = opened === undefined ? isOpenedState : opened; |
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 can be simplified using nullish coalescing:
const isOpened = opened === undefined ? isOpenedState : opened; | |
const isOpened = opened ?? isOpenedState; |
if ( onToggle ) { | ||
onToggle(); | ||
} |
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 can be simplified using optional chaining:
if ( onToggle ) { | |
onToggle(); | |
} | |
onToggle?.(); |
(Also, it needs a rebase.) |
Closing this, seems to have been superseded by #23327 |
Description
Related to #22890
How has this been tested?
I've tested the panels in the editor and in storybook and it seems to work fine. However the unit tests are failing. From what I can tell this seems to be because the test can't check the state if we're using React hooks
Types of changes
Refactored PanelBody into a functional component and used React hooks.
Checklist: