Skip to content

Commit

Permalink
Merge pull request #295 from jtomasek/generic-upload-field
Browse files Browse the repository at this point in the history
Make UploadField more generic
  • Loading branch information
mareklibra authored Nov 10, 2020
2 parents b69cd9e + d532682 commit a509bc8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
7 changes: 6 additions & 1 deletion src/components/clusterConfiguration/DiscoveryImageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,13 @@ const DiscoveryImageForm: React.FC<DiscoveryImageFormProps> = ({
name="sshPublicKey"
helperText={<SshPublicKeyHelperText />}
idPostfix="discovery"
isRequired
onBlur={onSshKeyBlur}
dropzoneProps={{
accept: '.pub',
maxSize: 2048,
onDropRejected: ({ setError }) => () => setError('File not supported.'),
}}
isRequired
/>
<ProxyFields />
</Form>
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/formik/TextAreaField.tsx
Original file line number Diff line number Diff line change
@@ -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<TextAreaProps> = ({
const TextAreaField: React.FC<TextAreaFieldProps> = ({
label,
helperText,
getErrorText,
Expand Down
32 changes: 19 additions & 13 deletions src/components/ui/formik/UploadField.tsx
Original file line number Diff line number Diff line change
@@ -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<TextAreaProps> = ({
const UploadField: React.FC<UploadFieldProps> = ({
label,
labelIcon,
helperText,
Expand All @@ -16,11 +16,13 @@ const UploadField: React.FC<TextAreaProps> = ({
isDisabled,
name,
onBlur,
allowEdittingUploadedText = true,
dropzoneProps,
}) => {
const [filename, setFilename] = React.useState<string>();
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);

Expand Down Expand Up @@ -58,7 +60,7 @@ const UploadField: React.FC<TextAreaProps> = ({
>
{children}
<FileUpload
id={fieldId}
id={field.name}
style={{ resize: 'vertical' }}
validated={isValid ? 'default' : 'error'}
isRequired={isRequired}
Expand All @@ -68,18 +70,22 @@ const UploadField: React.FC<TextAreaProps> = ({
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}
/>
</FormGroup>
);
Expand Down
18 changes: 15 additions & 3 deletions src/components/ui/formik/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<HTMLTextAreaElement>) => 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<HTMLTextAreaElement>) => void;
onBlur?: (event: React.FocusEvent<HTMLDivElement>) => void;
allowEdittingUploadedText?: boolean;
dropzoneProps?: Omit<DropzoneProps, 'onDropRejected'> & {
onDropRejected?: (helpers: FieldHelperProps<string>) => DropFileEventHandler;
};
}

export interface TextAreaSecretProps extends TextAreaFieldProps {
isSet?: boolean;
isEdit: boolean;
helperTextHidden?: string;
Expand Down

0 comments on commit a509bc8

Please sign in to comment.