Skip to content

Commit c666fbd

Browse files
authored
Merge pull request #18 from AmyShackles/add/ability-to-specify-id
Add ability to specify id
2 parents 4a0bb77 + fbc446d commit c666fbd

File tree

12 files changed

+188
-177
lines changed

12 files changed

+188
-177
lines changed

README.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,22 @@ const ExampleClient = () => {
5959
## AutoSuggest props:
6060
| propName | Description |
6161
| --- | --- |
62-
| **type** | Passing "server" will tell the component to render an AutoSuggestServer component. If a type is not passed and a url argument is passed, the component infers the type to be server. If no type is passed (or a type other than "server" is passed), AutoSuggestClient component is rendered. |
63-
| **name** | name used for the autosuggest input label and the ids of nested elements |
62+
| **id** | value used to generate ids of nested elements |
63+
| **type** | passing "server" will tell the component to render an AutoSuggestServer component. If a type is not passed and a url argument is passed, the component infers the type to be server. If no type is passed (or a type other than "server" is passed), AutoSuggestClient component is rendered. |
64+
| **name** | name used for the autosuggest input label. If `id` prop is not provided, the `name` prop is used for generating the ids of nested elements |
6465
| **url** | endpoint to query to generate the suggestions list. Component expects the url to accept input text as a parameter, not query.<br><br>Example:<br>With "https://ntsb-server.herokuapp.com/api/accidents/makeList" as the url,<br>if a user typed 'Bo' into the input field, a request would be sent to<br>https://ntsb-server.herokuapp.com/api/accidents/makeList/Bo) |
65-
| **debounceTime** | The amount of milliseconds to wait before sending requests to url based on input |
66-
| **styles** | An object to customize the appearance of the AutoSuggest component |
66+
| **debounceTime** | the amount of milliseconds to wait before sending requests to url based on input |
67+
| **styles** | an object to customize the appearance of the AutoSuggest component |
6768
| **options** | The array of options if the autosuggest is created using a list of items already in the application |
68-
| **handleChange** | Array for updating the text in the autosuggest field |
69-
| **value** | The value of the autosuggest field |
70-
| **caseInsensitive** | Setting for Client version of AutoSuggest, whether to perform case-insensitive matches against the options array |
69+
| **handleChange** | array for updating the text in the autosuggest field |
70+
| **value** | the value of the autosuggest field |
71+
| **caseInsensitive** | setting for Client version of AutoSuggest, whether to perform case-insensitive matches against the options array |
7172

7273

7374
## AutoSuggest Default Props
7475
| propName | type | defaultValue |
7576
| --- | --- | --- |
77+
| **id** | string | "" |
7678
| **type** | string | "" |
7779
| **name** | string | "Search" |
7880
| **url** | string | "" |
@@ -106,7 +108,7 @@ Using our earlier example
106108

107109
```jsx
108110
<form onSubmit={handleSubmit}>
109-
<AutoSuggest name="Make" url="https://ntsb-server.herokuapp.com/api/accidents/makeList" handleChange={setMake} value={make}/>
111+
<AutoSuggest id="desktop-make" name="Make" url="https://ntsb-server.herokuapp.com/api/accidents/makeList" handleChange={setMake} value={make}/>
110112
<button>Submit</button>
111113
</form>
112114
```
@@ -115,23 +117,23 @@ If the user entered "brau" into the input field, the resulting html would look l
115117

116118
```html
117119
<form>
118-
<div id="Make-announcement" class="visually-hidden" aria-live="polite" style="position: absolute; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); height: 1px; width: 1px; overflow: hidden;">4 suggestions displayed. To navigate, use up and down arrow keys.
120+
<div id="desktop-make-announcement" class="visually-hidden" aria-live="polite" style="position: absolute; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); height: 1px; width: 1px; overflow: hidden;">4 suggestions displayed. To navigate, use up and down arrow keys.
119121
</div>
120122
<div>
121-
<div id="Make-searchField" role="combobox" aria-expanded="true" aria-owns="Make-input" aria-haspopup="listbox" aria-controls="Make-autosuggest-options" style="display: inline-block;">
122-
<label for="Make-input" style="display: block; font-size: 1.35rem;">Make
123+
<div id="desktop-make-searchField" role="combobox" aria-expanded="true" aria-owns="desktop-make-input" aria-haspopup="listbox" aria-controls="desktop-make-autosuggest-options" style="display: inline-block;">
124+
<label for="desktop-make-input" style="display: block; font-size: 1.35rem;">Make
123125
</label>
124-
<input id="Make-input" type="text" class="searchfield" autocomplete="off" aria-autocomplete="both" value="brau" style="padding: 0.5rem; border: 2px solid rgb(200, 200, 200); background-color: rgb(255, 255, 255); border-radius: 6px; color: rgb(0, 0, 0); font-weight: normal; font-size: 1.35rem; margin: 0px auto; width: 19rem; outline: none;">
126+
<input id="desktop-make-input" type="text" class="searchfield" autocomplete="off" aria-autocomplete="both" value="brau" style="padding: 0.5rem; border: 2px solid rgb(200, 200, 200); background-color: rgb(255, 255, 255); border-radius: 6px; color: rgb(0, 0, 0); font-weight: normal; font-size: 1.35rem; margin: 0px auto; width: 19rem; outline: none;">
125127
</div>
126-
<div class="autocompleteSuggestions" id="Make-autocomplete" style="display: block; position: absolute; border: 1px solid rgb(153, 153, 153); background: rgb(255, 255, 255); width: 20rem;">
127-
<ul id="Make-autosuggest-options" role="listbox" style="margin: 0px; padding: 0px; list-style: none;">
128-
<li role="option" id="Make-suggestion0" aria-selected="false" class="auto-suggestions" style="margin: 0px; padding: 0.5rem; font-size: 1.35rem; white-space: nowrap; overflow: hidden; cursor: default;">BRAUCH
128+
<div class="autocompleteSuggestions" id="desktop-make-autocomplete" style="display: block; position: absolute; border: 1px solid rgb(153, 153, 153); background: rgb(255, 255, 255); width: 20rem;">
129+
<ul id="desktop-make-autosuggest-options" role="listbox" style="margin: 0px; padding: 0px; list-style: none;">
130+
<li role="option" id="desktop-make-suggestion-0" aria-selected="false" class="auto-suggestions" style="margin: 0px; padding: 0.5rem; font-size: 1.35rem; white-space: nowrap; overflow: hidden; cursor: default;">BRAUCH
129131
</li>
130-
<li role="option" id="Make-suggestion1" aria-selected="false" class="auto-suggestions" style="margin: 0px; padding: 0.5rem; font-size: 1.35rem; white-space: nowrap; overflow: hidden; cursor: default;">BRAULT GLASAIR
132+
<li role="option" id="desktop-make-suggestion-1" aria-selected="false" class="auto-suggestions" style="margin: 0px; padding: 0.5rem; font-size: 1.35rem; white-space: nowrap; overflow: hidden; cursor: default;">BRAULT GLASAIR
131133
</li>
132-
<li role="option" id="Make-suggestion2" aria-selected="false" class="auto-suggestions" style="margin: 0px; padding: 0.5rem; font-size: 1.35rem; white-space: nowrap; overflow: hidden; cursor: default;">Brault
134+
<li role="option" id="desktop-make-suggestion-2" aria-selected="false" class="auto-suggestions" style="margin: 0px; padding: 0.5rem; font-size: 1.35rem; white-space: nowrap; overflow: hidden; cursor: default;">Brault
133135
</li>
134-
<li role="option" id="Make-suggestion3" aria-selected="false" class="auto-suggestions" style="margin: 0px; padding: 0.5rem; font-size: 1.35rem; white-space: nowrap; overflow: hidden; cursor: default;">Braunschmidt
136+
<li role="option" id="desktop-make-suggestion-3" aria-selected="false" class="auto-suggestions" style="margin: 0px; padding: 0.5rem; font-size: 1.35rem; white-space: nowrap; overflow: hidden; cursor: default;">Braunschmidt
135137
</li>
136138
</ul>
137139
</div>

dist/AutoSuggest.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-autosuggestions",
3-
"version": "3.2.1",
3+
"version": "3.3.0",
44
"keywords": [
55
"React",
66
"auto-suggest",

src/components/AutoSuggest.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import "../index.css";
55
import { defaultOptions } from "../utils/defaultOptions.js";
66

77
export const AutoSuggest = ({
8-
type = "",
9-
url = "",
10-
name = "Search",
8+
caseInsensitive = true,
119
debounceTime = 200,
12-
styles = defaultOptions,
13-
options = [],
14-
handleChange,
1510
disabled = false,
11+
handleChange,
12+
id,
13+
name = "Search",
14+
options = [],
15+
styles = defaultOptions,
16+
type = "",
17+
url = "",
1618
value,
17-
caseInsensitive = true
1819
}) => {
1920
const combinedStyles = {
2021
announcement: {
@@ -72,33 +73,35 @@ export const AutoSuggest = ({
7273
if (type === "server" || url) {
7374
return (
7475
<AutoSuggestServer
75-
ref={ref}
76-
name={name}
77-
url={url}
78-
type="Server"
79-
debounceTime={debounceTime}
80-
styles={combinedStyles}
81-
isOpen={isOpen}
82-
setIsOpen={setIsOpen}
83-
handleChange={handleChange}
84-
disabled={disabled}
85-
value={value}
76+
debounceTime={debounceTime}
77+
disabled={disabled}
78+
handleChange={handleChange}
79+
id={id}
80+
isOpen={isOpen}
81+
name={name}
82+
setIsOpen={setIsOpen}
83+
styles={combinedStyles}
84+
type="Server"
85+
ref={ref}
86+
url={url}
87+
value={value}
8688
/>
8789
);
8890
}
8991
return (
9092
<AutoSuggestClient
91-
ref={ref}
93+
caseInsensitive={caseInsensitive}
94+
disabled={disabled}
95+
handleChange={handleChange}
96+
id={id}
97+
isOpen={isOpen}
9298
name={name}
93-
type="Client"
9499
options={options}
95-
styles={combinedStyles}
96-
isOpen={isOpen}
100+
ref={ref}
97101
setIsOpen={setIsOpen}
98-
handleChange={handleChange}
99-
disabled={disabled}
102+
type="Client"
103+
styles={combinedStyles}
100104
value={value}
101-
caseInsensitive={caseInsensitive}
102105
/>
103106
);
104107
};

src/components/AutoSuggestClient.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { alphanumericSort } from "../utils/alphanumericSort.js";
44

55
export const AutoSuggestClient = React.forwardRef(
66
(
7-
{ name, options, styles, type, isOpen, setIsOpen, handleChange, disabled, value, caseInsensitive },
7+
{ caseInsensitive, disabled, handleChange, id, isOpen, name, options, setIsOpen, styles, type, value, },
88
ref
99
) => {
1010
const [sortedOptions, optionType] = alphanumericSort(options);
@@ -53,22 +53,23 @@ export const AutoSuggestClient = React.forwardRef(
5353

5454
return (
5555
<AutoSuggestContainer
56-
ref={ref}
56+
activeDescendant={activeDescendant}
57+
clearText={() => {
58+
handleChange();
59+
}}
60+
dataType={type}
61+
disabled={disabled}
62+
id={id}
5763
name={name}
58-
options={results}
64+
noResult={noResult}
5965
openListbox={isOpen}
66+
options={results}
67+
ref={ref}
6068
searchText={value}
69+
setActiveDescendant={setActiveDescendant}
6170
setOpenListbox={setIsOpen}
6271
setSearchText={handleInputChange}
6372
styles={styles}
64-
dataType={type}
65-
noResult={noResult}
66-
activeDescendant={activeDescendant}
67-
setActiveDescendant={setActiveDescendant}
68-
clearText={() => {
69-
handleChange();
70-
}}
71-
disabled={disabled}
7273
/>
7374
);
7475
}

src/components/AutoSuggestContainer.js

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ import { AutoSuggestOptions } from "./AutoSuggestOptions.js";
44
export const AutoSuggestContainer = React.forwardRef(
55
(
66
{
7+
activeDescendant,
8+
clearText,
9+
dataType,
10+
disabled,
11+
error = false,
12+
id,
13+
loading = false,
714
name,
15+
noResult = false,
16+
openListbox,
817
options,
9-
error = false,
10-
setSearchText,
1118
searchText = "",
12-
openListbox,
13-
setOpenListbox,
14-
clearText,
15-
noResult = false,
16-
styles,
17-
loading = false,
18-
setLoading = () => {},
19-
dataType,
20-
activeDescendant,
2119
setActiveDescendant,
22-
disabled
20+
setLoading = () => {},
21+
setOpenListbox,
22+
setSearchText,
23+
styles
2324
},
2425
inputRef
2526
) => {
@@ -133,10 +134,12 @@ export const AutoSuggestContainer = React.forwardRef(
133134
let text = highlighted.getAttribute("textvalue");
134135
setSearchText(text);
135136
};
137+
138+
const idOrName = id || name;
136139
return (
137140
<>
138141
<div
139-
id={`${name}-announcement`}
142+
id={`${idOrName}-announcement`}
140143
className="visually-hidden"
141144
aria-live="polite"
142145
data-type={dataType}
@@ -148,19 +151,19 @@ export const AutoSuggestContainer = React.forwardRef(
148151
</div>
149152
<div>
150153
<div
151-
id={`${name}-searchField`}
154+
id={`${idOrName}-searchField`}
152155
role="combobox"
153156
aria-expanded={openListbox ? "true" : "false"}
154-
aria-owns={`${name}-input`}
157+
aria-owns={`${idOrName}-input`}
155158
aria-haspopup="listbox"
156-
aria-controls={`${name}-autosuggest-options`}
159+
aria-controls={`${idOrName}-autosuggest-options`}
157160
style={styles.combobox && styles.combobox}
158161
>
159-
<label id={`${name}-label`} style={styles.searchLabel && styles.searchLabel}>
162+
<label id={`${idOrName}-label`} style={styles.searchLabel && styles.searchLabel}>
160163
{label}
161164
</label>
162165
<input
163-
id={`${name}-input`}
166+
id={`${idOrName}-input`}
164167
type="text"
165168
className={loading ? `loading searchfield` : "searchfield"}
166169
autoComplete="off"
@@ -169,7 +172,7 @@ export const AutoSuggestContainer = React.forwardRef(
169172
onChange={doSearch}
170173
onKeyDown={doKeyPress}
171174
value={searchText}
172-
aria-labelledby={`${name}-label`}
175+
aria-labelledby={`${idOrName}-label`}
173176
style={styles.searchField && styles.searchField}
174177
aria-activedescendant={activeDescendant}
175178
disabled={disabled ? true : false}
@@ -178,25 +181,25 @@ export const AutoSuggestContainer = React.forwardRef(
178181
{loading && <p style={styles.announcement && styles.announcement}>Loading {label} options</p>}
179182
<div
180183
className="autocompleteSuggestions"
181-
id={`${name}-autocomplete`}
184+
id={`${idOrName}-autocomplete`}
182185
style={
183186
styles.suggestionsContainer
184187
? {
185-
...styles.suggestionsContainer,
186-
display: openListbox ? "block" : "none"
187-
}
188+
...styles.suggestionsContainer,
189+
display: openListbox ? "block" : "none"
190+
}
188191
: { display: openListbox ? "block" : "none" }
189192
}
190193
>
191194
{openListbox && options.length > 0 && (
192195
<AutoSuggestOptions
193-
ref={suggestionRef}
194-
id={`${name}-autosuggest-options`}
195-
options={options}
196-
onClick={(e) => copyTextRemoveSuggestions(e)}
197-
styles={styles}
196+
id={idOrName}
198197
name={name}
198+
onClick={(e) => copyTextRemoveSuggestions(e)}
199+
options={options}
200+
ref={suggestionRef}
199201
selected={activeDescendant}
202+
styles={styles}
200203
/>
201204
)}
202205
</div>

src/components/AutoSuggestOption.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
import React from "react";
22

3-
export const AutoSuggestOption = ({
4-
id,
5-
styles,
6-
value,
7-
selected,
8-
onClick,
9-
name = undefined,
10-
abbr = undefined
11-
}) => {
3+
export const AutoSuggestOption = ({ abbr = undefined, id, name = undefined, onClick, selected, styles, value }) => {
124
return (
135
<li
14-
role="option"
15-
id={id}
6+
abbr={abbr && abbr}
167
aria-selected={id === selected}
17-
className={id === selected ? `auto-suggestions highlighted` : 'auto-suggestions'}
8+
className={id === selected ? `auto-suggestions highlighted` : "auto-suggestions"}
9+
id={id}
10+
name={name && name}
11+
onClick={onClick}
12+
role="option"
1813
style={styles.suggestionOption && styles.suggestionOption}
1914
textvalue={value}
20-
onClick={onClick}
21-
name={name && name}
22-
abbr={abbr && abbr}
2315
>
2416
{value}
2517
</li>

0 commit comments

Comments
 (0)