This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
[DEV-050] add wizard form #70
Open
AipioxTechson
wants to merge
3
commits into
develop
Choose a base branch
from
DEV-050
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,24 +1,21 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-return */ | ||
/* eslint-disable react/prop-types */ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-call */ | ||
import React from "react"; | ||
import { connect } from "react-redux"; | ||
import { useDispatch } from "react-redux"; | ||
|
||
import { FeaturesList } from "../components/FeaturesList"; | ||
import { HeroSection } from "../components/HeroSection"; | ||
import { displaySuccessSnack } from "../stores/uiSlice/actions"; | ||
|
||
export function Home({ displaySuccessSnack }): JSX.Element { | ||
export function Home(): JSX.Element { | ||
const dispatch = useDispatch(); | ||
return ( | ||
<> | ||
<HeroSection /> | ||
<FeaturesList /> | ||
<button onClick={() => displaySuccessSnack("hey lol")}>Click me</button> | ||
<button onClick={() => dispatch(displaySuccessSnack("hi lol"))}> | ||
Click me | ||
</button> | ||
</> | ||
); | ||
} | ||
|
||
export default connect(null, { | ||
displaySuccessSnack, | ||
})(Home); | ||
export default Home; | ||
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 |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { Form, FormikProps, withFormik } from "formik"; | ||
import React, { useState } from "react"; | ||
import * as Yup from "yup"; | ||
|
||
//Validation Schema | ||
const SignupSchema = Yup.object().shape({ | ||
Name: Yup.string().optional(), | ||
School: Yup.string().optional(), | ||
email: Yup.string().email("Invalid email").required("Required"), | ||
password: Yup.string().required("Required!"), //Update this with correct validation | ||
}); | ||
|
||
//Initial Values | ||
const initialValues = { | ||
name: "", | ||
school: "", | ||
email: "", | ||
password: "", | ||
}; | ||
|
||
//Key types | ||
type FormFields = keyof typeof initialValues; | ||
|
||
//Form interface | ||
interface FormValues { | ||
name: string; | ||
school: string; | ||
email: string; | ||
password: string; | ||
} | ||
|
||
//Props for each subcomponent | ||
interface SubFormProps { | ||
errors?: string; | ||
previousStep?: () => void; | ||
nextStep?: () => void; | ||
setValue: (value: string) => void; | ||
FormValue: FormFields; | ||
hasErrors: boolean; | ||
hasPreviousStep: boolean; | ||
hasNextStep: boolean; | ||
} | ||
|
||
//Mapping type | ||
interface SubForm { | ||
Component: (props: SubFormProps) => JSX.Element; | ||
key: FormFields; | ||
} | ||
|
||
const sampleComponent = ({ | ||
setValue, | ||
FormValue, | ||
hasPreviousStep, | ||
hasNextStep, | ||
nextStep, | ||
previousStep, | ||
}: SubFormProps) => { | ||
return ( | ||
<> | ||
<h1>{FormValue}</h1> | ||
{hasPreviousStep ? ( | ||
<button type="button" onClick={() => previousStep()}> | ||
{" "} | ||
PREVIOUS STEP | ||
</button> | ||
) : null} | ||
<button type="button" onClick={() => setValue("10@gmail.com")}> | ||
change Name | ||
</button> | ||
{hasNextStep ? ( | ||
<button type="button" onClick={() => nextStep()}> | ||
{" "} | ||
NEXT STEP | ||
</button> | ||
) : null} | ||
{FormValue === "password" ? <button type="submit">SUBMIT</button> : null} | ||
</> | ||
); | ||
}; | ||
|
||
//Mapping | ||
const SubForms: Array<SubForm> = [ | ||
{ | ||
Component: sampleComponent, | ||
key: "name", | ||
}, | ||
{ | ||
Component: sampleComponent, | ||
key: "school", | ||
}, | ||
{ | ||
Component: sampleComponent, | ||
key: "email", | ||
}, | ||
{ | ||
Component: sampleComponent, | ||
key: "password", | ||
}, | ||
]; | ||
|
||
// Form Helpers | ||
const setValueHOC = ( | ||
key: string, | ||
setterFunction: (key: string, value: string) => void | ||
) => (value: string) => setterFunction(key, value); | ||
|
||
//Form | ||
const WizardFormComponent = ({ | ||
handleSubmit, | ||
setFieldValue, | ||
errors, | ||
values, | ||
}: FormikProps<FormValues>) => { | ||
const [currentStep, setStep] = useState(0); | ||
const nextStep = () => setStep((currentStep) => currentStep + 1); | ||
const previousStep = () => setStep((currentStep) => currentStep - 1); | ||
|
||
const { Component, key } = SubForms[currentStep]; | ||
|
||
return ( | ||
<Form onSubmit={handleSubmit}> | ||
<Component | ||
FormValue={key} | ||
setValue={setValueHOC(key, setFieldValue)} | ||
errors={errors[key]} | ||
nextStep={nextStep} | ||
previousStep={previousStep} | ||
hasNextStep={currentStep !== SubForms.length - 1} | ||
hasPreviousStep={currentStep !== 0} | ||
hasErrors={errors[key] !== ""} | ||
/> | ||
<pre>{JSON.stringify(errors)}</pre> | ||
<pre>{JSON.stringify(values)}</pre> | ||
</Form> | ||
); | ||
}; | ||
|
||
const ComponentWithFormik = withFormik({ | ||
validationSchema: SignupSchema, | ||
mapPropsToValues: () => initialValues, | ||
validateOnMount: true, | ||
handleSubmit: (values) => { | ||
alert(JSON.stringify(values)); // TODO | ||
}, | ||
})(WizardFormComponent); | ||
|
||
export function Signup(): JSX.Element { | ||
return <ComponentWithFormik />; | ||
} | ||
|
||
export default Signup; |
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
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.
oooh TIL