-
-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dcb36f
commit c06c741
Showing
8 changed files
with
533 additions
and
119 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,87 @@ | ||
import React, { useContext } from 'react'; | ||
import type { FormikProps } from 'formik'; | ||
import { Field } from 'formik'; | ||
import { pickBy } from 'lodash'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import { isOCError } from '../lib/errors'; | ||
import { formatFormErrorMessage, RICH_ERROR_MESSAGES } from '../lib/form-utils'; | ||
|
||
import { Input } from './ui/Input'; | ||
import { Label } from './ui/Label'; | ||
import { FormikZodContext, getInputAttributesFromZodSchema } from './FormikZod'; | ||
|
||
export function FormField({ | ||
label, | ||
showError = true, | ||
name, | ||
hint, | ||
placeholder, | ||
children, | ||
error: customError, | ||
Check failure on line 21 in components/FormField.tsx GitHub Actions / typescript
|
||
...props | ||
}: { | ||
label?: string; | ||
showError?: boolean; | ||
name: string; | ||
hint?: string; | ||
placeholder?: string; | ||
children?: (props: { form: FormikProps<any>; meta: any; field: any }) => JSX.Element; | ||
required?: boolean; | ||
min?: number; | ||
max?: number; | ||
inputType?: string; | ||
disabled?: boolean; | ||
htmlFor?: string; | ||
}) { | ||
const intl = useIntl(); | ||
const htmlFor = props.htmlFor || `input-${name}`; | ||
const { schema } = useContext(FormikZodContext); | ||
|
||
return ( | ||
<Field name={name}> | ||
{({ field, form, meta }) => { | ||
const hasError = Boolean(meta.error && (meta.touched || form.submitCount)) || Boolean(customError); | ||
const error = customError || meta.error; | ||
|
||
const fieldAttributes = { | ||
...(schema ? getInputAttributesFromZodSchema(schema, name) : null), | ||
...pickBy( | ||
{ | ||
...field, | ||
name: name || htmlFor, | ||
id: htmlFor, | ||
type: props.inputType, | ||
disabled: props.disabled, | ||
min: props.min, | ||
max: props.max, | ||
required: props.required, | ||
error: hasError, | ||
placeholder, | ||
}, | ||
value => value !== undefined, | ||
), | ||
}; | ||
console.log({ fieldAttributes }); | ||
if ( | ||
'required' in fieldAttributes && | ||
!fieldAttributes.required && | ||
meta.error && | ||
meta.error === intl.formatMessage(RICH_ERROR_MESSAGES.requiredValue) | ||
) { | ||
fieldAttributes.required = true; | ||
} | ||
return ( | ||
<div className="flex w-full flex-col gap-1"> | ||
{label && <Label className="leading-normal">{label}</Label>} | ||
{hint && <p className="text-sm text-muted-foreground">{hint}</p>} | ||
{children ? children({ form, meta, field: fieldAttributes }) : <Input {...fieldAttributes} />} | ||
{hasError && showError && ( | ||
<p className="text-xs text-red-600">{isOCError(error) ? formatFormErrorMessage(intl, error) : error}</p> | ||
)} | ||
</div> | ||
); | ||
}} | ||
</Field> | ||
); | ||
} |
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
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
Oops, something went wrong.