Skip to content

Commit

Permalink
DatePicker ref (#52)
Browse files Browse the repository at this point in the history
* changes DatePicker to use a React Ref

* fixes small CSS bug in example
  • Loading branch information
jtrein committed Apr 10, 2019
1 parent f120b2b commit 422fc93
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions example/index.js
Expand Up @@ -173,6 +173,7 @@ const styles = {
padding: '12px 24px',
position: 'fixed',
right: 0,
top: 0,
},
pre: {
wordBreak: 'break-all',
Expand Down
10 changes: 4 additions & 6 deletions src/DatePicker.js
Expand Up @@ -21,6 +21,7 @@ type State = {
export class DatePicker extends React.PureComponent<Props, State> {
id: string;
flatpickr: flatpickr;
flatpickrRef: {current: null | HTMLInputElement} = React.createRef();

state = {
enableTime: this.props.enableTime,
Expand All @@ -31,7 +32,7 @@ export class DatePicker extends React.PureComponent<Props, State> {

const { cleanName } = this.props;
const timestamp = new Date().getTime().toString();
this.id = this.id || `date_${cleanName}_${timestamp}`;
this.id = `date_${cleanName}_${timestamp}`;

const self: any = this;
self.onChange = this.onChange.bind(this);
Expand All @@ -57,11 +58,7 @@ export class DatePicker extends React.PureComponent<Props, State> {
options.defaultDate = new Date(parseInt(this.props.savedValue));
}

this.flatpickr = flatpickr(document.getElementById(this.id), options);
}

componentWillUnmount() {
this.flatpickr.destroy();
this.flatpickr = flatpickr(this.flatpickrRef.current, options);
}

get isIOS() {
Expand Down Expand Up @@ -92,6 +89,7 @@ export class DatePicker extends React.PureComponent<Props, State> {
<input
id={this.id}
placeholder={description}
ref={this.flatpickrRef}
/>
</label>

Expand Down

0 comments on commit 422fc93

Please sign in to comment.