Skip to content

move indexing from Dash.jl #5

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

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/components.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
to_dash(t::Any) = t

struct Component
struct Component
name ::String
type ::String
namespace ::String
Expand All @@ -9,9 +9,9 @@ struct Component
wildcard_regex ::Union{Nothing, Regex}
function Component(name::String, type::String, namespace::String,
props::Vector{Symbol}, wildcard_props::Vector{Symbol}; kwargs...)

available_props = Set{Symbol}(props)
wildcard_regex::Union{Nothing, Regex} = nothing
wildcard_regex::Union{Nothing, Regex} = nothing
if !isempty(wildcard_props)
wildcard_regex = Regex(join(string.(wildcard_props), "|"))
end
Expand All @@ -30,13 +30,36 @@ get_available_props(comp::Component) = getfield(comp, :available_props)
get_wildcard_regex(comp::Component) = getfield(comp, :wildcard_regex)
get_props(comp::Component) = getfield(comp, :props)

function Base.getproperty(comp::Component, prop::Symbol)
const VecChildTypes = Union{NTuple{N, Component} where {N}, Vector{<:Component}}

function Base.getindex(component::Component, id::AbstractString)
component.id == id && return component
hasproperty(component, :children) || return nothing
cc = component.children
return if cc isa Union{VecChildTypes, DashBase.Component}
cc[id]
elseif cc isa AbstractVector
fcc = identity.(filter(x->hasproperty(x, :id), cc))
isempty(fcc) ? nothing : fcc[id]
else
nothing
end
end
function Base.getindex(children::VecChildTypes, id::AbstractString)
for element in children
element.id == id && return element
el = element[id]
el !== nothing && return el
end
end

function Base.getproperty(comp::Component, prop::Symbol)
!Base.hasproperty(comp, prop) && error("Component $(get_name(comp)) has no property $(prop)")
props = get_props(comp)
return haskey(props, prop) ? props[prop] : nothing
end

function Base.setproperty!(comp::Component, prop::Symbol, value)
function Base.setproperty!(comp::Component, prop::Symbol, value)
!Base.hasproperty(comp, prop) && error("Component $(get_name(comp)) has no property $(prop)")
props = get_props(comp)
push!(props, prop=>to_dash(value))
Expand All @@ -56,4 +79,4 @@ Base.propertynames(comp::Component) = collect(get_available_props(comp))
push_prop!(component::Component, prop::Symbol, value) = push!(component.props, prop=>to_dash(value))

JSON3.StructTypes.StructType(::Type{DashBase.Component}) = JSON3.StructTypes.Struct()
JSON3.StructTypes.excludes(::Type{DashBase.Component}) = (:name, :available_props, :wildcard_regex)
JSON3.StructTypes.excludes(::Type{DashBase.Component}) = (:name, :available_props, :wildcard_regex)
50 changes: 33 additions & 17 deletions test/components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ using JSON3

@testset "components creation" begin
test_comp = Component("html_div", "Div", "dash_html_components",
[:children, :id, :n_clicks],
[Symbol("data-"), Symbol("aria-")];
id = 10,
n_clicks = 1
)
[:children, :id, :n_clicks],
[Symbol("data-"), Symbol("aria-")];
id=10,
n_clicks=1
)

@test get_name(test_comp) == "html_div"
@test get_type(test_comp) == "Div"
@test get_namespace(test_comp) == "dash_html_components"
Expand All @@ -24,11 +24,11 @@ using JSON3
@test_throws ErrorException test_comp.key
@test_throws ErrorException test_comp.key = 20
@test_throws ErrorException Component("html_div", "Div", "dash_html_components",
[:children, :id, :n_clicks],
[Symbol("data-"), Symbol("aria-")];
id = 10,
key = 1
)
[:children, :id, :n_clicks],
[Symbol("data-"), Symbol("aria-")];
id=10,
key=1
)
json = JSON3.write(test_comp)
res = JSON3.read(json)
@test all(keys(res) .== [:type, :namespace, :props])
Expand All @@ -40,11 +40,11 @@ end

@testset "empty wilds" begin
test_comp = Component("html_div", "Div", "dash_html_components",
[:children, :id, :n_clicks],
Symbol[];
id = 10,
n_clicks = 1
)
[:children, :id, :n_clicks],
Symbol[];
id=10,
n_clicks=1
)

@test get_name(test_comp) == "html_div"
@test get_type(test_comp) == "Div"
Expand All @@ -54,4 +54,20 @@ end

@test_throws ErrorException test_comp.var"data-id" = 20
@test_throws ErrorException test_comp.aaaaa
end
end
function test_component(; kwargs...)
Component("html_div", "Div", "dash_html_components", [:dir, :n_clicks, :key, :loading_state, :contentEditable, :contextMenu, :n_clicks_timestamp, :draggable, :accessKey, :hidden, :style, :children, :id, :title, :role, :lang, :spellCheck, :tabIndex, :disable_n_clicks, :className], Symbol[]; kwargs...)
end
@testset "Index by id" begin
layout = test_component(children=[
test_component(id="my-id", n_clicks=0, title="text"),
test_component(id="my-div", children=[
test_component(children=[]),
"string",
test_component(id="target")
]),
test_component(id="my-div2")
])
@test layout["target"].id == "target"
@test layout["ups"] === nothing
end