Converted DateInput into functional component

This commit is contained in:
Alejandro Celaya 2018-11-01 09:16:18 +01:00
parent cccf57a35a
commit 70ebb0362a
2 changed files with 33 additions and 33 deletions

View file

@ -3,35 +3,40 @@ import { isNil } from 'ramda';
import DatePicker from 'react-datepicker'; import DatePicker from 'react-datepicker';
import FontAwesomeIcon from '@fortawesome/react-fontawesome'; import FontAwesomeIcon from '@fortawesome/react-fontawesome';
import calendarIcon from '@fortawesome/fontawesome-free-regular/faCalendarAlt'; import calendarIcon from '@fortawesome/fontawesome-free-regular/faCalendarAlt';
import * as PropTypes from 'prop-types';
import './DateInput.scss'; import './DateInput.scss';
export default class DateInput extends React.Component { const propTypes = {
constructor(props) { className: PropTypes.string,
super(props); isClearable: PropTypes.bool,
this.inputRef = props.ref || React.createRef(); selected: PropTypes.oneOfType([ PropTypes.string, PropTypes.object ]),
} ref: PropTypes.object,
};
render() { const DateInput = (props) => {
const { className, isClearable, selected } = this.props; const { className, isClearable, selected, ref = React.createRef() } = props;
const showCalendarIcon = !isClearable || isNil(selected); const showCalendarIcon = !isClearable || isNil(selected);
return ( return (
<div className="date-input-container"> <div className="date-input-container">
<DatePicker <DatePicker
{...this.props} {...props}
className={`date-input-container__input form-control ${className || ''}`} className={`date-input-container__input form-control ${className || ''}`}
dateFormat="YYYY-MM-DD" dateFormat="YYYY-MM-DD"
readOnly readOnly
ref={this.inputRef} ref={ref}
/> />
{showCalendarIcon && ( {showCalendarIcon && (
<FontAwesomeIcon <FontAwesomeIcon
icon={calendarIcon} icon={calendarIcon}
className="date-input-container__icon" className="date-input-container__icon"
onClick={() => this.inputRef.current.input.focus()} onClick={() => ref.current.input.focus()}
/> />
)} )}
</div> </div>
); );
} };
}
DateInput.propTypes = propTypes;
export default DateInput;

View file

@ -13,12 +13,7 @@ describe('<DateInput />', () => {
return wrapped; return wrapped;
}; };
afterEach(() => { afterEach(() => wrapped && wrapped.unmount());
if (wrapped !== undefined) {
wrapped.unmount();
wrapped = undefined;
}
});
it('wrapps a DatePicker', () => { it('wrapps a DatePicker', () => {
wrapped = createComponent(); wrapped = createComponent();