GenieFramework / StippleUI.jl

StippleUI is a library of reactive UI elements for Stipple.jl.
MIT License
82 stars 15 forks source link

Client-side filtering for q-select #130

Closed svilupp closed 2 months ago

svilupp commented 2 months ago

Often you have too many options in your select() and want to allow the user to filter them out by typing the partial answer.

Behavior:

@hhaensel has kindly provided this MWE that achieves the desired behavior:

using Stipple, Stipple.ReactiveTools, StippleUI

@appname Filtering

@app begin
    @in select = ""
    @out options = ["a", "b", "c"]
    @out options_all = ["a", "b", "c"]
    @onchange isready begin
        @info "I am alive!"
    end
    @onchange select begin
        @info "Selected: $selected"
    end
end

function ui()
    [
        h3("Select"), Stipple.select(
            :select,
            options = :options,
            label = "Pick X",
            clearable = true,
            useinput = true,
            @on(:filter, "filterFn")
        )
    ]
end

@methods begin
    """
    filterFn (val, update) {
        if (val === '') {
          update(() => {
            // reset to full option list
            this.options = this.options_all
          })
          return
        }

        update(() => {
            // filter down based on user provided input
          const needle = val.toLowerCase()
          this.options = this.options_all.filter(v => v.toLowerCase().indexOf(needle) > -1)
        })
    }
"""
end

@page("/", ui)

up(open_browser = true)

Note the macro "methods" behind used above to inject the new javascript function!

Tested on StippleUI.jl version 0.23.

Thank you, Genie team!