scoop:scoop.ui.component:0.1.0
src/field/component.scoop
Selected production source · src/field/component.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*7
internal enum FieldPart {8
Control, Heading, Description, Error9
}11
internal class FieldLayoutProps(12
val input: FieldProps,13
val density: Double,14
val parts: Array<FieldPart>15
)17
public class Field public constructor(18
private val props: Props<FieldProps>,19
scope: ComponentScope20
): Component(scope) {21
private val themes = ThemeAccess(scope)22
public override fun onMount() {23
themes.mount(props.value.theme)24
}25
public override fun render(): View {26
val input = props.value27
val theme = themes.read(input.theme)28
val target = fieldSemanticTarget(input)29
val children = ArrayList<View>()30
val parts = ArrayList<FieldPart>()31
children.add(panelSlot(input.control, 0L))32
parts.add(FieldPart.Control)33
if (panelPresent(input.heading) || input.required) {34
children.add(35
panelSlot(36
fieldHeading(input, theme, target, target?.hasSemantics(scope) ?: false),37
1L38
)39
)40
parts.add(FieldPart.Heading)41
}42
when (input.description) {43
case Some(val value) -> {44
children.add(45
panelSlot(46
fieldAnnotated(47
panelCaption(48
value,49
fieldTextStyle(FieldTextRole.Description, theme),50
input.fonts51
),52
target,53
FieldAnnotationKind.Description54
),55
2L56
)57
)58
parts.add(FieldPart.Description)59
}60
case None -> {}61
}62
val errors = fieldErrors(input.errors)63
if (panelPresent(input.error) || errors.size > 0L) {64
children.add(65
panelSlot(66
component(67
FieldErrorProps(68
errors,69
input.error,70
input.fonts,71
input.theme,72
target = target73
),74
{ current, mount -> FieldError(current, scope = mount) }75
),76
3L77
)78
)79
parts.add(FieldPart.Error)80
}81
return panelFrame(82
element(83
FieldLayoutProps(84
input,85
theme.tokens.typography.body.fontSize / 14.0,86
parts.toArray()87
),88
{ current, mount -> FieldLayout(current, scope = mount) },89
children.toArray()90
),91
input.padding,92
input.style93
)94
}95
}97
internal fun fieldSemanticTarget(input: FieldProps): FieldTarget? =98
when (val target = input.target) {99
case Some(_) -> target100
case None -> when (input.heading) {101
case Some(FieldHeading.Label(_, val target)) -> Some(target)102
else -> None103
}104
}106
private fun fieldHeading(107
input: FieldProps,108
theme: Theme,109
target: FieldTarget?,110
requiredSemantics: Boolean111
): View {112
val base =113
fieldTextStyle(FieldTextRole.Title, theme).copy {114
if (input.invalid)115
this.color = theme.tokens.palette.destructive.background116
}117
return makeRow {118
when (input.heading) {119
case Some(val heading) -> {120
val view =121
when (heading) {122
case Title(val content) -> fieldAnnotated(123
panelCaption(content, base, input.fonts),124
target,125
FieldAnnotationKind.Label126
)127
case Label(val content, val target) -> component(128
FieldLabelProps(129
content,130
target,131
input.fonts,132
input.theme,133
Some(134
spanStyle {135
this.color = Some(base.color);136
this.lineHeight = Some(Some(20.0 / 14.0))137
}138
)139
),140
{ current, mount -> FieldLabel(current, scope = mount) }141
)142
}143
this.add(panelSlot(view, 0L))144
}145
case None -> {}146
}147
if (input.required)148
this.add(149
panelSlot(150
panelCaption(151
fieldPlain(152
input.requiredText,153
ComponentSemantics(ignored = requiredSemantics)154
),155
fieldTextStyle(FieldTextRole.Description, theme).copy {156
if (input.invalid)157
this.color = theme.tokens.palette.destructive.background158
},159
input.fonts160
),161
1L162
)163
)164
this.alignment = CrossAlignment.Baseline165
this.spacing = 8.0 * theme.tokens.typography.body.fontSize / 14.0166
}167
}