diff --git a/.github/workflows/label.yml b/.github/workflows/label.yml index c01c356..251a28c 100644 --- a/.github/workflows/label.yml +++ b/.github/workflows/label.yml @@ -7,6 +7,6 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/labeler@v2 + - uses: actions/labeler@v4 with: repo-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/src/app/dashapp.jl b/src/app/dashapp.jl index 4e687a3..92fccce 100644 --- a/src/app/dashapp.jl +++ b/src/app/dashapp.jl @@ -40,6 +40,28 @@ mutable struct DashApp end +const VecChildTypes = Union{NTuple{N, DashBase.Component} where {N}, Vector{<:DashBase.Component}} + +function Base.getindex(component::DashBase.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 + identity.(filter(x->hasproperty(x, :id), cc))[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 + #only name, index_string and layout are available to set function Base.setproperty!(app::DashApp, property::Symbol, value) property == :index_string && return set_index_string!(app, value) diff --git a/test/core.jl b/test/core.jl index 25c942e..fd9f030 100644 --- a/test/core.jl +++ b/test/core.jl @@ -200,3 +200,18 @@ end end @test_throws ErrorException make_handler(app) end + +@testset "Index by id" begin + app = dash() + app.layout = html_div() do + dcc_input(id = "my-id", value="initial value", type = "text"), + html_div(id = "my-div", children = [ + html_div(), + "string", + html_div(id = "target") + ]), + html_div(id = "my-div2") + end + @test app.layout["target"].id == "target" + @test app.layout["ups"] === nothing +end