-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
147 additions
and
101 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
118 changes: 118 additions & 0 deletions
118
src/components/taxonomicService/taxonomicServiceFormComponents/FormBuilderFieldArray.tsx
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,118 @@ | ||
/* Import Dependencies */ | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
import { FieldArray } from "formik"; | ||
import jp from 'jsonpath'; | ||
import { cloneDeep } from "lodash"; | ||
import { Row, Col } from "react-bootstrap"; | ||
|
||
/* Import Utilities */ | ||
import { MakeReadableString } from "app/Utilities"; | ||
|
||
/* Import Types */ | ||
import { FormField, Dict } from "app/Types"; | ||
|
||
/* Import Icons */ | ||
import { faX } from "@fortawesome/free-solid-svg-icons"; | ||
|
||
/* Import Components */ | ||
import { Button } from "components/general/CustomComponents"; | ||
|
||
|
||
/* Props Type */ | ||
type Props = { | ||
section: Dict, | ||
title: string, | ||
initialFormValues: Dict | ||
values: Dict, | ||
formSections: { | ||
[section: string]: { | ||
type: string; | ||
jsonPath: string; | ||
fields: FormField[]; | ||
} | ||
}, | ||
FlattenJSONPath: Function, | ||
ConstructFormField: Function | ||
}; | ||
|
||
|
||
const FormBuilderFieldArray = (props: Props) => { | ||
const { section, title, initialFormValues, values, formSections, FlattenJSONPath, ConstructFormField } = props; | ||
|
||
return ( | ||
<FieldArray name={section.jsonPath.replace('$', '')}> | ||
{({ push, remove }) => ( | ||
<div className="mt-4"> | ||
<Row> | ||
<Col> | ||
<p className="fw-lightBold">{`${title}${title.at(-1) !== 'a' ? 's' : ''}`}</p> | ||
</Col> | ||
<Col lg="auto"> | ||
<Button type="button" | ||
variant="blank" | ||
className="px-0 py-0" | ||
OnClick={() => { | ||
push(jp.value(initialFormValues, section.jsonPath)[0]); | ||
}} | ||
> | ||
<p className="tc-primary"> | ||
{`Add ${title}`} | ||
</p> | ||
</Button> | ||
</Col> | ||
</Row> | ||
|
||
{jp.value(values, section.jsonPath).map((_fields: Dict, index: number) => { | ||
const key = `${section.jsonPath}-${index}`; | ||
|
||
return ( | ||
<div key={key} | ||
className="mt-2 px-3 py-2 b-grey" | ||
> | ||
<Row> | ||
<Col> | ||
<p className="fw-lightBold"> | ||
{`${title} #${index + 1}`} | ||
</p> | ||
</Col> | ||
{jp.value(values, section.jsonPath).length > 1 && | ||
<Col lg="auto"> | ||
<Button type="button" | ||
variant="blank" | ||
OnClick={() => remove(index)} | ||
> | ||
<FontAwesomeIcon icon={faX} | ||
className="tc-grey" | ||
/> | ||
</Button> | ||
</Col> | ||
} | ||
</Row> | ||
<Row className="my-2"> | ||
<Col> | ||
{formSections[MakeReadableString(FlattenJSONPath(section.jsonPath)).split(' ').slice(1).join(' ')].fields.map(field => { | ||
let localField = cloneDeep(field); | ||
|
||
localField.jsonPath = field.jsonPath.replace('index', String(index)); | ||
|
||
return ( | ||
<Row key={localField.jsonPath}> | ||
<Col> | ||
{ConstructFormField(localField, jp.value(values, localField.jsonPath))} | ||
</Col> | ||
</Row> | ||
); | ||
})} | ||
</Col> | ||
</Row> | ||
|
||
</div> | ||
); | ||
})} | ||
</div> | ||
)} | ||
</FieldArray> | ||
); | ||
}; | ||
|
||
export default FormBuilderFieldArray; |