From f440e178dd423632bffe840dbdb4a841e888a139 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Fri, 17 Jul 2020 21:21:27 +0200 Subject: [PATCH] more error validation tweaks, reset form --- src/components/atoms/Input/InputElement.tsx | 5 +- src/components/atoms/Input/index.module.css | 10 ++++ src/components/atoms/Input/index.tsx | 17 +++++-- .../pages/Publish/Preview.module.css | 6 +-- .../pages/Publish/PublishForm.module.css | 6 +++ src/components/pages/Publish/PublishForm.tsx | 51 +++++++++++++++---- src/components/pages/Publish/index.tsx | 5 +- src/components/pages/Publish/validation.ts | 22 ++++---- 8 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/components/atoms/Input/InputElement.tsx b/src/components/atoms/Input/InputElement.tsx index 429db7c5a..ec780206d 100644 --- a/src/components/atoms/Input/InputElement.tsx +++ b/src/components/atoms/Input/InputElement.tsx @@ -6,7 +6,7 @@ import FilesInput from '../../molecules/FormFields/FilesInput' import Terms from '../../molecules/FormFields/Terms' export default function InputElement(props: InputProps): ReactElement { - const { type, options, rows, name } = props + const { type, options, rows, name, value } = props switch (type) { case 'select': @@ -62,10 +62,11 @@ export default function InputElement(props: InputProps): ReactElement { return ( ) } diff --git a/src/components/atoms/Input/index.module.css b/src/components/atoms/Input/index.module.css index e56057d0f..10cb7a97d 100644 --- a/src/components/atoms/Input/index.module.css +++ b/src/components/atoms/Input/index.module.css @@ -20,3 +20,13 @@ right: 0; top: 0; } + +.hasError label { + color: var(--brand-alert-red); +} + +.hasError input, +.hasError select, +.hasError textarea { + border-color: var(--brand-alert-red); +} diff --git a/src/components/atoms/Input/index.tsx b/src/components/atoms/Input/index.tsx index f01255416..72378b87a 100644 --- a/src/components/atoms/Input/index.tsx +++ b/src/components/atoms/Input/index.tsx @@ -3,8 +3,10 @@ import InputElement from './InputElement' import Help from './Help' import Label from './Label' import styles from './index.module.css' -import { ErrorMessage, FormikState, FieldProps, FieldInputProps } from 'formik' -import { MetadataPublishForm } from '../../../@types/Metadata' +import { ErrorMessage } from 'formik' +import classNames from 'classnames/bind' + +const cx = classNames.bind(styles) export interface InputProps { name: string @@ -36,9 +38,18 @@ export interface InputProps { export default function Input(props: Partial): ReactElement { const { required, name, label, help, additionalComponent, field } = props + const hasError = + props.form.touched[field.name] && + typeof props.form.errors[field.name] === 'string' + + const styleClasses = cx({ + field: true, + hasError: hasError + }) + return (