1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

make form error display kinda work

This commit is contained in:
Matthias Kretschmann 2021-10-28 16:34:46 +01:00
parent 778abc8aeb
commit 228b09098a
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 29 additions and 16 deletions

View File

@ -22,6 +22,7 @@ export default function URLInput({
<input <input
className={styles.input} className={styles.input}
{...props} {...props}
{...field}
type="url" type="url"
onBlur={(e: React.SyntheticEvent) => handleButtonClick(e, field.value)} onBlur={(e: React.SyntheticEvent) => handleButtonClick(e, field.value)}
/> />

View File

@ -154,6 +154,7 @@ export default function InputElement({
type={type || 'text'} type={type || 'text'}
size={size} size={size}
disabled={disabled} disabled={disabled}
{...field}
{...props} {...props}
/> />
{postfix && ( {postfix && (
@ -166,6 +167,7 @@ export default function InputElement({
type={type || 'text'} type={type || 'text'}
size={size} size={size}
disabled={disabled} disabled={disabled}
{...field}
{...props} {...props}
/> />
) )

View File

@ -10,11 +10,13 @@ import React, {
import InputElement from './InputElement' import InputElement from './InputElement'
import Label from './Label' import Label from './Label'
import styles from './index.module.css' import styles from './index.module.css'
import { ErrorMessage, FieldInputProps } from 'formik' import { ErrorMessage, FieldInputProps, FieldMetaProps, useField } from 'formik'
import classNames from 'classnames/bind' import classNames from 'classnames/bind'
import Disclaimer from './Disclaimer' import Disclaimer from './Disclaimer'
import Tooltip from '@shared/atoms/Tooltip' import Tooltip from '@shared/atoms/Tooltip'
import Markdown from '@shared/Markdown' import Markdown from '@shared/Markdown'
import MetadataFields from 'src/components/Publish/FormPublish/Metadata'
import toPath from 'lodash/toPath'
const cx = classNames.bind(styles) const cx = classNames.bind(styles)
@ -70,40 +72,53 @@ export default function Input(props: Partial<InputProps>): ReactElement {
help, help,
additionalComponent, additionalComponent,
size, size,
form,
field, field,
disclaimer, disclaimer,
disclaimerValues disclaimerValues
} = props } = props
const hasError = const isFormikField = typeof field !== 'undefined'
props.form?.touched[field.name] && props.form?.errors[field.name] const isNestedField = field?.name?.includes('.')
// TODO: this feels hacky as it assumes nested `values` store. But we can't use the
// `useField()` hook in here to get `meta.error` so we have to match against form?.errors?
// handling flat and nested data at same time.
const parsedFieldName =
isFormikField && (isNestedField ? field?.name.split('.') : [field?.name])
// const hasFormikError = !!meta?.touched && !!meta?.error
const hasFormikError =
form?.errors !== {} &&
form?.touched?.[parsedFieldName[0]]?.[parsedFieldName[1]] &&
form?.errors?.[parsedFieldName[0]]?.[parsedFieldName[1]]
const styleClasses = cx({ const styleClasses = cx({
field: true, field: true,
hasError: hasError hasError: hasFormikError
}) })
const [disclaimerVisible, setDisclaimerVisible] = useState(true) const [disclaimerVisible, setDisclaimerVisible] = useState(true)
useEffect(() => { useEffect(() => {
if (!isFormikField) return
if (disclaimer && disclaimerValues) { if (disclaimer && disclaimerValues) {
setDisclaimerVisible( setDisclaimerVisible(
disclaimerValues.includes(props.form?.values[field.name]) disclaimerValues.includes(
props.form?.values[parsedFieldName[0]]?.[parsedFieldName[1]]
)
) )
} }
}, [props.form?.values[field.name]]) }, [isFormikField, props.form?.values])
return ( return (
<div <div className={styleClasses}>
className={styleClasses}
data-is-submitting={props.form?.isSubmitting ? true : null}
>
<Label htmlFor={props.name} required={props.required}> <Label htmlFor={props.name} required={props.required}>
{label} {help && <Tooltip content={<Markdown text={help} />} />} {label} {help && <Tooltip content={<Markdown text={help} />} />}
</Label> </Label>
<InputElement size={size} {...field} {...props} /> <InputElement size={size} {...field} {...props} />
{field && hasError && ( {isFormikField && hasFormikError && (
<div className={styles.error}> <div className={styles.error}>
<ErrorMessage name={field.name} /> <ErrorMessage name={field.name} />
</div> </div>

View File

@ -68,11 +68,6 @@ export default function ServicesFields(): ReactElement {
component={Input} component={Input}
name="services[0].links" name="services[0].links"
/> />
<Field
{...getFieldContent('links', content.services.fields)}
component={Input}
name="services[0].links"
/>
<Field <Field
{...getFieldContent('access', content.services.fields)} {...getFieldContent('access', content.services.fields)}
component={Input} component={Input}