Skip to content

Commit

Permalink
Merge branch 'MakeDynamicRORField' of github.com:DiSSCo/tettris-marke…
Browse files Browse the repository at this point in the history
…tplace into MakeDynamicRORField
  • Loading branch information
TomDijkema committed Oct 23, 2024
2 parents 232fd36 + f89c8b3 commit ff7b74d
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 92 deletions.
3 changes: 0 additions & 3 deletions src/components/search/components/SearchResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import classNames from 'classnames';
import moment from 'moment';
import { Row, Col } from 'react-bootstrap';

/* Import Utilities */
import { MakeReadableString } from 'app/Utilities';

/* Import Hooks */
import { useAppDispatch } from 'app/Hooks';

Expand Down
1 change: 0 additions & 1 deletion src/components/taxonomicService/TaxonomicService.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* Import Dependencies */
import classNames from 'classnames';
import moment from 'moment';
import { useState } from 'react';
import { Link, useNavigate, useParams } from 'react-router-dom';
import { Container, Row, Col } from 'react-bootstrap';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import MarkdownIt from 'markdown-it';
import moment from 'moment';
import { Row, Col } from 'react-bootstrap';

/* Import Utilities */
import { MakeReadableString } from 'app/Utilities';

/* Import Types */
import { TaxonomicService } from 'app/Types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
/* Import Dependencies */
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Formik, Form, FieldArray } from "formik";
import { Formik, Form } 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 BooleanField from "./BooleanField";
import DateField from "./DateField";
import FormBuilderFieldArray from "./FormBuilderFieldArray";
import MultiSelectField from "./MultiSelectField";
import RORField from "./RORField";
import SelectField from "./SelectField";
Expand Down Expand Up @@ -111,8 +104,6 @@ const FormBuilder = (props: Props) => {
/* Add to initial form values array zero index */
jp.value(initialFormValues, jsonPath, DetermineInitialFormValue(field.type));
} else {
jsonPath = jsonPath.concat(FlattenJSONPath(field.jsonPath));

/* Add to initial form values */
jp.value(initialFormValues, field.jsonPath, DetermineInitialFormValue(field.type));
}
Expand Down Expand Up @@ -193,80 +184,14 @@ const FormBuilder = (props: Props) => {
</Row>
))}
</div>
: <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}
className="mt-2"
>
<Col>
{ConstructFormField(localField, jp.value(values, localField.jsonPath), setFieldValue)}
</Col>
</Row>
);
})}
</Col>
</Row>

</div>
);
})}
</div>
)}
</FieldArray>
: <FormBuilderFieldArray section={section}
title={title}
initialFormValues={initialFormValues}
values={values}
formSections={formSections}
FlattenJSONPath={FlattenJSONPath}
ConstructFormField={ConstructFormField}
/>
}
</Col>
</Row>
Expand Down
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;

0 comments on commit ff7b74d

Please sign in to comment.