-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[@mantine/core] [docs] Fix allowStepSelect regression #3340
Merged
rtivital
merged 2 commits into
mantinedev:master
from
rmzNadir:fix/stepper-allow-step-select
Jan 17, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -9,29 +9,57 @@ import { Stepper, Button, Group } from '@mantine/core'; | |
|
||
function Demo() { | ||
const [active, setActive] = useState(1); | ||
const nextStep = () => setActive((current) => (current < 3 ? current + 1 : current)); | ||
const prevStep = () => setActive((current) => (current > 0 ? current - 1 : current)); | ||
const [highestStepVisited, setHighestStepVisited] = useState(active); | ||
|
||
const handleStepChange = (nextStep: number) => { | ||
const isOutOfBounds = nextStep > 3 || nextStep < 0; | ||
|
||
if (isOutOfBounds) { | ||
return; | ||
} | ||
|
||
setActive(nextStep); | ||
setHighestStepVisited((hSC) => Math.max(hSC, nextStep)); | ||
}; | ||
|
||
// Allow the user to freely go back and forth between visited steps. | ||
const shouldAllowSelectStep = (step: number) => highestStepVisited >= step && active !== step; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this variant of the demo, good idea! |
||
|
||
return ( | ||
<> | ||
<Stepper active={active} onStepClick={setActive} breakpoint="sm"> | ||
<Stepper.Step label="First step" description="Create an account" allowStepSelect={active > 0}> | ||
Step 1 content: Create an account | ||
<Stepper.Step | ||
label="First step" | ||
description="Create an account" | ||
allowStepSelect={shouldAllowSelectStep(0)} | ||
> | ||
<Content>Step 1 content: Create an account</Content> | ||
</Stepper.Step> | ||
<Stepper.Step label="Second step" description="Verify email" allowStepSelect={active > 1}> | ||
Step 2 content: Verify email | ||
<Stepper.Step | ||
label="Second step" | ||
description="Verify email" | ||
allowStepSelect={shouldAllowSelectStep(1)} | ||
> | ||
<Content>Step 2 content: Verify email</Content> | ||
</Stepper.Step> | ||
<Stepper.Step label="Final step" description="Get full access" allowStepSelect={active > 2}> | ||
Step 3 content: Get full access | ||
<Stepper.Step | ||
label="Final step" | ||
description="Get full access" | ||
allowStepSelect={shouldAllowSelectStep(2)} | ||
> | ||
<Content>Step 3 content: Get full access</Content> | ||
</Stepper.Step> | ||
|
||
<Stepper.Completed> | ||
Completed, click back button to get to previous step | ||
<Content>Completed, click back button to get to previous step</Content> | ||
</Stepper.Completed> | ||
</Stepper> | ||
|
||
<Group position="center" mt="xl"> | ||
<Button variant="default" onClick={prevStep}>Back</Button> | ||
<Button onClick={nextStep}>Next step</Button> | ||
<Button variant="default" onClick={() => handleStepChange(active - 1)}> | ||
Back | ||
</Button> | ||
<Button onClick={() => handleStepChange(active + 1)}>Next step</Button> | ||
</Group> | ||
</> | ||
); | ||
|
@@ -40,23 +68,44 @@ function Demo() { | |
|
||
function Demo() { | ||
const [active, setActive] = useState(1); | ||
const nextStep = () => setActive((current) => (current < 3 ? current + 1 : current)); | ||
const prevStep = () => setActive((current) => (current > 0 ? current - 1 : current)); | ||
const [highestStepVisited, setHighestStepVisited] = useState(active); | ||
|
||
const handleStepChange = (nextStep: number) => { | ||
const isOutOfBounds = nextStep > 3 || nextStep < 0; | ||
|
||
if (isOutOfBounds) { | ||
return; | ||
} | ||
|
||
setActive(nextStep); | ||
setHighestStepVisited((hSC) => Math.max(hSC, nextStep)); | ||
}; | ||
|
||
// Allow the user to freely go back and forth between visited steps. | ||
const shouldAllowSelectStep = (step: number) => highestStepVisited >= step && active !== step; | ||
|
||
return ( | ||
<> | ||
<Stepper active={active} onStepClick={setActive} breakpoint="sm"> | ||
<Stepper.Step | ||
label="First step" | ||
description="Create an account" | ||
allowStepSelect={active > 0} | ||
allowStepSelect={shouldAllowSelectStep(0)} | ||
> | ||
<Content>Step 1 content: Create an account</Content> | ||
</Stepper.Step> | ||
<Stepper.Step label="Second step" description="Verify email" allowStepSelect={active > 1}> | ||
<Stepper.Step | ||
label="Second step" | ||
description="Verify email" | ||
allowStepSelect={shouldAllowSelectStep(1)} | ||
> | ||
<Content>Step 2 content: Verify email</Content> | ||
</Stepper.Step> | ||
<Stepper.Step label="Final step" description="Get full access" allowStepSelect={active > 2}> | ||
<Stepper.Step | ||
label="Final step" | ||
description="Get full access" | ||
allowStepSelect={shouldAllowSelectStep(2)} | ||
> | ||
<Content>Step 3 content: Get full access</Content> | ||
</Stepper.Step> | ||
|
||
|
@@ -66,10 +115,10 @@ function Demo() { | |
</Stepper> | ||
|
||
<Group position="center" mt="xl"> | ||
<Button variant="default" onClick={prevStep}> | ||
<Button variant="default" onClick={() => handleStepChange(active - 1)}> | ||
Back | ||
</Button> | ||
<Button onClick={nextStep}>Next step</Button> | ||
<Button onClick={() => handleStepChange(active + 1)}>Next step</Button> | ||
</Group> | ||
</> | ||
); | ||
|
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.
Giving priority to the Step's allowStepSelect prop made sense for more fine grain control over the step selection.
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.
Indeed, and here was actually the typo I added in the previous code... I forgot a
&&
operator, with the "typeof item.props..." part staying alone on its own line for nothing.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.
In terms of readability I agree with your change; in terms of performance since this is in a loop, the added function call might not be the best idea. Then again, I don't imagine a stepper component getting tens or hundreds of steps :)
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.
The following will achieve the same, without the function call:
But it's arguably more difficult to read.
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.
The performance change is so negligible (even with a thousand steps) that it may as well be part of the margin of error and at that point I'd rather favor readability
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.
Yeah I agree. This was definitely a nitpick.