diff --git a/src/components/forms/formGenerator.js b/src/components/forms/formGenerator.js new file mode 100644 index 0000000..efba6d5 --- /dev/null +++ b/src/components/forms/formGenerator.js @@ -0,0 +1,158 @@ +import React from 'react' + +/** + * + * @param { [ {}, {}, ...{} ] } inputList - list of dicts with info about input + * @param { [] } refList - react ref objects list for handler validation + * @param { } action - fetch method + */ +export const FormGenerator = ({ + inputList, refList, + action +}) => { + + const handler = async (event) => { + event.preventDefault() + for ( let i = 0; i < refList.length; i++ ) { + if ( + refList[i].current.value === '' + && inputList[0].action !== 'Update' + || i === 0 && refList.length !== 1 + ) { + refList[i].current.focus() + } else if ( i === refList.length - 1 ) { + await action( refList ) + } + } + } + + let info + + return ( +
handler( event ) }> + { + inputList.map( (input, key) => { + + if ( input.type === 'info' ) { + info = input + } else if ( input.type === 'text' ) { + return ( + + ) + } else if ( input.type === 'file' ) { + return ( + + ) + } + }) + } +