-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[upd] Rename steps to formQuestions outside wizards
- Loading branch information
Showing
9 changed files
with
157 additions
and
208 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { useMemo, useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import FormUtils from '../util/FormUtils'; | ||
|
||
const INITIAL_DATA = {}; | ||
const INITIAL_FORM_QUESTIONS_DATA = []; | ||
|
||
const FormQuestionsContext = React.createContext({}); | ||
|
||
const FormQuestionsProvider = (props) => { | ||
const [data, setData] = useState(props.data || INITIAL_DATA); | ||
const [formQuestionsData, setFormQuestionsData] = useState(props.formQuestions || INITIAL_FORM_QUESTIONS_DATA); | ||
|
||
useEffect(() => { | ||
if (props.isFormValid) { | ||
const isValid = FormUtils.isValid(data); | ||
props.isFormValid(isValid); | ||
} | ||
}, []); | ||
|
||
const updateData = (update) => { | ||
if (!update) return; | ||
|
||
setData({ ...data, ...update }); | ||
}; | ||
|
||
const updateFormQuestionsData = (index, update) => { | ||
if (!update || index < 0 || index >= formQuestionsData.length) return; | ||
|
||
const newFormQuestionsData = [...formQuestionsData]; | ||
newFormQuestionsData[index] = { ...newFormQuestionsData[index], ...update }; | ||
|
||
setFormQuestionsData(newFormQuestionsData); | ||
|
||
if (props.isFormValid) { | ||
const isValid = FormUtils.isValid(data); | ||
props.isFormValid(isValid); | ||
} | ||
}; | ||
|
||
const getData = () => { | ||
return data; | ||
}; | ||
|
||
const getFormQuestionsData = (index) => { | ||
return index === null || index === undefined ? formQuestionsData : formQuestionsData[index]; | ||
}; | ||
|
||
const values = useMemo( | ||
() => ({ | ||
updateData, | ||
updateFormQuestionsData, | ||
getData, | ||
getFormQuestionsData | ||
}), | ||
[getFormQuestionsData, getData] | ||
); | ||
|
||
return ( | ||
<FormQuestionsContext.Provider value={values} {...props}> | ||
{props.children} | ||
</FormQuestionsContext.Provider> | ||
); | ||
}; | ||
|
||
FormQuestionsProvider.propTypes = { | ||
children: PropTypes.element.isRequired, | ||
data: PropTypes.object.isRequired, | ||
formQuestions: PropTypes.array.isRequired, | ||
isFormValid: PropTypes.func | ||
}; | ||
|
||
export { FormQuestionsContext, FormQuestionsProvider }; |
Oops, something went wrong.