diff --git a/src/components/FilePicker/index.jsx b/src/components/FilePicker/index.jsx index 955af3f3d..8b1d74ae8 100644 --- a/src/components/FilePicker/index.jsx +++ b/src/components/FilePicker/index.jsx @@ -6,122 +6,114 @@ 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, setIsFileSelected] = React.useState(false); - 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) { + setIsFileSelected(true); + 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; + setIsFileSelected(false); + setFileName(''); + if (onRemove) { + 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 ( -
- -
- {isFileSelected ? ( - - ) : null} - -
+ ) : null} +
- ); - } -} + + ); +}; + +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/src/components/FilePicker/styles.scss b/src/components/FilePicker/styles.scss index 8347aa993..da410bf17 100644 --- a/src/components/FilePicker/styles.scss +++ b/src/components/FilePicker/styles.scss @@ -11,7 +11,6 @@ background-color: $color-disabled; border: 0; box-shadow: none; - height: 28px; margin-right: 0; } @@ -21,6 +20,7 @@ .aui--button { box-shadow: none; + height: 28px; &:focus { background-color: inherit; diff --git a/www/containers/props.json b/www/containers/props.json index 00eeafbdc..5de81ce29 100644 --- a/www/containers/props.json +++ b/www/containers/props.json @@ -957,13 +957,13 @@ "methods": [ { "name": "usePreventSwipeClicks", - "docblock": "A hook to prevent child click handlers from firing immediately after swiping the Carousel\n\n@returns {object} - handlers to be spread onto any carousel items with `onClick` handlers", + "docblock": "A hook to prevent child click handlers from firing immediately after swiping the Carousel\n\n@returns {object} - to be spread onto any carousel items with `onClick` handlers", "modifiers": [ "static" ], "params": [], "returns": { - "description": "handlers to be spread onto any carousel items with `onClick` handlers", + "description": "to be spread onto any carousel items with `onClick` handlers", "type": { "name": "object" } @@ -1444,7 +1444,7 @@ "src/components/FilePicker/index.jsx": [ { "description": "", - "displayName": "FilePickerComponent", + "displayName": "FilePicker", "methods": [ { "name": "onChange", @@ -1452,7 +1452,7 @@ "modifiers": [], "params": [ { - "name": "changeEvent", + "name": "event", "type": null } ],