Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Text Input, Edit and Delete Functionality
- Loading branch information
1 parent
99e8dfd
commit 5470b31
Showing
9 changed files
with
296 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useContext } from "react"; | ||
|
||
// ContextAPI | ||
import { FormData, PopupContext } from "../../App"; | ||
|
||
// Icons | ||
import EditIcon from "@mui/icons-material/Edit"; | ||
import DeleteIcon from "@mui/icons-material/Delete"; | ||
|
||
// Constants | ||
import { FORM_TYPES, getUserSideFormTypeById } from "../../constants/formTypes"; | ||
import { POPUP_TYPES } from "../../constants/popupTypes"; | ||
|
||
// Services | ||
import GraphStructureService from "../../services/graph.structurer.service"; | ||
|
||
function FormComponent({ element, groupId }) { | ||
const gs = new GraphStructureService(); | ||
const [, setPopupContext] = useContext(PopupContext); | ||
const [formDataContext, setFormDataContext] = useContext(FormData); | ||
|
||
const handleEdit = () => { | ||
setPopupContext({ | ||
data: { | ||
id: groupId, | ||
formData: element, | ||
type: FORM_TYPES.TEXT_INPUT, | ||
}, | ||
show: true, | ||
edit: true, | ||
type: POPUP_TYPES.ADD_FORM_ELEMENT, | ||
}); | ||
}; | ||
|
||
const handleDelete = () => { | ||
setFormDataContext( | ||
gs.deleteFormElement(groupId, element.id, formDataContext) | ||
); | ||
}; | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
justifyContent: "space-between", | ||
alignItems: "center", | ||
}} | ||
> | ||
<div> | ||
<p style={{ margin: "0px" }}> | ||
<strong>Label: </strong> | ||
{element.label} | ||
</p> | ||
<p style={{ margin: "0px" }}> | ||
<strong>Form Type: </strong> | ||
{getUserSideFormTypeById(element.type)} | ||
</p> | ||
</div> | ||
<div style={{ display: "flex" }}> | ||
<div style={{ cursor: "pointer", marginRight: "10px" }}> | ||
<EditIcon onClick={handleEdit} /> | ||
</div> | ||
<div style={{ cursor: "pointer" }}> | ||
<DeleteIcon onClick={handleDelete} /> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default FormComponent; |
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
47 changes: 31 additions & 16 deletions
47
src/components/Popup/types/AddFormElement/AddFormElement.jsx
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
123 changes: 123 additions & 0 deletions
123
src/components/Popup/types/formTypes/TextInput/TextInput.jsx
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,123 @@ | ||
// ContextAPI | ||
import { FormData, PopupContext } from "../../../../../App"; | ||
|
||
// Libraries | ||
import React, { Fragment, useContext } from "react"; | ||
import { useFormik } from "formik"; | ||
import * as yup from "yup"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
// Components | ||
import Button from "@material-ui/core/Button"; | ||
import TextField from "@material-ui/core/TextField"; | ||
|
||
// styles | ||
import styles from "./TextInput.module.css"; | ||
import { Checkbox, FormControlLabel } from "@material-ui/core"; | ||
import { FORM_TYPES } from "../../../../../constants/formTypes"; | ||
|
||
const initialValidationSchema = yup.object({ | ||
label: yup | ||
.string("Enter the form label") | ||
.required("This field must be specified"), | ||
}); | ||
|
||
function TextInput({ edit }) { | ||
// ContextAPI | ||
const [formData] = useContext(FormData); | ||
const [popupContext, setPopupContext] = useContext(PopupContext); | ||
|
||
const initialFormValues = () => { | ||
let value = {}; | ||
value = popupContext.data.formData || { label: "", isRequired: false }; | ||
return value; | ||
}; | ||
|
||
// handleSubmit | ||
const handleFormSubmit = async (values) => { | ||
values.type = FORM_TYPES.TEXT_INPUT; | ||
const updatedFormData = formData; | ||
if (!edit) { | ||
values.id = uuidv4(); | ||
updatedFormData.schema[popupContext.data.id].formElements.push(values); | ||
setPopupContext({ ...popupContext, show: false }); | ||
} else { | ||
const formElements = formData.schema[popupContext.data.id].formElements; | ||
for (let i = 0; i < formElements.length; i++) { | ||
if (formElements[i].id === popupContext.data.formData.id) { | ||
values.id = formElements[i].id; | ||
formElements[i] = values; | ||
} | ||
} | ||
updatedFormData.schema[popupContext.data.id].formElements = formElements; | ||
setPopupContext({ ...popupContext, show: false }); | ||
} | ||
}; | ||
|
||
const handleClose = async () => { | ||
setPopupContext({ ...popupContext, show: false }); | ||
}; | ||
|
||
// Formik | ||
const formik = useFormik({ | ||
initialValues: initialFormValues(), | ||
validationSchema: initialValidationSchema, | ||
onSubmit: (values) => { | ||
handleFormSubmit(values); | ||
}, | ||
}); | ||
|
||
return ( | ||
<Fragment> | ||
<div className={styles.InitialFormScreen} onSubmit={handleFormSubmit}> | ||
<h2>Text Input</h2> | ||
<p>Add a text Input</p> | ||
<form onSubmit={formik.handleSubmit} className={styles.InitialForm}> | ||
<TextField | ||
variant="outlined" | ||
className={styles.FormField} | ||
id="label" | ||
name="label" | ||
label="Label" | ||
value={formik.values["label"]} | ||
onChange={formik.handleChange} | ||
onBlur={formik.handleBlur} | ||
error={formik.touched["label"] && Boolean(formik.errors["label"])} | ||
helperText={formik.touched["label"] && formik.errors["label"]} | ||
/> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
id="isRequired" | ||
name="isRequired" | ||
checked={formik.values["isRequired"]} | ||
onChange={formik.handleChange} | ||
/> | ||
} | ||
label="Is Required" | ||
/> | ||
<div | ||
style={{ | ||
marginTop: "20px", | ||
display: "flex", | ||
justifyContent: "space-between", | ||
}} | ||
> | ||
<div style={{ cursor: "pointer" }} onClick={handleClose}> | ||
<p>CANCEL</p> | ||
</div> | ||
<Button | ||
variant="contained" | ||
type="submit" | ||
className={styles.SubmitButton} | ||
> | ||
{edit ? "SAVE" : "CREATE"} | ||
</Button> | ||
</div> | ||
</form> | ||
</div> | ||
</Fragment> | ||
); | ||
} | ||
|
||
export default TextInput; |
14 changes: 14 additions & 0 deletions
14
src/components/Popup/types/formTypes/TextInput/TextInput.module.css
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,14 @@ | ||
.SubmitButton { | ||
width: 100px; | ||
background-color: rgb(0, 213, 255) !important; | ||
margin-top: 10px; | ||
} | ||
|
||
.FormFieldContainer { | ||
margin-top: 10px; | ||
margin-bottom: 10px; | ||
} | ||
|
||
.FormField { | ||
width: 100%; | ||
} |
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,26 @@ | ||
export const FORM_TYPES = { | ||
TEXT_INPUT: "TEXT_INPUT", | ||
PARAGRAPH_INPUT: "PARAGRAPH_INPUT", | ||
}; | ||
|
||
export const USER_SIDE_FORM_TYPES = { | ||
"Text Input": "TEXT_INPUT", | ||
"Paragraph Input": "PARAGRAPH_INPUT", | ||
}; | ||
|
||
export const getFormType = (type) => { | ||
return USER_SIDE_FORM_TYPES[type]; | ||
}; | ||
|
||
export const getUserSideFormTypeById = (id) => { | ||
for (let key in USER_SIDE_FORM_TYPES) { | ||
if (USER_SIDE_FORM_TYPES[key] === id) return key; | ||
} | ||
}; | ||
|
||
export const getUserSideFormTypes = () => { | ||
const formTypes = []; | ||
for (let key in USER_SIDE_FORM_TYPES) | ||
formTypes.push({ value: key, key: USER_SIDE_FORM_TYPES[key] }); | ||
return formTypes; | ||
}; |
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