Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert FilePicker into functional component #1235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 95 additions & 107 deletions src/components/FilePicker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div data-testid="file-picker-wrapper" className={mainClass}>
<input
data-testid="file-picker-form-control"
className="form-control"
type="text"
disabled
placeholder={this.props.placeholder}
readOnly="readonly"
value={fileName}
title={fileName}
/>
<div className="input-group-btn">
{isFileSelected ? (
<Button data-testid="file-picker-remove-button" className="remove-file" onClick={this.removeFile}>
×
</Button>
) : null}
<Button
data-testid="file-picker-input-button"
inverse
onClick={this.onUploadBtnClick}
disabled={this.props.disabled || isFileSelected}
>
<span data-testid="file-picker-input-button-label">{this.props.label}</span>
<input
data-testid="file-picker-input-button-input"
className="file-input"
ref={this.fileInput}
type="file"
onChange={this.onChange}
accept={this.props.filter}
data-test-selector={this.props.dts}
/>
return (
<div data-testid="file-picker-wrapper" className={mainClass}>
<input
data-testid="file-picker-form-control"
className="form-control"
type="text"
disabled
placeholder={placeholder}
readOnly="readonly"
value={fileName}
title={fileName}
/>
<div className="input-group-btn">
{isFileSelected ? (
<Button data-testid="file-picker-remove-button" className="remove-file" onClick={removeFile}>
×
</Button>
</div>
) : null}
<Button
data-testid="file-picker-input-button"
inverse
onClick={onUploadBtnClick}
disabled={disabled || isFileSelected}
>
<span data-testid="file-picker-input-button-label">{label}</span>
<input
data-testid="file-picker-input-button-input"
className="file-input"
ref={fileInputRef}
type="file"
onChange={onChange}
accept={filter}
data-test-selector={dts}
/>
</Button>
</div>
);
}
}
</div>
);
};

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;
14 changes: 6 additions & 8 deletions src/components/FilePicker/index.spec.jsx
Original file line number Diff line number Diff line change
@@ -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('<FilePicker />', () => {
it('should render with defaults', () => {
const { getByTestId } = render(<FilePickerComponent onSelect={jest.fn()} />);
const { getByTestId } = render(<FilePicker onSelect={jest.fn()} />);
expect(getByTestId('file-picker-wrapper')).toHaveClass('filepicker-component input-group');

expect(getByTestId('file-picker-form-control')).toHaveClass('form-control');
Expand All @@ -20,9 +20,7 @@ describe('<FilePicker />', () => {

it('should show remove button and call `onSelect` when file selected', () => {
const onSelect = jest.fn();
const { getByTestId, queryByTestId } = render(
<FilePickerComponent onSelect={onSelect} dts="test-file-picker-input" />
);
const { getByTestId, queryByTestId } = render(<FilePicker onSelect={onSelect} dts="test-file-picker-input" />);

expect(getByTestId('file-picker-form-control')).toHaveAttribute('title', '');
expect(getByTestId('file-picker-form-control')).toHaveAttribute('placeholder', 'No file selected');
Expand Down Expand Up @@ -52,7 +50,7 @@ describe('<FilePicker />', () => {
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(<FilePickerComponent onSelect={onSelect} />);
const { getByTestId } = render(<FilePicker onSelect={onSelect} />);

userEvent.upload(getByTestId('file-picker-input-button-input'), file);
expect(onSelect).toHaveBeenCalledTimes(1);
Expand All @@ -68,7 +66,7 @@ describe('<FilePicker />', () => {
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(<FilePickerComponent onSelect={onSelect} />);
const { getByTestId } = render(<FilePicker onSelect={onSelect} />);

userEvent.upload(getByTestId('file-picker-input-button-input'), file);
expect(onSelect).toHaveBeenCalledTimes(1);
Expand All @@ -84,7 +82,7 @@ describe('<FilePicker />', () => {
const onSelect = jest.fn();
const onRemove = jest.fn();
const file = new File(['test'], 'test.png', { type: 'image/png' });
const { getByTestId } = render(<FilePickerComponent onSelect={onSelect} onRemove={onRemove} />);
const { getByTestId } = render(<FilePicker onSelect={onSelect} onRemove={onRemove} />);

userEvent.upload(getByTestId('file-picker-input-button-input'), file);
expect(getByTestId('file-picker-form-control')).toHaveAttribute('title', 'test.png');
Expand Down
4 changes: 2 additions & 2 deletions www/containers/props.json
Original file line number Diff line number Diff line change
Expand Up @@ -1455,15 +1455,15 @@
"src/components/FilePicker/index.jsx": [
{
"description": "",
"displayName": "FilePickerComponent",
"displayName": "FilePicker",
"methods": [
{
"name": "onChange",
"docblock": null,
"modifiers": [],
"params": [
{
"name": "changeEvent",
"name": "event",
"type": null
}
],
Expand Down