-
Notifications
You must be signed in to change notification settings - Fork 905
chore: Change usage of ChooseOne (no final condition) #4158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,42 @@ | ||
import { Children, PropsWithChildren } from "react" | ||
|
||
export interface CondProps { | ||
condition: boolean | ||
condition?: boolean | ||
} | ||
|
||
/** | ||
* Wrapper component that attaches a condition to a child component so that ChooseOne can | ||
* determine which child to render. The last Cond in a ChooseOne is the fallback case; set | ||
* its `condition` to `true` to avoid confusion. | ||
* @param condition boolean expression indicating whether the child should be rendered | ||
* determine which child to render. The last Cond in a ChooseOne is the fallback case and | ||
* should not have a condition. | ||
* @param condition boolean expression indicating whether the child should be rendered, or undefined | ||
* @returns child. Note that Cond alone does not enforce the condition; it should be used inside ChooseOne. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export const Cond = ({ children, condition }: PropsWithChildren<CondProps>): JSX.Element => { | ||
export const Cond = ({ children }: PropsWithChildren<CondProps>): JSX.Element => { | ||
return <>{children}</> | ||
} | ||
|
||
/** | ||
* Wrapper component for rendering exactly one of its children. Wrap each child in Cond to associate it | ||
* with a condition under which it should be rendered. If no conditions are met, the final child | ||
* will be rendered. | ||
* @returns one of its children | ||
* @returns one of its children, or null if there are no children | ||
* @throws an error if its last child has a condition prop, or any non-final children do not have a condition prop | ||
*/ | ||
export const ChooseOne = ({ children }: PropsWithChildren): JSX.Element => { | ||
export const ChooseOne = ({ children }: PropsWithChildren): JSX.Element | null => { | ||
const childArray = Children.toArray(children) as JSX.Element[] | ||
const chosen = childArray.find((child) => child.props.condition) | ||
return chosen ?? childArray[childArray.length - 1] | ||
if (childArray.length === 0) { | ||
return null | ||
} | ||
const conditionedOptions = childArray.slice(0, childArray.length - 1) | ||
const defaultCase = childArray[childArray.length - 1] | ||
if (defaultCase.props.condition !== undefined) { | ||
throw new Error( | ||
"The last Cond in a ChooseOne was given a condition prop, but it is the default case.", | ||
) | ||
} | ||
if (conditionedOptions.some((cond) => cond.props.condition === undefined)) { | ||
throw new Error("A non-final Cond in a ChooseOne does not have a condition prop.") | ||
} | ||
const chosen = conditionedOptions.find((child) => child.props.condition) | ||
return chosen ?? defaultCase | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
idk if this is worth consideration but if given an empty array
defaultCase
would beundefined
I think?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.
Thanks! How does this look?
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.
Perfect!!