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

Allow custom CSS styles on Typeahead component #292

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

SSchulze1989
Copy link

@SSchulze1989 SSchulze1989 commented Oct 11, 2022

see feature request: #227

Behaviour

When the component is initialized the provided values for "class" and "style" are extracted from the uncaptured attributes parameter AdditionalAttributes. The values are then put into separate private fields:

  • string _classAttribute for setting the class
  • string _styleAttribute for setting styles
  • IReadOnlyDictionary<string, objec> _capturedAttributes for the rest of the additional attributes
    When no values for "class" or "style" are provided the component shows as default.
    Otherwise the css styling from the outer div component will be overriden:
<div class="blazored-typeahead @FieldCssClasses @_classAttribute" style="@_styleAttribute">

Changes

  • Added private fields for _capturedAttributes, _classAttribute and _styleAttribute
  • Added method to extract "class" and "style" values from uncaptured attributes
  • Set _classAttribute and _styleAttribute on the outer div of the component
  • Change 'blazored-typeahead__input' css class to inherit color and background-color (default would be set from browser and not inherited)

Example

Here is a quick example showing the component working together with input groups from Bootstrap 5.0:

Default:

<div class="input-group">
    <div class="input-group-text">
        Person
    </div>
    <BlazoredTypeahead @bind-Value=PersonSelected 
                       SearchMethod=SearchPeople 
                       placeholder="Search by name...">
        <SelectedTemplate Context=person>
            @person?.Firstname @person?.Lastname
        </SelectedTemplate>
        <ResultTemplate Context=person>
            @person.Firstname @person.Lastname
        </ResultTemplate>
    </BlazoredTypeahead>
    <button class="btn btn-outline-secondary" type="button">Ok</button>
</div>

grafik

Override style

<div class="input-group">
    <div class="input-group-text">
        Person
    </div>
    <BlazoredTypeahead @bind-Value=PersonSelected
                       class="form-control p-0"
                       style="color:red;"
                       SearchMethod=SearchPeople 
                       placeholder="Search by name...">
        <SelectedTemplate Context=person>
            @person?.Firstname @person?.Lastname
        </SelectedTemplate>
        <ResultTemplate Context=person>
            @person.Firstname @person.Lastname
        </ResultTemplate>
    </BlazoredTypeahead>
    <button class="btn btn-outline-secondary" type="button">Ok</button>
</div>

grafik
grafik

Additional Information

This was just a quickly drawn together experiment because i wanted to use the component in a input-group, but since it seems to work well overall i thought i might propose this a feature.
The styling can be a bit fiddly to get right because it has to work together with all other styles used on the component. As you can see i had to set the default form-control padding to 0 to avoid double margins, but i guess it is up to the user to make this right.

All in all i think this is a good way to give the user enhance control over the design without interfering with the functionality of the component too much - and without any need to override the typehead css classes directly.

@SSchulze1989 SSchulze1989 marked this pull request as ready for review October 11, 2022 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant