Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Formik, add validation #23

Merged
merged 5 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.15.0
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
"@react-pdf/renderer": "^1.6.8",
"@rehooks/local-storage": "^2.3.0",
"date-format": "^3.0.0",
"file-saver": "^2.0.2",
"formik": "^2.1.4",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-input-autosize": "^2.2.2",
"react-scripts": "3.4.1",
"react-signature-canvas": "^1.0.3",
"styled-components": "^5.0.1"
"styled-components": "^5.0.1",
"yup": "^0.28.3"
},
"scripts": {
"start": "react-scripts start",
Expand Down
48 changes: 48 additions & 0 deletions src/components/FormControls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from "react";
import { useField } from "formik";
import styled from "styled-components";

import { color, spacing } from "../helpers/constants";

import TextField from "./TextField";

export const FTF = props => {
// useField() returns [formik.getFieldProps(), formik.getFieldMeta()]
// which we can spread on <input> and also replace ErrorMessage entirely.
const [field, meta] = useField(props);
return <TextField hasError={Boolean(meta.touched && meta.error)} {...field} {...props} />;
};

const Input = styled.input`
visibility: hidden;
position: absolute;
`;

const Label = styled.label`
display: block;
cursor: pointer;
margin-bottom: ${spacing.small};
`;

const Checkbox = styled(({ hasError, ...props }) => <span {...props} />)`
width: 10px;
height: 10px;
display: inline-block;
border: 1px solid;
border-color: ${({ hasError }) => (hasError ? color.red : color.black)};
margin-right: ${spacing.small};
background-color: ${({ checked }) => (checked ? color.black : color.white)};
`;

export const FCK = ({ children, hasError, ...props }) => {
const [field, meta] = useField({ ...props, type: "checkbox" });
return (
<>
<Label style={{ border: hasError ? "1px solid red" : "" }}>
<Checkbox hasError={Boolean(meta.touched && meta.error)} checked={field.checked} />
<Input type="checkbox" {...field} {...props} />
{children}
</Label>
</>
);
};
6 changes: 4 additions & 2 deletions src/components/TextField.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import React from 'react';
import AutosizeInput from "react-input-autosize";
import PropTypes from "prop-types";
import styled from "styled-components";

import { color, width, fontWeight, borderRadius } from "../helpers/constants";

const TextField = styled(AutosizeInput)`
const TextField = styled(({ hasError, ...props }) => <AutosizeInput {...props} />)`
margin: 0 2px 0 5px;
input {
padding: 5px 10px;
text-transform: uppercase;
font-weight: ${fontWeight.bold};
border: 1px solid ${color.black};
border: 1px solid;
border-radius: ${borderRadius.small};
min-width: ${({ size }) => width[size]};
border-color: ${({ hasError }) => (hasError ? color.red : color.black)};
}
`;

Expand Down
1 change: 1 addition & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const color = {
white: "#ffffff",
black: "#000000",
red: "#FF0000"
};

export const spacing = {
Expand Down
28 changes: 28 additions & 0 deletions src/helpers/validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as Yup from "yup";

export const schema = Yup.object().shape({
nume: Yup.string().required("required"),
adresa_localitate: Yup.string().required("required"),
adresa_judet: Yup.string().required("required"),
cnp: Yup.string().required("required"),
ci_seria: Yup.string().required("required"),
interval_orar: Yup.string().required("required"),
traseu_start: Yup.string().required("required"),
traseu_sfarsit: Yup.string().required("required"),
situatie_urgenta: Yup.string().when(["deplasare_urgenta"], { is: true, then: Yup.string().required() }),
checkboxes_are_valid: Yup.bool().when(
[
"deplasare_servici",
"deplasare_consult",
"deplasare_cumparaturi",
"deplasare_ajutor",
"deplasare_scurta",
"deplasare_animale",
"deplasare_urgenta",
],
{
is: false,
then: Yup.bool().oneOf([true], "Field must be checked"),
},
),
});
Loading