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

Feature/allow zero expanded #160

Merged
merged 5 commits into from Jan 21, 2019
Merged

Feature/allow zero expanded #160

merged 5 commits into from Jan 21, 2019

Conversation

catepalmer
Copy link
Contributor

I've added an allowZeroExpanded prop (as outlined in issue #153) that determines whether a 'close' action should be blocked if the item is currently the only one open. By default this prop is set to false, but it can be set to true in order to enable this functionality. I've also updated and added tests to cover this feature.

Copy link
Contributor

@ryami333 ryami333 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 PR @catepalmer. I think in this case you might have misunderstood the desired functionality. You've focused on item removal (AccordionContainer@removeItem), whereas I think you ought to be focussed on AccordionContainer.setExpanded.

Ultimately, the desired outcome looks something like this:

User clicks the only expanded AccordionItem - is collapse permitted?

Conditions Collapse Permitted
allowZeroExpanded={false} (default) No
allowZeroExpanded={true} Yes

EDITED: Table above originally had "Collapse permitted" round the wrong way, as mentioned in subsequent conversation.

@@ -23,7 +23,7 @@ describe('Accordion', () => {

function mountAccordion(): ReactWrapper {
return mount(
<Accordion allowMultipleExpanded={false}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice - that was redundant wasn't it

const { allowMultipleExpanded, onChange, ...rest } = this.props;
const {
allowMultipleExpanded,
allowZeroExpanded,
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch. Would have caused console errors otherwise :)

);
});

it("setting the expanded property to false in an accordion that doesn't allow zero expansions when there is only one item expanded doesn't close that item", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests raise some questions about whether or not we should support 'controlled' accordions (ie. the expanded prop). We basically have two conflicting mechanisms for state management, and one can be leveraged/abused to make the accordion not-accessible. Nothing to action here, just a mental note about reviewing the expanded prop functionality (will raise an issue/proposal).

@catepalmer
Copy link
Contributor Author

Thanks for the PR @catepalmer. I think in this case you might have misunderstood the desired functionality. You've focused on item removal (AccordionContainer@removeItem), whereas I think you ought to be focussed on AccordionContainer.setExpanded.

Ah, whoops. I've changed it now so that the new functionality is in AccordionContainer.setExpanded rather than AccordionContainer.removeItem. Hopefully it'll be more on the right track when I push the changes up this time.

Ultimately, the desired outcome looks something like this:

Conditions Collapse Permitted
allowZeroExpanded={false} (default) Yes
allowZeroExpanded={true} No

Hmm, just to check here - my understanding was that when allowZeroExpanded is set to false, collapse is not permitted when there's one only item open, and vice versa when it's set to true.

@coveralls
Copy link

coveralls commented Jan 21, 2019

Coverage Status

Coverage increased (+0.03%) to 99.425% when pulling 6303e17 on feature/allow-zero-expanded into 41bfab5 on next.

@ryami333
Copy link
Contributor

Hmm, just to check here - my understanding was that when allowZeroExpanded is set to false, collapse is not permitted when there's one only item open, and vice versa when it's set to true.

@catepalmer yes you got it! Sorry, I'll update my comment above to reflect this.

Copy link
Contributor

@ryami333 ryami333 left a comment

Choose a reason for hiding this comment

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

Getting pretty close!

...item,
expanded,
};
if (
Copy link
Contributor

@ryami333 ryami333 Jan 21, 2019

Choose a reason for hiding this comment

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

Two notes on this little block:

  • It could be moved above the this.setState block and just do return; to bail out. That way, if the conditions are met then setState is never called at all, and an unnecessary re-render is prevented.
  • The .length < 2 is slightly ambiguous. I think more correctly it would be .length === 1, because this logic should not apply if length = 0 or if length = 1.5, for example. Both of those examples would not eventuate in practice (particularly 1.5 haha) but still doesn't hurt to be explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yep, good thinking - just made those changes 👍

children?: React.ReactNode;
items?: Item[];
onChange?(args: UUID[]): void;
};

export type AccordionContainer = {
allowMultipleExpanded: boolean;
allowZeroExpanded?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that this is an optional (?) property of this type. It is optional as a prop though (ProviderProps), just not here.

Copy link
Contributor

@ryami333 ryami333 left a comment

Choose a reason for hiding this comment

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

Nicely done, thanks for persisting with some patchy (and incorrect :-P) feedback.

@catepalmer catepalmer merged commit 3d7950e into next Jan 21, 2019
@catepalmer catepalmer deleted the feature/allow-zero-expanded branch January 21, 2019 23:14
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.

None yet

3 participants