Skip to content

Commit

Permalink
fix(dropdown): now closes on clicking outside for mobile devices
Browse files Browse the repository at this point in the history
  • Loading branch information
ritz078 committed Jul 23, 2018
1 parent 5cfc416 commit 5072986
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/components/DropDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ class DropDown extends React.PureComponent<DropdownProps, DropdownState> {
);
};

addClickListener = () =>
this.props.closeOnClickOutside &&
document.addEventListener("mousedown", this.handleOutsideClick);
addClickListener = () => {
if (this.props.closeOnClickOutside) {
document.addEventListener("mousedown", this.handleOutsideClick);
document.addEventListener("touchstart", this.handleOutsideClick);
}
};

removeClickListener = () =>
removeClickListener = () => {
document.removeEventListener("mousedown", this.handleOutsideClick);
document.removeEventListener("touchstart", this.handleOutsideClick);
};

handleOutsideClick = (e: MouseEvent) => {
if (
Expand All @@ -52,6 +57,12 @@ class DropDown extends React.PureComponent<DropdownProps, DropdownState> {
this.removeClickListener();
}

componentDidMount() {
if (this.props.initiallyOpen) {
this.addClickListener();
}
}

render() {
const {
buttonLabel,
Expand Down
5 changes: 4 additions & 1 deletion src/components/Popper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ export default class extends React.PureComponent<PopperProps, PopperState> {
};

componentDidMount() {
if (this.props.closeOnClickOutside)
if (this.props.closeOnClickOutside) {
document.addEventListener("mousedown", this.handleOutsideClick);
document.addEventListener("touchstart", this.handleOutsideClick);
}
}

componentWillUnmount() {
document.removeEventListener("mousedown", this.handleOutsideClick);
document.removeEventListener("touchstart", this.handleOutsideClick);
}

render() {
Expand Down
1 change: 1 addition & 0 deletions src/components/styles/Button.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const buttonStyle = css({
border: 0,
color: violet.base,
minWidth: 0,
padding: 0,
fontSize: 14,
":not(.__pebble__button__disabled):hover": {
textDecoration: "underline"
Expand Down
2 changes: 1 addition & 1 deletion src/components/typings/Input.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";

export interface InputProps {
type?: "text" | "date" | "password" | "number";
type?: "text" | "date" | "password" | "number" | "email";
required?: boolean;
placeholder: string;
onChange: (text: string) => void;
Expand Down
2 changes: 1 addition & 1 deletion stories/input.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const className = css({
width: 400
});

const type = ["text", "date", "password"];
const type = ["text", "date", "password", "email"];

storiesOf("Input", module)
.add(
Expand Down

0 comments on commit 5072986

Please sign in to comment.