Skip to content

Commit 77e8cd8

Browse files
Merge pull request #116 from i-like-robots/id-as-prop
Configurable listbox ID
2 parents 64966a0 + 5fc24a3 commit 77e8cd8

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ ReactDOM.render(<App />, document.getElementById('app'))
6868

6969
### Options
7070

71+
- [`id`](#id-optional)
7172
- [`tags`](#tags-optional)
7273
- [`suggestions`](#suggestions-optional)
7374
- [`placeholder`](#placeholder-optional)
@@ -85,6 +86,10 @@ ReactDOM.render(<App />, document.getElementById('app'))
8586
- [`allowBackspace`](#allowbackspace-optional)
8687
- [`tagComponent`](#tagcomponent-optional)
8788

89+
#### id (optional)
90+
91+
The ID attribute given to the listbox element. Default: `ReactTags`.
92+
8893
#### tags (optional)
8994

9095
An array of tags that are displayed as pre-selected. Each tag must have an `id` and a `name` property. Default: `[]`.

lib/Input.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ class Input extends React.Component {
6363
}
6464

6565
render () {
66-
const { query, placeholder, expanded, listboxId, index } = this.props
66+
const { id, query, placeholder, expanded, classNames, index } = this.props
6767

6868
return (
69-
<div className={this.props.classNames.searchInput}>
69+
<div className={classNames.searchInput}>
7070
<input
7171
ref={(c) => { this.input = c }}
7272
value={query}
7373
placeholder={placeholder}
7474
role='combobox'
7575
aria-autocomplete='list'
7676
aria-label={placeholder}
77-
aria-owns={listboxId}
78-
aria-activedescendant={index > -1 ? `${listboxId}-${index}` : null}
77+
aria-owns={id}
78+
aria-activedescendant={index > -1 ? `${id}-${index}` : null}
7979
aria-expanded={expanded}
8080
style={{ width: this.state.inputWidth }} />
8181
<div ref={(c) => { this.sizer = c }} style={SIZER_STYLES}>{query || placeholder}</div>

lib/ReactTags.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ class ReactTags extends React.Component {
171171
}
172172

173173
render () {
174-
const listboxId = 'ReactTags-listbox'
175-
176174
const TagComponent = this.props.tagComponent || Tag
177175

178176
const tags = this.props.tags.map((tag, i) => (
@@ -190,7 +188,10 @@ class ReactTags extends React.Component {
190188

191189
return (
192190
<div className={classNames.join(' ')} onClick={this.onClick.bind(this)}>
193-
<div className={this.state.classNames.selected} aria-live='polite' aria-relevant='additions removals'>
191+
<div
192+
className={this.state.classNames.selected}
193+
aria-relevant='additions removals'
194+
aria-live='polite'>
194195
{tags}
195196
</div>
196197
<div
@@ -200,14 +201,14 @@ class ReactTags extends React.Component {
200201
onKeyDown={this.onKeyDown.bind(this)}
201202
onInput={this.onInput.bind(this)}>
202203
<Input {...this.state}
204+
id={this.props.id}
203205
ref={(c) => { this.input = c }}
204-
listboxId={listboxId}
205206
autoresize={this.props.autoresize}
206207
expanded={expanded}
207208
placeholder={this.props.placeholder} />
208209
<Suggestions {...this.state}
210+
id={this.props.id}
209211
ref={(c) => { this.suggestions = c }}
210-
listboxId={listboxId}
211212
expanded={expanded}
212213
addTag={this.addTag.bind(this)} />
213214
</div>
@@ -217,6 +218,7 @@ class ReactTags extends React.Component {
217218
}
218219

219220
ReactTags.defaultProps = {
221+
id: 'ReactTags',
220222
tags: [],
221223
placeholder: 'Add new tag',
222224
suggestions: [],
@@ -230,6 +232,7 @@ ReactTags.defaultProps = {
230232
}
231233

232234
ReactTags.propTypes = {
235+
id: PropTypes.string,
233236
tags: PropTypes.arrayOf(PropTypes.object),
234237
placeholder: PropTypes.string,
235238
suggestions: PropTypes.arrayOf(PropTypes.object),

lib/Suggestions.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class Suggestions extends React.Component {
1818
return null
1919
}
2020

21-
const options = this.props.options.map((item, i) => {
22-
const key = `${this.props.listboxId}-${i}`
21+
const options = this.props.options.map((item, index) => {
22+
const key = `${this.props.id}-${index}`
2323
const classNames = []
2424

25-
if (this.props.index === i) {
25+
if (this.props.index === index) {
2626
classNames.push(this.props.classNames.suggestionActive)
2727
}
2828

@@ -45,7 +45,7 @@ class Suggestions extends React.Component {
4545

4646
return (
4747
<div className={this.props.classNames.suggestions}>
48-
<ul role='listbox' id={this.props.listboxId}>{options}</ul>
48+
<ul role='listbox' id={this.props.id}>{options}</ul>
4949
</div>
5050
)
5151
}

0 commit comments

Comments
 (0)