-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from rluncasu/master
use Formik, add validation
- Loading branch information
Showing
8 changed files
with
306 additions
and
158 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
12.15.0 |
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,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> | ||
</> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export const color = { | ||
white: "#ffffff", | ||
black: "#000000", | ||
red: "#FF0000" | ||
}; | ||
|
||
export const spacing = { | ||
|
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,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"), | ||
}, | ||
), | ||
}); |
Oops, something went wrong.
292a0cf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: