Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1260 Updated search-bar input to allow autocomplete #1387

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/frontend/src/components/SearchBar/SearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ const useStyles = makeStyles((theme) => ({
},
input: {
fontSize: '1.6rem',
'&:hover': {
border: '2px solid #E5E5E5',
},
'&:focus': {
border: '2px solid #333E64',
},
'& > *': {
fontSize: '1.6rem !important',
color: theme.palette.text.default,
},
height: '50px',
height: '55px',
backgroundColor: '#E5E5E5',
paddingLeft: '10px',
border: '1px solid #A0A0A0',
borderRadius: '5px',
Comment on lines +34 to +48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to use our colour palette?.

See how it was done in other components, like here or here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I'm switching over to using the palette now.

},
header: {
padding: 0,
Expand Down Expand Up @@ -166,11 +176,7 @@ function CustomizedInputBase(props) {
variant="outlined"
list="searchData"
></input>
<datalist id="searchData">
<option>Test 1</option>
<option>Test 2</option>
<option>Test 3</option>
</datalist>
<datalist id="searchData"></datalist>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To do this properly, we're going to want to only apply the new datalist (and eventually fetch its data) if the user selects an Author filter. The easiest way to achieve this is to use the filter value in order to decide whether or not to render this. Another way to go would be to break this part of the search UI into its own components:

<SearchInput filter={filter} />

And the SearchInput.jsx could do something like this:

function AuthorSearchInput() {
  return (
    <>
      <input 
         //...rest of props here
          list="searchData"
      />
      <datalist id="searchData">...</datalist>
    </>
  );
}

function PostSearchInput() {
  return <input ... />
}

function SeachInput({ filter }) {
   return filter === 'author' ? <AuthorSearchInput /> : <PostSearchInput />;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think I see where your going here. What is the purpose of PostSearchInput() in this case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where AuthorSearchInput includes a <datalist> and a list="..." attribute, PostSearchInput does not: it only returns the <input ...> where ... is all of the attributes I didn't bother typing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait never mind, it would just be the output for a non autocomplete input.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, and if you want, it could be what it is now vs being limited to what you had to do to get the datalist to work (i.e., they can use different components/elements to render the input).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@humphd

I have the jsx page setup and I can now switch between the two different author and post inputs. I'm now trying to figure out how make both these inputs to activate the SearchPage portion of the site.

Here's were I call my SearchInput.jsx providing the filter.

<FormControl fullWidth={true}>
              <SearchInput filter={filter} />
</FormControl>

And then here's my AuthorSearchInput():

function AuthorSearchInput(props) {
  const classes = useStyles();
  const { searchText, onChangeHandler } = props;

  const onTextChange = (event) => {
    onChangeHandler(event.target.value);
  };

  return (
    <>
      <input
        // ...rest of props here
        type="text"
        id="authorSearch"
        className={classes.input}
        placeholder="How to Get Started in Open Source"
        variant="outlined"
        list="searchData"
        value={searchText}
        onChange={(event) => onTextChange(event)}
        inputProps={{ 'aria-label': 'search telescope' }}
      />
      <datalist id="searchData">
        <option>Test 1</option>
        <option>Test 2</option>
        <option>Test 3</option>
      </datalist>
    </>
  );
}

Within my SearchInput.jsx I can't seem to be able to use onChange={(event) => onTextChange(event)}
This throws an error that it is undefined. I'm unsure on how to get this working.

</FormControl>
</Grid>
</Grid>
Expand Down
1 change: 0 additions & 1 deletion src/frontend/src/components/SearchPage/SearchPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function SearchPage() {
}
const posts = await res.json();
setResults(posts.values);
console.log(results);
} catch (error) {
console.error('Something went wrong while fetching data', error);
} finally {
Expand Down