Skip to content

Commit

Permalink
Add experimentalAllowAnyInput option to allow submission of value n…
Browse files Browse the repository at this point in the history
…ot in suggestions. Solves alphagov#156.
  • Loading branch information
peterjaric committed Apr 28, 2020
1 parent 0588034 commit beb32ea
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- [Pull request #415: Make React bundle work server-side in a NodeJS environment](https://github.com/alphagov/accessible-autocomplete/pull/415)
- Add `experimentalAllowAnyInput` option to allow submission of value not matching anything in suggestion list. Tests missing, must be added before creating pull request.

## 2.0.2 - 2020-01-30

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ Type: `Function`

A function that gets passed an object with the property `className` (`{ className: '' }`) and should return a string of HTML or a (P)React element. :warning: **Caution:** because this function allows you to output arbitrary HTML, you should [make sure it's trusted](https://en.wikipedia.org/wiki/Cross-site_scripting), and accessible.

#### `experimentalAllowAnyInput` (default: `false`)

Type: `Boolean`

The autocomplete will submit the entered value even if does not match a value in the list of suggestions when pressing Enter. This option has no effect if `autoselect` is `true` or if `enhanceSelectElement` is used.

### Internationalization

#### `tNoResults` (default: `() => 'No results found'`)
Expand Down
2 changes: 1 addition & 1 deletion dist/accessible-autocomplete.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/accessible-autocomplete.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/accessible-autocomplete.preact.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/accessible-autocomplete.preact.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/accessible-autocomplete.react.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/accessible-autocomplete.react.min.js.map

Large diffs are not rendered by default.

75 changes: 51 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"build:css": "csso src/autocomplete.css -o dist/accessible-autocomplete.min.css",
"build:js": "cross-env NODE_ENV=production webpack --progress --display-modules",
"build": "run-s 'build:js' 'build:css'",
"build": "run-s build:js build:css",
"dev": "cross-env NODE_ENV=development webpack-dev-server --inline --hot --progress",
"karma:dev": "cross-env NODE_ENV=test karma start test/karma.config.js",
"karma": "npm run karma:dev -- --single-run",
Expand Down Expand Up @@ -69,7 +69,7 @@
"karma-webpack": "^4.0.0-rc.2",
"mocha": "^5.2.0",
"npm-run-all": "^4.1.5",
"puppeteer": "^1.10.0",
"puppeteer": "^1.20.0",
"replace-bundle-webpack-plugin": "^1.0.0",
"sinon": "^6.3.5",
"sinon-chai": "^3.2.0",
Expand Down
12 changes: 10 additions & 2 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export default class Autocomplete extends Component {
required: false,
tNoResults: () => 'No results found',
tAssistiveHint: () => 'When autocomplete results are available use up and down arrows to review and enter to select. Touch device users, explore by touch or with swipe gestures.',
dropdownArrow: DropdownArrowDown
dropdownArrow: DropdownArrowDown,
experimentalAllowAnyInput: false
}

elementReferences = {}
Expand Down Expand Up @@ -354,8 +355,15 @@ export default class Autocomplete extends Component {

handleEnter (event) {
if (this.state.menuOpen) {
event.preventDefault()
// If not using autoselect and not using enhanceSelectElement, check if the current
// value can be submitted without selecting an option from the open menu.
const allowAnyInput = !this.props.autoselect && !this.props.selectElement && this.props.experimentalAllowAnyInput
const hasSelectedOption = this.state.selected >= 0

if (!allowAnyInput || hasSelectedOption) {
event.preventDefault()
}

if (hasSelectedOption) {
this.handleOptionClick(event, this.state.selected)
}
Expand Down

0 comments on commit beb32ea

Please sign in to comment.