Skip to content
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

Styling input field for example when an error occurs #428

Open
aliuk2012 opened this issue Mar 19, 2020 · 2 comments
Open

Styling input field for example when an error occurs #428

aliuk2012 opened this issue Mar 19, 2020 · 2 comments
Labels
feature request User requests a new feature

Comments

@aliuk2012
Copy link
Contributor

Background

I'm attempting to style autocomplete input field to match govuk design system recommendations. The fieldset has two conditionally revealed accessible autocomplete inputs. I would like to add extra CSS classes to the input based on whether the value of the field has failed validation or caused any errors.

Looking at the source code it would seem that there no option to set or use additional class names for custom styling. (https://github.com/alphagov/accessible-autocomplete/blob/master/src/autocomplete.js#L493-L513)

There is an existing cssNamespace option that can set (https://github.com/alphagov/accessible-autocomplete/blob/master/src/autocomplete.js#L431-L433) but it does not allow the developer to add additional custom CSS classes to the component.

Scenario:

  1. The user does not select a value in the input of a required field and goes on to submit the form.
  2. The user enters or selects an invalid option and submits the form.

Approaches

CSS styling

I attempted to add CSS to my stylesheets to target the focus group. Unfortunately, this did not work because I had two autocomplete inputs within the same form-group and also highlights the inputs regardless as to whether they cause the error

.govuk-form-group--error .autocomplete__input {
  border: $govuk-border-width-form-element-error solid $govuk-error-colour;

  &:focus {
    border-color: $govuk-input-border-colour;
    box-shadow: none;
  }
}
Attempt CSS - Incorrectly styles input whether the radio buttons are at fault

Mar-19-2020 21-02-37

Attempt CSS - Incorrectly styles all autocompletes within a form-group that has errors

Mar-19-2020 21-05-43

JavaScript

I also attempted to use JavaScript to clone the CSS class names of the fallback input so that it could be mirrored. For the most part, this worked but the class names were reset after the user focuses away. I could try to bind an onfocus event to the autocomplete input to add the class names back in. This is starting to feel rather hacky.

Sample of my code
const initAutocomplete = ({element, input, path}) => {
  const $input = document.getElementById(input);
  const $el = document.getElementById(element);
  const inputValueTemplate = result => (typeof result === "string" ? result : result && result.name);
  const suggestionTemplate = result =>
    typeof result === "string" ? result : result && `${result.name} (${result.code})`;

  try {
    if($input) {
      accessibleAutocomplete({
        element: $el,
        id: $input.id,
        showNoOptionsFound: true,
        name: $input.name,
        defaultValue: $input.value,
        minLength: 3,
        source: request(path),
        templates: {
          inputValue: inputValueTemplate,
          suggestion: suggestionTemplate
        },
        onConfirm: option => ($input.value = option ? option.code : ""),
        confirmOnBlur: false
      });

      const $accessibleAutocompleteInput = $el.querySelector('.autocomplete__input');
      const existingInputClasses = $input.className.split(' ')


      for(let i=0; i < existingInputClasses.length; i++) {
        $accessibleAutocompleteInput.classList.add(existingInputClasses[i])
      }

      $input.parentNode.removeChild($input);
    }
  } catch(err) {
    console.error("Failed to initialise provider autocomplete:", err);
  }
};

This sort of works until the user sets focus and then navigations to another element. It would seem as though there is another onfocus event being trigger which resets the classes
.

Conclusion

I think this component could do additionalInputCSSClassName option that could be set when the autocomplete is initialized.

I'm happy to work on this feature if others think it would be beneficial to others and agree to it in principle. My approach would be to create a new option which if set would be added to the input when its rendered. It would not affect anything else/

@kellylee-gds kellylee-gds added awaiting triage Needs triaging by team 🕔 hours A well understood issue which we expect to take less than a day to resolve. Priority: low 🕔 days A few unknowns, but we roughly know what’s involved. and removed awaiting triage Needs triaging by team 🕔 hours A well understood issue which we expect to take less than a day to resolve. labels Mar 23, 2020
@edwardhorsford
Copy link
Contributor

I've also come here looking for how to add classes.

Ideally it would support the same classes option as the other design system macros, which adds classes to the parent element. Alternately it could clone classes from the select you're enhancing.

As it is, I think I need to add a second wrapper around autocomplete__wrapper.

@mikepspc
Copy link

Likewise. I'm trying to use this with bootstrap, and would be good to not have to repeat styling code especially for this component.

@vanitabarrett vanitabarrett added feature request User requests a new feature and removed 🕔 days A few unknowns, but we roughly know what’s involved. labels Jan 6, 2022
andreyyudin added a commit to andreyyudin/accessible-autocomplete that referenced this issue Oct 13, 2023
romaricpascal pushed a commit to andreyyudin/accessible-autocomplete that referenced this issue Jan 12, 2024
romaricpascal pushed a commit to andreyyudin/accessible-autocomplete that referenced this issue Jan 12, 2024
romaricpascal added a commit to andreyyudin/accessible-autocomplete that referenced this issue Jan 12, 2024
Co-authored-by: Romaric Pascal <romaric.pascal@digital.cabinet-office.gov.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request User requests a new feature
Projects
None yet
Development

No branches or pull requests

6 participants