Skip to content

Commit 3ddc9f5

Browse files
committed
Add props for setting form attribute on the input element
1 parent 96bf629 commit 3ddc9f5

9 files changed

+60
-7
lines changed

dist/accessible-autocomplete.min.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.

dist/accessible-autocomplete.min.js.map

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

dist/lib/accessible-autocomplete.preact.min.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.

dist/lib/accessible-autocomplete.preact.min.js.map

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

dist/lib/accessible-autocomplete.react.min.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.

dist/lib/accessible-autocomplete.react.min.js.map

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

src/autocomplete.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default class Autocomplete extends Component {
4242
displayMenu: 'inline',
4343
minLength: 0,
4444
name: 'input-autocomplete',
45+
form: '',
4546
placeholder: '',
4647
onConfirm: () => {},
4748
confirmOnBlur: true,
@@ -405,6 +406,12 @@ export default class Autocomplete extends Component {
405406
}
406407
}
407408

409+
setFormAttribute (element) {
410+
if (element && this.props.form) {
411+
element.setAttribute('form', this.props.form)
412+
}
413+
}
414+
408415
render () {
409416
const {
410417
cssNamespace,
@@ -557,7 +564,10 @@ export default class Autocomplete extends Component {
557564
onFocus={this.handleInputFocus}
558565
name={name}
559566
placeholder={placeholder}
560-
ref={(inputElement) => { this.elementReferences[-1] = inputElement }}
567+
ref={(inputElement) => {
568+
this.elementReferences[-1] = inputElement
569+
this.setFormAttribute(inputElement)
570+
}}
561571
type='text'
562572
role='combobox'
563573
required={required}

test/functional/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ describe('Autocomplete', () => {
9090
expect(inputElement.getAttribute('aria-describedby')).to.equal('autocomplete-default__assistiveHint')
9191
})
9292

93+
describe('renders with an form attribute', () => {
94+
it('is not present', () => {
95+
render(<Autocomplete />, scratch)
96+
97+
const wrapperElement = scratch.getElementsByClassName('autocomplete__wrapper')[0]
98+
const inputElement = wrapperElement.getElementsByTagName('input')[0]
99+
100+
expect(inputElement.getAttribute('form')).to.equal(null)
101+
})
102+
103+
it('is not present', () => {
104+
render(<Autocomplete form={'myForm'} />, scratch)
105+
106+
const wrapperElement = scratch.getElementsByClassName('autocomplete__wrapper')[0]
107+
const inputElement = wrapperElement.getElementsByTagName('input')[0]
108+
109+
expect(inputElement.getAttribute('form')).to.equal('myForm')
110+
})
111+
})
112+
93113
describe('renders with an aria-autocomplete attribute', () => {
94114
it('of value "list", when autoselect is not enabled', () => {
95115
render(<Autocomplete required />, scratch)

test/functional/wrapper.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,27 @@ describe('Wrapper', () => {
336336
}, 250)
337337
}, 250)
338338
})
339+
340+
it('does not add a form attribute by default', () => {
341+
const select = injectSelectToEnhanceIntoDOM(scratch)
342+
343+
accessibleAutocomplete.enhanceSelectElement({
344+
selectElement: select
345+
})
346+
347+
const autocompleteInput = scratch.querySelector('.autocomplete__input')
348+
expect(autocompleteInput.getAttribute('form')).to.equal(null)
349+
})
350+
351+
it('can define a custom form for the autocomplete element', () => {
352+
const select = injectSelectToEnhanceIntoDOM(scratch)
353+
354+
accessibleAutocomplete.enhanceSelectElement({
355+
form: 'my-custom-form',
356+
selectElement: select
357+
})
358+
359+
const autocompleteInput = scratch.querySelector('.autocomplete__input')
360+
expect(autocompleteInput.getAttribute('form')).to.equal('my-custom-form')
361+
})
339362
})

0 commit comments

Comments
 (0)