-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using templates and defaultValue results in an empty option on focus #424
Comments
I'm having a similar problem. In my case, it seems that the issue is caused by the |
Have also come across this - similarly my template functions expect an object. My hacky solution is for my template functions to test if the've been given an object - if not, they just render return whatever they were given. |
Have realised my above solution isn't ideal where the template to display is something other than a string. For our schools search, we return results like this: Because of this bug, this ends up as: If I have my suggestion template check if This isn't terrible but is also not showing what it's meant to show. Will see if I can hack around something showing. It seems there's two issues:
|
I slightly feel that if the Autocomplete gets |
I have another (hacky) solution - avoid sending Instead, after the autocomplete is initialised, use your own javascript to set the value of the input directly. When focused, the autocomplete treats this as a search with a query so runs the regular |
@edwardhorsford When I do this the autocomplete suggestions list automatically appears on page load (albeit only showing the first option) but I would like it to not appear at all. Have you got any suggestions to work around this? |
We've been having the same issue with the defaultValues. It's a very hacky workaround but it does at least work. |
Also added workaround for known bug of accessible-autocomplete (alphagov/accessible-autocomplete#424) where using templates shows "undefined" on first click/focus Need to fix contrast issues on highlight
## Description of change - Added offence type to Offence - Used a template for main offence suggestion autocomplete to include offence type as well as description ## Link to relevant ticket [Update main offence list](https://dsdmoj.atlassian.net/browse/CRM457-326) ## Notes for reviewer The `clearUndefinedSuggestions()` function is a pretty awkward and not very good workaround for a [bug in the accessible-autocomplete ](alphagov/accessible-autocomplete#424) - as it stands ## Screenshots of changes (if applicable) ### Before changes: ![image](https://github.com/ministryofjustice/laa-claim-non-standard-magistrate-fee-backend/assets/26544538/9ef44898-07d8-4040-bdc2-f0e216cd2b96) ### After changes: ![image](https://github.com/ministryofjustice/laa-claim-non-standard-magistrate-fee-backend/assets/26544538/940cc112-8c0c-48e7-a279-f6acc3857472) ## TO DO - [x] Change on hover/selected styling of text in accessible autocomplete's list items to be white instead of grey to fix contrast issues - [x] Add tests for JS code - [x] Error handling where necessary (this may include figuring out a different way of doing the autocomplete bug workaround as it isnt airtight by any means)
Adding the accessible autocomplete element meant that when completing a text search from the home page (non-js route) the input value is no longer populated when the search page is loaded. We have added a default value on init autocomplete. There is a known issue with autocomplete (alphagov/accessible-autocomplete#424), that setting a default value results in a suggestion with the text 'undefined'. This is bedause the default value is treated as a selected item, even though it is likely to be a partial search term, and is a string instead of an Aucomplete empty object. The workaround for this is to check if the result is a string when setting the custom template.
Adding the accessible autocomplete element meant that when completing a text search from the home page (non-js route) the input value is no longer populated when the search page is loaded. We have added a default value on init autocomplete. There is a known issue with autocomplete (alphagov/accessible-autocomplete#424), that setting a default value results in a suggestion with the text 'undefined'. This is bedause the default value is treated as a selected item, even though it is likely to be a partial search term, and is a string instead of an Aucomplete empty object. The workaround for this is to check if the result is a string when setting the custom template.
Adding the accessible autocomplete element meant that when completing a text search from the home page (non-js route) the input value is no longer populated when the search page is loaded. We have added a default value on init autocomplete. There is a known issue with autocomplete (alphagov/accessible-autocomplete#424), that setting a default value results in a suggestion with the text 'undefined'. This is bedause the default value is treated as a selected item, even though it is likely to be a partial search term, and is a string instead of an Aucomplete empty object. The workaround for this is to check if the result is a string when setting the custom template.
Using the 'templates' option in tandem with 'defaultValue' results in an empty (or undefined) suggestion when you've give focus to the input.
I've created a codepen to show the scenario and detailed repro steps are below.
In this example, the suggestion is 'undefined':
because I'm returning undefined from the
suggestion
function, but if you return empty string then you still get an 'empty' suggestion:.
This is somewhat related to #240 (I think), in that my issue is only manifesting itself, because of the behavior from #240.
Repro steps
defaultValue
propertytemplates.inputValue
to a functiontemplates.suggestion
to a functionExpected result
Nothing happens
Actual result
A sugestion of undefined appears
Suggested fix
I think this is because inside autocomplete.js the initial
options
state doesn't use theisQueryAnOption
function, which in turn usestemplateInputValue
. So line 65 could be:OR we change the options.map call within the render, to only render options where the result of
this.templateSuggestion(option)
is not falsey.I think the second option is better, but would appreciate any comments!
The text was updated successfully, but these errors were encountered: