Skip to content

Commit 3e48671

Browse files
committed
WIP: Add autocompletion
1 parent fd7dc79 commit 3e48671

File tree

4 files changed

+98
-21
lines changed

4 files changed

+98
-21
lines changed

components/search/forms/simple.jsx

+52-20
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
11
import React from 'react'
2-
import { Button, InputGroup, InputGroupAddon, Input } from 'reactstrap'
2+
import { Select } from '@oacore/design'
33

4-
const SearchField = ({
5-
size = '',
6-
id = 'search-form-field',
7-
label = 'Search in CORE',
8-
...fieldProps
9-
}) => (
10-
<>
11-
<label className="sr-only" htmlFor={id}>
12-
{label}
13-
</label>
14-
<InputGroup size={size}>
15-
<Input type="search" id={id} {...fieldProps} />
16-
<InputGroupAddon addonType="append">
17-
<Button color="primary">Search</Button>
18-
</InputGroupAddon>
19-
</InputGroup>
20-
</>
21-
)
4+
import styles from './styles.module.css'
5+
6+
const options = [
7+
{ id: 1, icon: '#magnify', value: 'Option A' },
8+
{ id: 2, icon: '#magnify', value: 'Option B' },
9+
{ id: 3, icon: '#magnify', value: 'Option C' },
10+
{ id: 4, icon: '#magnify', value: 'Option D' },
11+
{ id: 5, icon: '#magnify', value: 'Option E' },
12+
]
13+
14+
const SelectExample = ({ ...passProps }) => {
15+
const [suggestions, setSuggestions] = React.useState(options)
16+
const [value, setValue] = React.useState('')
17+
18+
const handleOnChange = data => {
19+
// eslint-disable-next-line no-console
20+
console.log(data)
21+
// trigger search here
22+
}
23+
24+
const handleOnInput = data => {
25+
// if id doesn't exists it means user type own text
26+
// and didn't use suggestion
27+
if (!data.id) {
28+
setSuggestions(
29+
options.slice(0, Math.max(0, options.length - data.value.length))
30+
)
31+
}
32+
33+
setValue(data.value)
34+
}
35+
36+
return (
37+
<Select
38+
id="search-select"
39+
value={value}
40+
onChange={handleOnChange}
41+
onInput={handleOnInput}
42+
prependIcon="#magnify"
43+
className={styles['search-box']}
44+
{...passProps}
45+
>
46+
{suggestions.map(el => (
47+
<Select.Option key={el.id} id={el.id} value={el.value} icon={el.icon}>
48+
{el.value}
49+
</Select.Option>
50+
))}
51+
</Select>
52+
)
53+
}
2254

2355
const SearchForm = ({
2456
action,
@@ -28,7 +60,7 @@ const SearchForm = ({
2860
...fieldProps
2961
}) => (
3062
<form id={id} action={action} method={method} onSubmit={onSubmit}>
31-
<SearchField id={`${id}-field`} size="lg" {...fieldProps} />
63+
<SelectExample id={`${id}-field`} {...fieldProps} />
3264
</form>
3365
)
3466

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.search-box {
2+
--form-control-corner-radius: 0.3rem;
3+
--form-control-color: var(--gray-500);
4+
--form-label-color: var(--gray-500);
5+
}
6+
7+
.search-box:focus-within {
8+
--form-control-color: var(--primary);
9+
}
10+
11+
.search-box:focus-within div:nth-child(2){
12+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.25);
13+
}
14+
15+
.search-box:focus-within div:nth-child(2) > * {
16+
border-color: transparent;
17+
}

design.config.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const path = require('path')
2+
3+
const icons = ['magnify']
4+
5+
const iconsRoot = path.join(
6+
path.dirname(require.resolve('@mdi/svg/package.json')),
7+
'./svg'
8+
)
9+
10+
const config = {
11+
icons: {
12+
path: iconsRoot,
13+
files: icons,
14+
},
15+
16+
output: {
17+
path: path.join(__dirname, 'public/design'),
18+
publicPath: '/design',
19+
icons: {
20+
files: 'icons',
21+
sprite: 'icons.svg',
22+
},
23+
},
24+
}
25+
26+
module.exports = config

pages/index.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ const IndexPage = () => (
8585
<SearchForm
8686
action="/search"
8787
name="q"
88-
placeholder={patchStats(page.searchPlaceholder, page.statistics)}
88+
label={patchStats(page.searchPlaceholder, page.statistics)}
89+
placeholder="e.g. article title or author name"
90+
variant="pure"
8991
/>
9092
<SearchIntro>
9193
<Markdown>{page.covid19Notice}</Markdown>

0 commit comments

Comments
 (0)