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

Expose validation state via Controls context #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Props = {|
isLast: boolean,
isLoading: boolean,
activeIndex: number,
isStepValid: boolean,
) => React$Node,
|};

Expand All @@ -22,9 +23,18 @@ const Controls = ({ render }: Props) => {
isLast,
isLoading,
activeIndex,
isStepValid,
} = useContext(ControlsContext);

return render(onNext, onPrevious, isFirst, isLast, isLoading, activeIndex);
return render(
onNext,
onPrevious,
isFirst,
isLast,
isLoading,
activeIndex,
isStepValid,
);
};

export default Controls;
14 changes: 14 additions & 0 deletions src/Wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ type Props = {|
const Wizard = ({ children, onComplete, stateManager, debug }: Props) => {
const [index, setIndex] = useState<number | null>(null);
const [steps, setSteps] = useState<Array<Losen$Step>>([]);
const [validSteps, setValidSteps] = useState<Array<boolean>>([]);
const [isLoading, setLoadingState] = useState(false);

function registerStep(step) {
const alreadyRegistered = steps.map(el => el.name).includes(step.name);
if (!alreadyRegistered) {
setSteps(previousSteps => [...previousSteps, step]);
setValidSteps(previousSteps => [...previousSteps, true]);
}
}

Expand All @@ -35,11 +37,20 @@ const Wizard = ({ children, onComplete, stateManager, debug }: Props) => {
]);
}

function updateValidSteps(stepIndex, isValid) {
setValidSteps(previousSteps => [
...previousSteps.slice(0, stepIndex),
isValid,
...previousSteps.slice(stepIndex + 1),
]);
}

async function onNext() {
if (index === null) {
return;
}
const { validator } = steps[index];

const next = findNextValid(steps, index);

const nextAction =
Expand All @@ -51,8 +62,10 @@ const Wizard = ({ children, onComplete, stateManager, debug }: Props) => {
try {
setLoadingState(true);
await validator();
updateValidSteps(index, true);
nextAction();
} catch (error) {
updateValidSteps(index, false);
if (error instanceof ValidationError) {
console.error('ReactLosen', error); // eslint-disable-line
} else {
Expand Down Expand Up @@ -124,6 +137,7 @@ const Wizard = ({ children, onComplete, stateManager, debug }: Props) => {
index !== null ? findPreviousValid(steps, index) === index : false,
isLast: index !== null ? findNextValid(steps, index) === index : false,
activeIndex: index,
isStepValid: index !== null && validSteps ? validSteps[index] : true,
}}>
<StepContext.Provider
value={{
Expand Down