Skip to content

Commit

Permalink
Allow passing ref via custom props (Hacker0x01#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisdiamond authored and aij committed Dec 11, 2017
1 parent f33c25b commit 224d478
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ General datepicker component.
|`children`|`node`|||
|`className`|`string`|||
|`customInput`|`element`|||
|`customInputRef`|`string`|`'ref'`|The property used to pass the ref callback|
|`dateFormat`|`union(string\|array)`|`'L'`||
|`dateFormatCalendar`|`string`|`'MMMM YYYY'`||
|`dayClassName`|`func`|||
Expand Down
4 changes: 3 additions & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class DatePicker extends React.Component {
children: PropTypes.node,
className: PropTypes.string,
customInput: PropTypes.element,
customInputRef: PropTypes.string,
dateFormat: PropTypes.oneOfType([ // eslint-disable-line react/no-unused-prop-types
PropTypes.string,
PropTypes.array
Expand Down Expand Up @@ -461,13 +462,14 @@ export default class DatePicker extends React.Component {
})

const customInput = this.props.customInput || <input type="text" />
const customInputRef = this.props.customInputRef || 'ref'
const inputValue =
typeof this.props.value === 'string' ? this.props.value
: typeof this.state.inputValue === 'string' ? this.state.inputValue
: safeDateFormat(this.props.selected, this.props)

return React.cloneElement(customInput, {
ref: (input) => { this.input = input },
[customInputRef]: (input) => { this.input = input },
value: inputValue,
onBlur: this.handleBlur,
onChange: this.handleChange,
Expand Down

0 comments on commit 224d478

Please sign in to comment.