Skip to content

Commit

Permalink
Merge pull request #791 from FluxNotes/refocus-editor-datepicker
Browse files Browse the repository at this point in the history
Moved calendar onKeyDown logic to slate plugin
  • Loading branch information
Matthew Gramigna authored Oct 17, 2019
2 parents 6422b85 + 9ed82c4 commit 406af20
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/context/ContextCalendar.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
import React from 'react';
import PropTypes from 'prop-types';
import Calendar from 'rc-calendar';
import _ from 'lodash';

const ENTER_KEY = 13;
const UP_ARROW_KEY = 38;
const DOWN_ARROW_KEY = 40;
const LEFT_ARROW_KEY = 37;
const RIGHT_ARROW_KEY = 39;

class ContextCalendar extends React.Component {
handleDateSelect = (date) => {
this.props.closePortal();
const context = { key: 'set-date-id', context: `${date.format('D MMM YYYY')}`, object: date };
this.props.onSelected(this.props.state, context);
return this.props.onSelected(this.props.state, context);
}

handleArrowKey = (e) => {
e.preventDefault();
e.stopPropagation();
}

onKeyDown = (e, data, state, editor) => {
const keyCode = e.which;
if (keyCode === ENTER_KEY) {
this.handleArrowKey(e);
if (_.isUndefined(this.refs.input.state.selectedValue)) {
this.refs.input.state.selectedValue = this.refs.input.state.value;
}
return this.handleDateSelect(this.refs.input.state.selectedValue);
} else if (keyCode === UP_ARROW_KEY) {
this.handleArrowKey(e);
this.refs.input.goTime(-1, 'weeks');
} else if (keyCode === DOWN_ARROW_KEY) {
this.handleArrowKey(e);
this.refs.input.goTime(1, 'weeks');
} else if (keyCode === LEFT_ARROW_KEY) {
this.handleArrowKey(e);
this.refs.input.goTime(-1, 'days');
} else if (keyCode === RIGHT_ARROW_KEY) {
this.handleArrowKey(e);
this.refs.input.goTime(1, 'days');
}
}


render() {
return (
<Calendar
showDateInput={false}
onSelect={this.handleDateSelect}
ref={input => input && setTimeout(() => { input.focus(); }, 100)}
ref="input"
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/notes/FluxNotesEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class FluxNotesEditor extends React.Component {
closeCompletionPortal = () => {
if (this.state.completionComponentShortcut) {
// Clean up some variables stored at the Editor level

this.setState({
completionComponentShortcut: null,
portalOptions: [],
Expand Down

0 comments on commit 406af20

Please sign in to comment.