Skip to content

Commit 942d633

Browse files
authored
Merge pull request #203 from BeastyBlacksmith/bbs/index-by-id
add index by id
2 parents fece921 + e185414 commit 942d633

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

.github/workflows/label.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- uses: actions/labeler@v2
10+
- uses: actions/labeler@v4
1111
with:
1212
repo-token: "${{ secrets.GITHUB_TOKEN }}"

src/app/dashapp.jl

+22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ mutable struct DashApp
4040

4141
end
4242

43+
const VecChildTypes = Union{NTuple{N, DashBase.Component} where {N}, Vector{<:DashBase.Component}}
44+
45+
function Base.getindex(component::DashBase.Component, id::AbstractString)
46+
component.id == id && return component
47+
hasproperty(component, :children) || return nothing
48+
cc = component.children
49+
return if cc isa Union{VecChildTypes, DashBase.Component}
50+
cc[id]
51+
elseif cc isa AbstractVector
52+
identity.(filter(x->hasproperty(x, :id), cc))[id]
53+
else
54+
nothing
55+
end
56+
end
57+
function Base.getindex(children::VecChildTypes, id::AbstractString)
58+
for element in children
59+
element.id == id && return element
60+
el = element[id]
61+
el !== nothing && return el
62+
end
63+
end
64+
4365
#only name, index_string and layout are available to set
4466
function Base.setproperty!(app::DashApp, property::Symbol, value)
4567
property == :index_string && return set_index_string!(app, value)

test/core.jl

+15
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,18 @@ end
200200
end
201201
@test_throws ErrorException make_handler(app)
202202
end
203+
204+
@testset "Index by id" begin
205+
app = dash()
206+
app.layout = html_div() do
207+
dcc_input(id = "my-id", value="initial value", type = "text"),
208+
html_div(id = "my-div", children = [
209+
html_div(),
210+
"string",
211+
html_div(id = "target")
212+
]),
213+
html_div(id = "my-div2")
214+
end
215+
@test app.layout["target"].id == "target"
216+
@test app.layout["ups"] === nothing
217+
end

0 commit comments

Comments
 (0)