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
className={styles.input}
{...props}
{...field}
type="url"
onBlur={(e: React.SyntheticEvent) => handleButtonClick(e, field.value)}
/>

View File

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

View File

@ -10,11 +10,13 @@ import React, {
import InputElement from './InputElement'
import Label from './Label'
import styles from './index.module.css'
import { ErrorMessage, FieldInputProps } from 'formik'
import { ErrorMessage, FieldInputProps, FieldMetaProps, useField } from 'formik'
import classNames from 'classnames/bind'
import Disclaimer from './Disclaimer'
import Tooltip from '@shared/atoms/Tooltip'
import Markdown from '@shared/Markdown'
import MetadataFields from 'src/components/Publish/FormPublish/Metadata'
import toPath from 'lodash/toPath'
const cx = classNames.bind(styles)
@ -70,40 +72,53 @@ export default function Input(props: Partial<InputProps>): ReactElement {
help,
additionalComponent,
size,
form,
field,
disclaimer,
disclaimerValues
} = props
const hasError =
props.form?.touched[field.name] && props.form?.errors[field.name]
const isFormikField = typeof field !== 'undefined'
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({
field: true,
hasError: hasError
hasError: hasFormikError
})
const [disclaimerVisible, setDisclaimerVisible] = useState(true)
useEffect(() => {
if (!isFormikField) return
if (disclaimer && disclaimerValues) {
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 (
<div
className={styleClasses}
data-is-submitting={props.form?.isSubmitting ? true : null}
>
<div className={styleClasses}>
<Label htmlFor={props.name} required={props.required}>
{label} {help && <Tooltip content={<Markdown text={help} />} />}
</Label>
<InputElement size={size} {...field} {...props} />
{field && hasError && (
{isFormikField && hasFormikError && (
<div className={styles.error}>
<ErrorMessage name={field.name} />
</div>

View File

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