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

Accept Both Functions and Components for DayPickerInput #862

Closed
wants to merge 3 commits into from
Closed
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
32 changes: 20 additions & 12 deletions src/DayPickerInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,20 +567,28 @@ export default class DayPickerInput extends React.Component {
render() {
const Input = this.props.component;
const { inputProps } = this.props;
const props = {
placeholder: this.props.placeholder,
...inputProps,
value: this.state.typedValue || this.state.value,
onChange: this.handleInputChange,
onFocus: this.handleInputFocus,
onBlur: this.handleInputBlur,
onKeyDown: this.handleInputKeyDown,
onKeyUp: this.handleInputKeyUp,
onClick: !inputProps.disabled ? this.handleInputClick : undefined,
}
return (
<div className={this.props.classNames.container} style={this.props.style}>
<Input
ref={el => (this.input = el)}
placeholder={this.props.placeholder}
{...inputProps}
value={this.state.typedValue || this.state.value}
onChange={this.handleInputChange}
onFocus={this.handleInputFocus}
onBlur={this.handleInputBlur}
onKeyDown={this.handleInputKeyDown}
onKeyUp={this.handleInputKeyUp}
onClick={!inputProps.disabled ? this.handleInputClick : undefined}
/>
{this.props.component &&
{}.toString.call(this.props.component) !== '[object Function]' ? (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just typeof this.props.component === 'function'?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way is more verbose. Not really relevant here but one good example is that it will return Array for Array instead of Object. A better way would be to actually have a function that would extract out Function.

<Input
ref={el => (this.input = el)}
{...props}
/>
) : (
this.props.component({...props})
)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly recommend to extract common props to the const.

const props = {
            placeholder: this.props.placeholder,
            ...inputProps,
            value: this.state.typedValue || this.state.value,
            onChange: this.handleInputChange,
            onFocus: this.handleInputFocus,
            onBlur: this.handleInputBlur,
            onKeyDown: this.handleInputKeyDown,
            onKeyUp: this.handleInputKeyUp,
            onClick: !inputProps.disabled ? this.handleInputClick : undefined,
};

and then spread it to child.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted.

{this.state.showOverlay && this.renderOverlay()}
</div>
);
Expand Down
25 changes: 25 additions & 0 deletions test/daypickerinput/rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,30 @@ describe('DayPickerInput', () => {
new Date(2020, 2, 10)
);
});
it('should render a component passed as function', () => {
const wrapper = shallow(
<DayPickerInput
value="2019-2-5"
component={props => (
<div id="customDiv">
<input id="customInput" {...props} />
</div>
)}
/>
);
const customDiv = wrapper.find('#customDiv');
expect(customDiv).toBeDefined();
const input = wrapper.find('input');
expect(input).toBeDefined();
expect(input).toHaveProp('value', '2019-2-5');
expect(input).toHaveProp('placeholder', 'YYYY-M-D');
expect(input).toHaveProp('onChange');
expect(input).toHaveProp('onFocus');
expect(input).toHaveProp('onBlur');
expect(input).toHaveProp('onKeyDown');
expect(input).toHaveProp('onKeyUp');
expect(input).toHaveProp('onClick');
expect(typeof wrapper.instance().props.component).toBe('function');
});
});
});