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

fix(Section): defaultIsOpen should behave as expected #6326

Merged

Conversation

nerdstep
Copy link
Contributor

Fixes #6322

Fixes #0000

Checklist

  • Includes tests
  • Update documentation

Changes proposed in this pull request:

Negate defaultIsOpen value provided to useState so that it matches the expected isCollapsed behavior.

Reviewers should focus on:

Whether this fixes the desired behavior.

Screenshot

when defaultIsOpen={true}

image

when defaultIsOpen={false}

image

@palantirtech
Copy link
Member

Thanks for your interest in palantir/blueprint, @nerdstep! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@nerdstep nerdstep changed the title Nerdstep/fix section default is open fix(Section): defaultIsOpen should behave as expected Aug 17, 2023
Copy link
Contributor

@gluxon gluxon 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 the help fixing this!

@@ -109,7 +112,7 @@ export const Section: React.FC<SectionProps> = React.forwardRef((props, ref) =>
title,
...htmlProps
} = props;
const [isCollapsed, setIsCollapsed] = React.useState<boolean>(collapseProps?.defaultIsOpen ?? false);
const [isCollapsed, setIsCollapsed] = React.useState<boolean>(!collapseProps?.defaultIsOpen ?? false);
Copy link
Contributor

Choose a reason for hiding this comment

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

The ! turns collapseProps?.defaultIsOpen into a boolean. Is ?? false needed if the left side will never be undefined or null?

It is a bit verbose, but I think collapseProps?.defaultIsOpen ?? true conveys the new @default true pretty well. Perhaps we should invert after performing that defaulting logic?

Suggested change
const [isCollapsed, setIsCollapsed] = React.useState<boolean>(!collapseProps?.defaultIsOpen ?? false);
const [isCollapsed, setIsCollapsed] = React.useState<boolean>(!(collapseProps?.defaultIsOpen ?? true));

Copy link
Contributor Author

@nerdstep nerdstep Aug 18, 2023

Choose a reason for hiding this comment

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

Agreed, that does make for more readable code! Updated the PR with the suggested change, including a comment to better explain the behavior. Also added a new test to assert the defaultIsOpen={undefined} case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

@pgoldberg pgoldberg left a comment

Choose a reason for hiding this comment

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

Thanks!

@pgoldberg pgoldberg merged commit f113fc1 into palantir:develop Aug 18, 2023
// re-mount when `defaultIsOpen` is changed, otherwise
// the local state in the `Collapse` component is not
// updated.
key={String(defaultIsOpen)}
Copy link
Contributor

Choose a reason for hiding this comment

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

Using key to reset state and animations is one of my favorite tricks in the pocket too. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha yeah it's a neat little trick! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Section defaultIsOpen does the opposite of the provided boolean
4 participants