Restore ClickAwayListener usage to SearchBarView
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Fri, 21 Dec 2018 15:46:06 +0000 (16:46 +0100)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Fri, 21 Dec 2018 15:46:06 +0000 (16:46 +0100)
Feature #14649

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/views-components/search-bar/search-bar-view.tsx

index 8d767b2b6affc816fcd57f2fcc94df7f2ca4c266..51ea3fa14793fe9ff3fe19b939b867c3db52a497 100644 (file)
@@ -11,7 +11,7 @@ import {
     WithStyles,
     Tooltip,
     InputAdornment, Input,
-    Popover,
+    ClickAwayListener
 } from '@material-ui/core';
 import SearchIcon from '@material-ui/icons/Search';
 import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown';
@@ -144,99 +144,49 @@ const handleDropdownClick = (e: React.MouseEvent, props: SearchBarViewProps) =>
 };
 
 export const SearchBarView = withStyles(styles)(
-    class SearchBarView extends React.Component<SearchBarViewProps> {
-
-        viewAnchorRef = React.createRef<HTMLDivElement>();
-
-        render() {
-            const { children, ...props } = this.props;
-            const { classes } = props;
-            return (
-                <Paper className={classes.container}>
-                    <div ref={this.viewAnchorRef}>
-                        <form onSubmit={props.onSubmit}>
-                            <SearchInput {...props} />
-                        </form>
+    (props : SearchBarViewProps) => {
+        const { classes, isPopoverOpen } = props;
+        return (
+            <ClickAwayListener onClickAway={props.closeView}>
+                <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
+                    <form onSubmit={props.onSubmit}>
+                        <Input
+                            className={classes.input}
+                            onChange={props.onChange}
+                            placeholder="Search"
+                            value={props.searchValue}
+                            fullWidth={true}
+                            disableUnderline={true}
+                            onClick={e => handleInputClick(e, props)}
+                            onKeyDown={e => handleKeyDown(e, props)}
+                            startAdornment={
+                                <InputAdornment position="start">
+                                    <Tooltip title='Search'>
+                                        <IconButton type="submit">
+                                            <SearchIcon />
+                                        </IconButton>
+                                    </Tooltip>
+                                </InputAdornment>
+                            }
+                            endAdornment={
+                                <InputAdornment position="end">
+                                    <Tooltip title='Advanced search'>
+                                        <IconButton onClick={e => handleDropdownClick(e, props)}>
+                                            <ArrowDropDownIcon />
+                                        </IconButton>
+                                    </Tooltip>
+                                </InputAdornment>
+                            } />
+                    </form>
+                    <div className={classes.view}>
+                        {isPopoverOpen && getView({...props})}
                     </div>
-                    <SearchViewContainer
-                        {...props}
-                        width={this.getViewWidth()}
-                        anchorEl={this.viewAnchorRef.current}>
-                        <form onSubmit={props.onSubmit}>
-                            <SearchInput
-                                {...props}
-                                autoFocus
-                                disableClickHandler />
-                        </form>
-                        {getView({ ...props })}
-                    </SearchViewContainer>
                 </Paper >
-            );
-        }
-
-        getViewWidth() {
-            const { current } = this.viewAnchorRef;
-            return current ? current.offsetWidth : 0;
-        }
+            </ClickAwayListener>
+        );
     }
-
 );
 
-const SearchInput = (props: SearchBarViewProps & { disableClickHandler?: boolean; autoFocus?: boolean }) => {
-    const { classes } = props;
-    return <Input
-        autoFocus={props.autoFocus}
-        className={classes.input}
-        onChange={props.onChange}
-        placeholder="Search"
-        value={props.searchValue}
-        fullWidth={true}
-        disableUnderline={true}
-        onClick={e => !props.disableClickHandler && handleInputClick(e, props)}
-        onKeyDown={e => handleKeyDown(e, props)}
-        startAdornment={
-            <InputAdornment position="start">
-                <Tooltip title='Search'>
-                    <IconButton type="submit">
-                        <SearchIcon />
-                    </IconButton>
-                </Tooltip>
-            </InputAdornment>
-        }
-        endAdornment={
-            <InputAdornment position="end">
-                <Tooltip title='Advanced search'>
-                    <IconButton onClick={e => handleDropdownClick(e, props)}>
-                        <ArrowDropDownIcon />
-                    </IconButton>
-                </Tooltip>
-            </InputAdornment>
-        } />;
-};
-
-const SearchViewContainer = (props: SearchBarViewProps & { width: number, anchorEl: HTMLElement | null, children: React.ReactNode }) =>
-    <Popover
-        PaperProps={{
-            style: { width: props.width }
-        }}
-        anchorEl={props.anchorEl}
-        anchorOrigin={{
-            vertical: 'top',
-            horizontal: 'center',
-        }}
-        transformOrigin={{
-            vertical: 'top',
-            horizontal: 'center',
-        }}
-        disableAutoFocus
-        open={props.isPopoverOpen}
-        onClose={props.closeView}>
-        {
-            props.children
-        }
-    </Popover>;
-
-
 const getView = (props: SearchBarViewProps) => {
     switch (props.currentView) {
         case SearchView.AUTOCOMPLETE: