Skip to content

Commit

Permalink
Add Multi-Selector (#18)
Browse files Browse the repository at this point in the history
* multi-selector added

* removed debug printing

* Select bug fixed

* dependency to Random added

* review comments included
  • Loading branch information
lungben authored Aug 14, 2020
1 parent 1e16bf9 commit 7251d84
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/Builtins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ end

get(textfield::TextField) = textfield.default


"""A dropdown menu (`<select>`) - the user can choose one of the `options`, an array of `String`s.
See [`MultiSelect`](@ref) for a version that allows multiple selected items.
`options` can also be an array of pairs `key::String => value::Any`. The `key` is returned via `@bind`; the `value` is shown.
See the [Mozilla docs about `select`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select)
Expand Down Expand Up @@ -182,6 +183,44 @@ end

get(select::Select) = ismissing(select.default) ? first(select.options).first : select.default


"""A multi-selector (`<select multi>`) - the user can choose one or more of the `options`, an array of `Strings.
See [`Select`](@ref) for a version that allows only one selected item.
`options` can also be an array of pairs `key::String => value::Any`. The `key` is returned via `@bind`; the `value` is shown.
See the [Mozilla docs about `select`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select)
# Examples
`@bind veg MultiSelect(["potato", "carrot"])`
`@bind veg MultiSelect(["potato" => "🥔", "carrot" => "🥕"])`
`@bind veg MultiSelect(["potato" => "🥔", "carrot" => "🥕"], default=["carrot"])`"""
struct MultiSelect
options::Array{Pair{<:AbstractString,<:Any},1}
default::Union{Missing, AbstractVector{AbstractString}}
end
MultiSelect(options::Array{<:AbstractString,1}; default=missing) = MultiSelect([o => o for o in options], default)
MultiSelect(options::Array{<:Pair{<:AbstractString,<:Any},1}; default=missing) = MultiSelect(options, default)

function show(io::IO, ::MIME"text/html", select::MultiSelect)
withtag(io, Symbol("select multiple")) do
for o in select.options
print(io, """<option value="$(htmlesc(o.first))"$(!ismissing(select.default) && o.first select.default ? " selected" : "")>""")
if showable(MIME"text/html"(), o.second)
show(io, MIME"text/html"(), o.second)
else
print(io, o.second)
end
print(io, "</option>")
end
end
end

get(select::MultiSelect) = ismissing(select.default) ? Any[] : select.default

"""A file upload box. The chosen file will be read by the browser, and the bytes are sent back to Julia.
The optional `accept` argument can be an array of `MIME`s. The user can only select files with these MIME. If only `image/*` MIMEs are allowed, then smartphone browsers will open the camera instead of a file browser.
Expand Down

2 comments on commit 7251d84

@fonsp
Copy link
Member

@fonsp fonsp commented on 7251d84 Aug 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/19386

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.0 -m "<description of version>" 7251d84768dbe8495f3c1abf05f8a9c5f1fbcd97
git push origin v0.6.0

Please sign in to comment.