From f85aef04d1f1e93debf7e8e8bd56ac4fc30a85f9 Mon Sep 17 00:00:00 2001 From: wzglinieckisoldevelo Date: Mon, 3 Jul 2023 09:49:57 +0200 Subject: [PATCH] ONI-120: Formik validation POC. --- src/components/MedicalServiceMasterPanel.js | 57 +++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/src/components/MedicalServiceMasterPanel.js b/src/components/MedicalServiceMasterPanel.js index 3c126aa..9737714 100644 --- a/src/components/MedicalServiceMasterPanel.js +++ b/src/components/MedicalServiceMasterPanel.js @@ -1,6 +1,8 @@ import React from "react"; import { connect } from "react-redux"; import { injectIntl } from "react-intl"; +import { withFormik} from 'formik'; +import * as yup from 'yup'; import { withStyles, withTheme } from "@material-ui/core/styles"; import { Grid } from "@material-ui/core"; @@ -33,13 +35,15 @@ class MedicalServiceMasterPanel extends FormPanel { return shouldValidate; } render() { - const { classes, edited, readOnly, isServiceValid, isServiceValidating, serviceValidationError} = this.props; + const { classes, edited, readOnly, isServiceValid, isServiceValidating, serviceValidationError, errors, touched, + handleBlur, values} = this.props; return ( this.updateAttributes({ name })} + error={(touched.serviceName && Boolean(errors.serviceName)) ? errors.serviceName : null} + onBlur={handleBlur} /> this.updateAttribute("type", p)} /> @@ -83,15 +95,21 @@ class MedicalServiceMasterPanel extends FormPanel { this.updateAttribute("category", p)} + error={(touched.serviceCategory && Boolean(errors.serviceCategory)) ? errors.serviceCategory : null} + onBlur={handleBlur} /> this.updateAttribute("price", p)} @@ -116,6 +136,9 @@ class MedicalServiceMasterPanel extends FormPanel { pubRef="medical.CareTypePicker" required withNull={false} + name="serviceCareType" + error={(touched.serviceCareType && Boolean(errors.serviceCareType)) ? errors.serviceCareType : null} + onBlur={handleBlur} readOnly={Boolean(edited.id) || readOnly} value={edited?.careType ? edited.careType : " "} onChange={(p) => this.updateAttribute("careType", p)} @@ -125,6 +148,9 @@ class MedicalServiceMasterPanel extends FormPanel { this.updateAttribute("frequency", p)} @@ -133,6 +159,9 @@ class MedicalServiceMasterPanel extends FormPanel { this.updateAttribute("patientCategory", p)} @@ -152,6 +181,28 @@ const mapStateToProps = (state) => ({ savedServiceCode: state.medical?.medicalService?.code, }); +const validationSchema = yup.object({ + serviceCode: yup.string().required('Service code is required'), + serviceName: yup.string().required('Service name is required'), + serviceType: yup.string().required('Service type is required'), + serviceCareType: yup.string().required('asdad') +}); + export default injectIntl( - withModulesManager(withHistory(connect(mapStateToProps)(withTheme(withStyles(styles)(MedicalServiceMasterPanel))))), + withModulesManager(withHistory(connect(mapStateToProps)(withTheme(withStyles(styles)(withFormik({ + mapPropsToValues: (props) => {return {serviceName: props.edited.name, + serviceCode: props.edited.code, + serviceType: props.edited.type, + serviceCareType: props.edited.careType, + };}, + validationSchema: validationSchema, + enableReinitialize: true, + validate: values => { + let errors ={} + return errors + }, + handleSubmit: (values) => { + console.log(values); + }, + })(MedicalServiceMasterPanel)))))), );