diff --git a/src/components/FilePicker/index.jsx b/src/components/FilePicker/index.jsx
index 955af3f3d..1453d45e0 100644
--- a/src/components/FilePicker/index.jsx
+++ b/src/components/FilePicker/index.jsx
@@ -6,122 +6,110 @@ import './styles.scss';
const baseClass = 'filepicker-component';
-class FilePickerComponent extends React.PureComponent {
- static propTypes = {
- /**
- * determines if the filePicker is disabled
- */
- disabled: PropTypes.bool,
- /**
- * data-test-selector of the filePicker
- */
- dts: PropTypes.string,
- /**
- * determines what file types the user can pick from the file input dialog box
- */
- filter: PropTypes.string,
- /**
- * determines if the filePicker is highlighted or not
- */
- isHighlighted: PropTypes.bool,
- /**
- * the label to be displayed
- */
- label: PropTypes.string,
- /**
- * function called when onRemove event is fired
- */
- onRemove: PropTypes.func,
- /**
- * function called when onSelect event is fired
- */
- onSelect: PropTypes.func.isRequired,
- /**
- * determines the placeholder when no date is selected
- */
- placeholder: PropTypes.string,
- };
-
- static defaultProps = {
- isHighlighted: false,
- label: 'Select',
- placeholder: 'No file selected',
- disabled: false,
- };
-
- constructor(props) {
- super(props);
-
- this.fileInput = React.createRef();
- }
+const FilePicker = ({ filter, dts, placeholder, onSelect, onRemove, isHighlighted, disabled, label }) => {
+ const fileInputRef = React.useRef();
+ const [fileName, setFileName] = React.useState('');
+ const isFileSelected = !!fileName;
- state = {
- isFileSelected: false,
- fileName: '',
- };
-
- onChange = (changeEvent) => {
- if (!this.state.isFileSelected) {
- this.setState({ isFileSelected: true, fileName: changeEvent.target.files[0].name });
- this.props.onSelect(changeEvent.target.files[0]);
+ const onChange = (event) => {
+ if (!isFileSelected) {
+ setFileName(event.target.files[0].name);
+ onSelect(event.target.files[0]);
}
};
- onUploadBtnClick = () => {
- this.fileInput.current.click();
+ const onUploadBtnClick = () => {
+ fileInputRef.current.click();
};
- removeFile = () => {
- this.fileInput.current.value = null;
- this.setState({ isFileSelected: false, fileName: '' });
- if (this.props.onRemove) {
- this.props.onRemove();
- }
+ const removeFile = () => {
+ fileInputRef.current.value = null;
+ setFileName('');
+ onRemove?.();
};
- render() {
- const mainClass = classNames({ [`${baseClass}-highlight`]: this.props.isHighlighted }, baseClass, 'input-group');
- const { isFileSelected, fileName } = this.state;
+ const mainClass = classNames({ [`${baseClass}-highlight`]: isHighlighted }, baseClass, 'input-group');
- return (
-
-
-
+ );
+};
+
+FilePicker.propTypes = {
+ /**
+ * determines if the filePicker is disabled
+ */
+ disabled: PropTypes.bool,
+ /**
+ * data-test-selector of the filePicker
+ */
+ dts: PropTypes.string,
+ /**
+ * determines what file types the user can pick from the file input dialog box
+ */
+ filter: PropTypes.string,
+ /**
+ * determines if the filePicker is highlighted or not
+ */
+ isHighlighted: PropTypes.bool,
+ /**
+ * the label to be displayed
+ */
+ label: PropTypes.string,
+ /**
+ * function called when onRemove event is fired
+ */
+ onRemove: PropTypes.func,
+ /**
+ * function called when onSelect event is fired
+ */
+ onSelect: PropTypes.func.isRequired,
+ /**
+ * determines the placeholder when no date is selected
+ */
+ placeholder: PropTypes.string,
+};
+
+FilePicker.defaultProps = {
+ isHighlighted: false,
+ label: 'Select',
+ placeholder: 'No file selected',
+ disabled: false,
+};
-export default FilePickerComponent;
+export default FilePicker;
diff --git a/src/components/FilePicker/index.spec.jsx b/src/components/FilePicker/index.spec.jsx
index fccfbb4ce..e65266bc5 100644
--- a/src/components/FilePicker/index.spec.jsx
+++ b/src/components/FilePicker/index.spec.jsx
@@ -1,13 +1,13 @@
import React from 'react';
import { render, cleanup, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
-import FilePickerComponent from '.';
+import FilePicker from '.';
afterEach(cleanup);
describe('
', () => {
it('should render with defaults', () => {
- const { getByTestId } = render(
);
+ const { getByTestId } = render(
);
expect(getByTestId('file-picker-wrapper')).toHaveClass('filepicker-component input-group');
expect(getByTestId('file-picker-form-control')).toHaveClass('form-control');
@@ -20,9 +20,7 @@ describe('
', () => {
it('should show remove button and call `onSelect` when file selected', () => {
const onSelect = jest.fn();
- const { getByTestId, queryByTestId } = render(
-
- );
+ const { getByTestId, queryByTestId } = render(
);
expect(getByTestId('file-picker-form-control')).toHaveAttribute('title', '');
expect(getByTestId('file-picker-form-control')).toHaveAttribute('placeholder', 'No file selected');
@@ -52,7 +50,7 @@ describe('
', () => {
it('should upload a file when input-button is clicked', () => {
const onSelect = jest.fn();
const file = new File(['test'], 'test.png', { type: 'image/png' });
- const { getByTestId } = render(
);
+ const { getByTestId } = render(
);
userEvent.upload(getByTestId('file-picker-input-button-input'), file);
expect(onSelect).toHaveBeenCalledTimes(1);
@@ -68,7 +66,7 @@ describe('
', () => {
it('should remove file selected when remove file button is clicked', () => {
const onSelect = jest.fn();
const file = new File(['test'], 'test.png', { type: 'image/png' });
- const { getByTestId } = render(
);
+ const { getByTestId } = render(
);
userEvent.upload(getByTestId('file-picker-input-button-input'), file);
expect(onSelect).toHaveBeenCalledTimes(1);
@@ -84,7 +82,7 @@ describe('
', () => {
const onSelect = jest.fn();
const onRemove = jest.fn();
const file = new File(['test'], 'test.png', { type: 'image/png' });
- const { getByTestId } = render(
);
+ const { getByTestId } = render(
);
userEvent.upload(getByTestId('file-picker-input-button-input'), file);
expect(getByTestId('file-picker-form-control')).toHaveAttribute('title', 'test.png');
diff --git a/www/containers/props.json b/www/containers/props.json
index 8e61d2cb1..4f2ff6bd3 100644
--- a/www/containers/props.json
+++ b/www/containers/props.json
@@ -1455,7 +1455,7 @@
"src/components/FilePicker/index.jsx": [
{
"description": "",
- "displayName": "FilePickerComponent",
+ "displayName": "FilePicker",
"methods": [
{
"name": "onChange",
@@ -1463,7 +1463,7 @@
"modifiers": [],
"params": [
{
- "name": "changeEvent",
+ "name": "event",
"type": null
}
],