Skip to content

Commit

Permalink
Fix for ctrl key having no impact on submit process (freeCodeCamp#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
joops75 authored and Bouncey committed Jul 27, 2018
1 parent 0a74065 commit 690f2ec
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions src/templates/Challenges/project/ProjectForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,61 @@ const options = {
};

export class ProjectForm extends PureComponent {
constructor(props) {
super(props);
this.state = {
keysDown: {
Control: false,
Enter: false
}
};
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleKeyUp = this.handleKeyUp.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
componentDidMount() {
this.props.updateProjectForm({});
window.addEventListener('keydown', this.handleKeyDown);
window.addEventListener('keyup', this.handleKeyUp);
}
componentDidUpdate() {
this.props.updateProjectForm({});
}
handleSubmit = values => {
this.props.openModal('completion');
this.props.updateProjectForm(values);
};

componentWillUnmount() {
window.removeEventListener('keydown', this.handleKeyDown);
window.removeEventListener('keyup', this.handleKeyUp);
}
handleKeyDown(e) {
if (e.key === 'Control') {
this.setState(state => ({
...state, keysDown: { ...state.keysDown, Control: true }
}));
}
if (e.key === 'Enter') {
this.setState(state => ({
...state, keysDown: { ...state.keysDown, Enter: true }
}));
}
}
handleKeyUp(e) {
if (e.key === 'Control') {
this.setState(state => ({
...state, keysDown: { ...state.keysDown, Control: false }
}));
}
if (e.key === 'Enter') {
this.setState(state => ({
...state, keysDown: { ...state.keysDown, Enter: false }
}));
}
}
handleSubmit(values) {
const { keysDown: { Control, Enter } } = this.state;
if (Control && Enter || !Enter) {
this.props.openModal('completion');
this.props.updateProjectForm(values);
}
}
render() {
const { isSubmitting, isFrontEnd } = this.props;
const buttonCopy = isSubmitting
Expand Down

0 comments on commit 690f2ec

Please sign in to comment.