A major problem in Susper was that we were using the same components on different pages, with different styling properties. A major issue was that the Autocomplete box was not properly aligned in the index page and looked like this:
This was happening because the autocomplete box width was set for 634 px, a width perfect for the search bar in the results page. The index page had a search bar of width 534 px, and the autocomplete box was too large for that.
Here is how the issue was solved:
- Changing the suggestion box html code:
The code uses *ngIf which is why setting the autocomplete box width using the typescript files becomes impossible. *ngIf does not load the component into the DOM until there are results, hence we didnot have the autocomplete box in the DOM until after the query was typed in the search bar. That was why we could not set its width, hence it was decided to remove this attribute. Using the ‘hidecomponent emitter’ is a better option here (used in the typescript file).
See autocomplete.component.ts for the complete code.
- It is now required to dynamically change the id of the suggestion-box depending on the page it is on, and apply the correct CSS.
Here is the html code:
The id of the suggestion box will now depend on the value returned from the function getID(), defined as follows:
- We first check if the route url is simply ‘/’ (which implies it is in the index page).
- If yes the id is set to index-sug-box otherwise to sug-box.
Now we can write extra CSS properties for the index-sug-box id as follows:
- To understand components in Angular: https://angular.io/api/core/Component