Coder Social home page Coder Social logo

darthmaure / blazor-autocomplete Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 2.36 MB

Very simple autocomplete input control for Blazor WASM

HTML 70.81% CSS 25.51% C# 3.67%
blazor blazor-webassembly autocomplete tokens html css blazor-autocomplete blazor-component csharp

blazor-autocomplete's Introduction

blazor-autocomplete

This project contains two simple controls, that I could not find outside of large, heavy frameworks. Unfortunately I am not an advanced user of Blazor and CSS styling, so there might be some issues or bugs. Feel free to fork and improve the code.

Autocomplete input

This is a simple text input control based on build-in Blazor InputText. The control displays list of matching ('Contains' method is used) elements from linked collection.

Parameter Type Default value Descrption
Source List<string> empty List of autocomplete items
Placeholder string null Placeholder
MinTextLength int 2 Minimum text length to open autcomplete popup

Example

To use the control, simply place it inside EditForm:

<EditForm Model="@Model">
    <div class="form-group">
        <Autocomplete Source="@countries" @bind-Value="@Model.CountryName" class="form-control"/>
    </div>
</EditForm>

The minimal code should be like:

@code {
    private List<string> countries = new List<string> { /*some data*/ }
    private AutocompleteModel Model = new AutocompleteModel();
}

Where AutocompleteModel is a simple POCO class with some string CountryName property:

public class AutocompleteModel
{
    public string CountryName { get; set; }
}

Example - Autocomplete control

Token control

Token control allows to present something I call 'tags'. It is typically a list of string that describe the parent model. I was using similar control in my old WPF projects to tag entries and then to be able to seach the by keywords. This control works in two modes

  • Readonly - only boud collection of tokens is displayed
  • Edit - there is also an input that allows to add new items or remove existing items

When in edit mode - use ;, Tab or Enter to confirm value from input control to be added to the collection. Use Backspace when input control is empty to remove previous token (to the left) from the collection.

Parameter Type Default value Descrption
Tokens IList<string> empty List of tokens - I do not why but Blazor requires bound model property to be also declared an IList
TokensChanged EventCallback null The callback required for two-way binding
IsReadonly bool false A flag that sets the mode of control
TokensAutocomplete List<string> null to be implemented
ExistingOnly bool false When TokensAutocomplete is set and this flag is set - it allows user to add only items from the bound autocomplete collection
InputPlaceholder string empty Placeholder for the inner input control
CssClass string form-control Use this property with name of custom css class to style the root container. By default the container has white backgound and simple thin border
TokenItemCssClass string token-item Use this param with name of custom css class to style the elements inside the control

Example

When in edit mode - place the control inside EditForm:

<EditForm Model="@Model">
    <div class="form-group">
        <TokenInput InputPlaceholder="new tag..." @bind-Tokens="Model.Tags" />
    </div>
</EditForm>

The minimal code should be like:

@code {
    private Book Model = new Book { Tags = new List<string> { "tag1", "tag2" } }
}

Where Book is a simple POCO class with some IList<string> Tags property:

public class Book
{
   public IList<string> Tags { get; set; }
}

Example - usage of TokenInput control

Use custom css classes to style the control (root panel) and the items:

.token-input-transparent {
    border: none;
    background-color: transparent;
}

.token-item-small {
    display: inline-block;
    padding: 0.75em 0.65em;
    font-size: 0.8rem;
    font-weight: 300;
    line-height: 1;
    color: #fff;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
    border-radius: 0.2rem;
    margin-left: 1px;
    margin-top: 1px;
    background-color: royalblue;
}

.token-item-small:hover {
    background-color: #f3a508
}
<TokenInput IsReadonly="true" @bind-Tokens="book.Tags"
        CssClass="token-input-transparent"
        TokenItemCssClass="token-item-small" />

Example - list of books with tags

How to use it

I am very new to Blazor, so I was unable to make it separated control lib. I will try to do it in some near future. For now: simply copy the *.razor and *.razor.css files into your e.g. Controls folder

Known bugs or things to improve

  • Clicking Arrow Up button on Keyboard on Autocomplete control popup moves caret to the beginning of input text
  • No Autocomplete for TokenIput control

Fixed issues

  • Autocomplete popup now does not move down content below when opened. It overlaps the content (as a popup with higher z-index)
  • Clicking autocomplete item with mouse buton now selects the value inside the input control

blazor-autocomplete's People

Contributors

darthmaure avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

teguh-satriya

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.