From a4ca5c6fd82720c880cbb663a71b7982c903057f Mon Sep 17 00:00:00 2001 From: Jiri Tomasek Date: Tue, 10 Nov 2020 14:41:36 +0100 Subject: [PATCH] Make UploadField more generic To allow using it with other fields. --- .../DiscoveryImageForm.tsx | 7 +++- src/components/ui/formik/TextAreaField.tsx | 4 +-- src/components/ui/formik/UploadField.tsx | 32 +++++++++++-------- src/components/ui/formik/types.ts | 18 +++++++++-- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/components/clusterConfiguration/DiscoveryImageForm.tsx b/src/components/clusterConfiguration/DiscoveryImageForm.tsx index 4244a017bf..949d0deaa7 100644 --- a/src/components/clusterConfiguration/DiscoveryImageForm.tsx +++ b/src/components/clusterConfiguration/DiscoveryImageForm.tsx @@ -176,8 +176,13 @@ const DiscoveryImageForm: React.FC = ({ name="sshPublicKey" helperText={} idPostfix="discovery" - isRequired onBlur={onSshKeyBlur} + dropzoneProps={{ + accept: '.pub', + maxSize: 2048, + onDropRejected: ({ setError }) => () => setError('File not supported.'), + }} + isRequired /> diff --git a/src/components/ui/formik/TextAreaField.tsx b/src/components/ui/formik/TextAreaField.tsx index ad21fefd31..a02b4234e3 100644 --- a/src/components/ui/formik/TextAreaField.tsx +++ b/src/components/ui/formik/TextAreaField.tsx @@ -1,11 +1,11 @@ import * as React from 'react'; import { useField } from 'formik'; import { FormGroup, TextArea } from '@patternfly/react-core'; -import { TextAreaProps } from './types'; +import { TextAreaFieldProps } from './types'; import { getFieldId } from './utils'; import HelperText from './HelperText'; -const TextAreaField: React.FC = ({ +const TextAreaField: React.FC = ({ label, helperText, getErrorText, diff --git a/src/components/ui/formik/UploadField.tsx b/src/components/ui/formik/UploadField.tsx index 1c37579e19..ea912c8601 100644 --- a/src/components/ui/formik/UploadField.tsx +++ b/src/components/ui/formik/UploadField.tsx @@ -1,11 +1,11 @@ import * as React from 'react'; import { useField } from 'formik'; import { FormGroup, FileUpload } from '@patternfly/react-core'; -import { TextAreaProps } from './types'; +import { UploadFieldProps } from './types'; import { getFieldId } from './utils'; import HelperText from './HelperText'; -const UploadField: React.FC = ({ +const UploadField: React.FC = ({ label, labelIcon, helperText, @@ -16,11 +16,13 @@ const UploadField: React.FC = ({ isDisabled, name, onBlur, + allowEdittingUploadedText = true, + dropzoneProps, }) => { const [filename, setFilename] = React.useState(); - const [isSSHKeyUploading, setSSHKeyUploading] = React.useState(false); + const [isFileUploading, setIsFileUploading] = React.useState(false); - const [field, { touched, error }, { setError, setValue }] = useField(name); + const [field, { touched, error }, helpers] = useField(name); const fieldId = getFieldId(name, 'input', idPostfix); const isValid = !((touched || filename) && error); @@ -58,7 +60,7 @@ const UploadField: React.FC = ({ > {children} = ({ filename={filename} onChange={(value, filename) => { setFilename(filename); - setValue(value); + helpers.setTouched(true); + helpers.setValue(value); }} - onBlur={onBlur} - onReadStarted={() => setSSHKeyUploading(true)} - onReadFinished={() => setSSHKeyUploading(false)} - isLoading={isSSHKeyUploading} + onBlur={(e) => { + field.onBlur(e); + onBlur && onBlur(e); + }} + onReadStarted={() => setIsFileUploading(true)} + onReadFinished={() => setIsFileUploading(false)} + isLoading={isFileUploading} disabled={isDisabled} dropzoneProps={{ - accept: '.pub', - maxSize: 2048, - onDropRejected: () => setError('File not supported.'), + ...dropzoneProps, + onDropRejected: dropzoneProps?.onDropRejected && dropzoneProps?.onDropRejected(helpers), }} + allowEditingUploadedText={allowEdittingUploadedText} /> ); diff --git a/src/components/ui/formik/types.ts b/src/components/ui/formik/types.ts index 138c70f8b7..1872f80934 100644 --- a/src/components/ui/formik/types.ts +++ b/src/components/ui/formik/types.ts @@ -1,5 +1,6 @@ import { TextInputTypes, FormSelectOptionProps } from '@patternfly/react-core'; -import { FieldValidator } from 'formik'; +import { FieldValidator, FieldHelperProps } from 'formik'; +import { DropzoneProps, DropFileEventHandler } from 'react-dropzone'; export interface FieldProps { name: string; @@ -41,14 +42,25 @@ export interface InputFieldProps extends FieldProps { validate?: FieldValidator; } -export interface TextAreaProps extends FieldProps { +export interface TextAreaFieldProps extends FieldProps { getErrorText?: (error: string) => React.ReactNode | undefined; placeholder?: string; onChange?: (event: React.FormEvent) => void; onBlur?: () => void; } -export interface TextAreaSecretProps extends TextAreaProps { +export interface UploadFieldProps extends FieldProps { + getErrorText?: (error: string) => React.ReactNode | undefined; + placeholder?: string; + onChange?: (event: React.FormEvent) => void; + onBlur?: (event: React.FocusEvent) => void; + allowEdittingUploadedText?: boolean; + dropzoneProps?: Omit & { + onDropRejected?: (helpers: FieldHelperProps) => DropFileEventHandler; + }; +} + +export interface TextAreaSecretProps extends TextAreaFieldProps { isSet?: boolean; isEdit: boolean; helperTextHidden?: string;