import React, { useState } from 'react' import passwordVisibleImg from '../../images/password-visible.png' import passwordHiddenImg from '../../images/password-hidden.png' /** * * @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() if ( inputList[0].action === 'Async' ) { await action(refList) } else if ( inputList[0].action === 'Download' || inputList[0].action === 'Upload' ) { await action() } else { 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)} className="form"> { inputList.map((input, key) => { if (input.type === 'info') { info = input } else if (input.type === 'text') { return ( ) } else if (input.type === 'password') { return ( ) } else if (input.type === 'links-listing') { return ( ) } else if (input.type === 'file') { return ( ) } else if (input.type === 'choice-listing') { return ( ) } else if (input.type === 'range') { return ( ) } else if (input.type === 'vector') { return ( ) } }) } { info.button_value === '' ? <> : } ) } /** * Text input generator, example: * @param { * { * type: 'text', * name: 'name', * ref: React.createRef(), * onChange: null OR validationFunc * validationInfo: null OR useState("") * } } input - basic text input * @param { * { * type: 'info', * action: 'Update', * endpoint: 'Album' * } } info - information about form */ const TextInputGenerator = ({ input, info }) => { const [textInputValidationInfo, setTextInputValidationInfo] = useState("Empty") const inputRegex = /^[^'";<>=]+$/ const defaultValidation = (event) => { if (event.target.value === "") { setTextInputValidationInfo("Empty") } else if (!inputRegex.test(event.target.value)) { setTextInputValidationInfo("Please provide correct value") } else { setTextInputValidationInfo("Success") } } return (
{ input.validationInfo === null ? textInputValidationInfo : input.validationInfo }
) } /** * Text input generator, example: * @param { * { * type: 'password', * name: 'name', * ref: React.createRef() * } } input - basic text input * @param { * { * type: 'info', * action: 'Update' * endpoint: 'Album' * } } info - information about form */ const PasswordInputGenerator = ({ input, info }) => { const [contentIsHidden, setContentIsHidden] = useState(true) return (
{ setContentIsHidden(!contentIsHidden) } } />
{ input.validationInfo }
) } const ObjectIterator = ({ object, divPlacer }) => { return( <> { typeof object == "object" ? Object.keys( object ).map( ( key ) => { return (
{ divPlacer( object[key] ) }
) } ) : object } ) } /** * Text input generator, example: * @param { * { * type: 'drop-box', * name: 'name', * values: list, * link: link to the file * } } input - basic text input * @param { * { * type: 'info', * action: 'Update' * endpoint: 'Album' * } } info - information about form */ const DownloadFilesListInputGenerator = ({ input, info }) => { return (
{input.name + ':'} { input.values.length == 0 ? () => { return (
empty
) } : input.values.map( (item, index) => { return ( <>
{ typeof item == 'string' ? item : Object.keys(item).map( ( key, index ) => { return(
{ key + ': ' } { typeof item[key] === "object" ? Object.keys( item[key] ).map( ( key_two, index ) => { return (
{ key_two + ': '} { typeof item[key][key_two] == 'object' ? Object.keys( item[key][key_two] ).map( (key_three, index) => { return (
{ key_three + ": " } { "x: " + item[key][key_two][key_three].x + ", " } { "y: " + item[key][key_two][key_three].y + ", " } { "z: " + item[key][key_two][key_three].z + ", " }
) } ) : item[key][key_two] }
) } ) : item[key] }
) } ) }

download
) } ) }
) } /** * Text input generator, example: * @param { * { * type: 'chice-listing', * name: 'name', * values: list, * ref: React.createRef() * } } input - basic text input * @param { * { * type: 'info', * action: 'Update' * endpoint: 'Album' * } } info - information about form */ const ChoiceListingGenerator = ({ input, info }) => { const __handleRef = ( event ) => { event.preventDefault() input.ref.current = { value: event.target.value } } return (
{input.name + ':'}
) } /** * Upload file input generator, example: * @param { * { * type: 'file', * name: 'name', * endpoint: 'Album', * fileType: 'image' or 'audio', * dropInfo: dropInfo, setDropInfo: setDropInfo(), #useState * file: file, setFile: setFile() #useState * } } input - */ const UploadInputGenerator = ({ input, info }) => { const onLoadFile = async (event) => { event.preventDefault() let data = event.target.files[0] // input.setFile(await toBase64(data)) input.setFile( data ) setDropInfos(data.name, data.size) } const onLoadFileDrop = async (event) => { event.preventDefault() event.persist() let data = event.dataTransfer.files[0] // input.setFile(await toBase64(data)) input.setFile( data ) setDropInfos(data.name, data.size) } const toBase64 = async (file) => new Promise((resolve, reject) => { let fileReader = new FileReader() fileReader.readAsDataURL(file) fileReader.onload = () => resolve(fileReader.result) fileReader.onerror = error => reject(error) }) const setDropInfos = (name, size) => { input.setDropInfo( 'name: "' + name + '"\nsize: ' + (Math.round(size / 100 + 'e-2') / 100) + ' MB' ) } return (
onLoadFileDrop(event)} >
                {input.dropInfo}
            
onLoadFile(event)} />
) } /** * Text input generator, example: * @param { * { * type: 'range', * name: 'name', * min: min range value, * max: max range value, * step: step of value, * unit: unit of range value, * ref: React.createRef() * } } input - basic text input * @param { * { * type: 'info', * action: 'Update' * endpoint: 'Album' * } } info - information about form */ const RangeInputGenerator = ({ input, info }) => { let name = input.name + info.action + info.endpoint + 'Input' const [value, setValue] = useState(0) return (
{input.name + ': ' + value + ' ' + input.unit }
setValue( event.target.value ) } />
) } const RangeGenerator = ({ key, label, labelStyle, valueStyle, style, name, unit, min, max, defaultValue, step, reference }) => { const [value, setValue] = useState(0) return (
{ label + ': ' }
setValue( event.target.value ) } />
{ value + ' ' + unit }
) } /** * Text input generator, example: * @param { * { * type: 'vector', * name: 'name', * refDict: * { * x: React.createRef(), * y: React.createRef(), * z: React.createRef() * } * } } input - basic text input * @param { * { * type: 'info', * action: 'Update' * endpoint: 'Album' * } } info - information about form */ const VectorInputGenerator = ({ input, info }) => { return (
{ input.name }
{ Object.keys(input.refDict).map( (key) => { let name = input.name + key + info.action + info.endpoint + 'Input' return (
0 ? input.min[key] : 0 } step={0.1} reference={input.refDict[key]} />
) } ) }
) } export default FormGenerator