Skip to content

Commit

Permalink
Add Reset Form button
Browse files Browse the repository at this point in the history
  • Loading branch information
colinking committed May 14, 2020
1 parent 44264e0 commit c00929c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions public/form.yml
Expand Up @@ -37,6 +37,9 @@ instructions:
'download:button':
en: Download
es: Descargar
reset:
en: Reset Form
es: Restablecer formulario
submit-instructions:
en: |-
Please review and verify the information section below. If there are errors in any section, you may select 'Edit' to revisit that page of your application.
Expand Down
11 changes: 10 additions & 1 deletion src/components/Sidebar.tsx
@@ -1,10 +1,12 @@
import React, { useContext } from 'react'
import { Button } from './helper-components'
import { Card } from './helper-components'
import { Box, Text, Image, ResponsiveContext, Heading, Paragraph } from 'grommet'
// import { LanguageContext } from '../contexts/language'
import { FormContext } from '../contexts/form'
import { range } from 'lodash'
import { StyledSelect } from './helper-components/StyledSelect'
import { Refresh } from 'grommet-icons'
// import amplitude from 'amplitude-js'

interface Props {
Expand All @@ -21,7 +23,7 @@ interface Props {

const Sidebar: React.FC<Props> = (props) => {
const { pages } = props
const { translateByID, form, pageIndex, setPage, completion } = useContext(FormContext)
const { resetForm, translateByID, form, pageIndex, setPage, completion } = useContext(FormContext)
// const { language, setLanguage } = useContext(LanguageContext)
const size = useContext(ResponsiveContext)

Expand Down Expand Up @@ -92,6 +94,13 @@ const Sidebar: React.FC<Props> = (props) => {
>
<Box style={{ width: `${percent}%`, height: '100%', borderRadius: '12px', background: '#3A80C2' }} />
</Box>
<Box margin={{ top: '24px' }}>
<Button
onClick={resetForm}
label={translateByID('reset')}
icon={<Refresh style={{ marginRight: '4px' }} />}
/>
</Box>
</Box>
<Box margin={{ top: '24px' }}>
{size === 'large' ? (
Expand Down
9 changes: 9 additions & 0 deletions src/contexts/form.tsx
Expand Up @@ -28,6 +28,7 @@ export interface FormState {
completion: Record<string, boolean>
pageIndex: number
setPage: (index: number) => void
resetForm: () => void
}

const initialState = {
Expand Down Expand Up @@ -262,6 +263,13 @@ export const FormProvider: React.FC = (props) => {
// setPrefilled(true)
// })

const resetForm = () => {
setValues({})
setErrors({})
setPageIndex(0)
setCompletion({})
}

useEffect(() => {
if (form) {
// Update the page title
Expand Down Expand Up @@ -318,6 +326,7 @@ ${formValidationError.error ? '```' + JSON.stringify(formValidationError.error,
completion,
pageIndex,
setPage,
resetForm,
}

return <FormContext.Provider value={value}>{props.children}</FormContext.Provider>
Expand Down

0 comments on commit c00929c

Please sign in to comment.