Skip to content

Commit

Permalink
Fix but rendering component instance that has nested variants
Browse files Browse the repository at this point in the history
Fixes #2012. When resolving a variant by matching property and value names, don't give up as soon as a property doesn't match. This property could be used for a nested variant. As long as at least one property matches, keep looping through properties, then find a view with the resulting set of matched properties/values. If no properties match at all, then return null.
  • Loading branch information
rylin8 committed Jan 23, 2025
1 parent 6233782 commit 67e0a52
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ class VariantPropertyMap {
possibleValues.contains("*") -> resolvedNameProperties.add("$propertyName=*")
else -> return null
}
} else {
return null
}
}

// If no properties matched at all, there is no variant that matches so return null
if (resolvedNameProperties.isEmpty()) return null

var view = getViewFromPropertyList(resolvedNameProperties, variantViewMap)
if (view != null) return view

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ val EXAMPLES: ArrayList<Triple<String, @Composable () -> Unit, String?>> =
{ VariantPropertiesTest() },
VariantPropertiesTestDoc.javaClass.name,
),
Triple("Variant Nesting", { VariantNesting() }, VariantNestingDoc.javaClass.name),
Triple("Variable Borders", { VariableBorderTest() }, VariableBorderTestDoc.javaClass.name),
Triple("Variable Modes", { VariableModesTest() }, VariablesTestDoc.javaClass.name),
Triple(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.android.designcompose.testapp.validation.examples

import androidx.compose.runtime.Composable
import com.android.designcompose.ComponentReplacementContext
import com.android.designcompose.annotation.Design
import com.android.designcompose.annotation.DesignComponent
import com.android.designcompose.annotation.DesignDoc
import com.android.designcompose.annotation.DesignVariant

enum class BoxType {
Blue,
Red,
}

enum class CircleType {
Green,
Purple,
}

@DesignDoc(id = "lpUWHv5gZh6WOx56pRnJ2s")
interface VariantNesting {
@DesignComponent(node = "#stage")
fun Main(@Design(node = "#replace") replace: @Composable (ComponentReplacementContext) -> Unit)

@DesignComponent(node = "#Box")
fun Box(
@DesignVariant(property = "#BoxType") boxType: BoxType,
@DesignVariant(property = "#CircleType") circleType: CircleType,
)
}

@Composable
fun VariantNesting() {
VariantNestingDoc.Main(
replace = { VariantNestingDoc.Box(boxType = BoxType.Blue, circleType = CircleType.Purple) }
)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 67e0a52

Please sign in to comment.