Scoop API

scoop:scoop.ui.component:0.1.0

src/field/component.scoop

Selected production source · src/field/component.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.style.*
7internal enum FieldPart {
8 Control, Heading, Description, Error
9}
11internal class FieldLayoutProps(
12 val input: FieldProps,
13 val density: Double,
14 val parts: Array<FieldPart>
17public class Field public constructor(
18 private val props: Props<FieldProps>,
19 scope: ComponentScope
20): 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.value
27 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 1L
38 )
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.fonts
51 ),
52 target,
53 FieldAnnotationKind.Description
54 ),
55 2L
56 )
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 = target
73 ),
74 { current, mount -> FieldError(current, scope = mount) }
75 ),
76 3L
77 )
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.style
93 )
94 }
97internal fun fieldSemanticTarget(input: FieldProps): FieldTarget? =
98 when (val target = input.target) {
99 case Some(_) -> target
100 case None -> when (input.heading) {
101 case Some(FieldHeading.Label(_, val target)) -> Some(target)
102 else -> None
103 }
104 }
106private fun fieldHeading(
107 input: FieldProps,
108 theme: Theme,
109 target: FieldTarget?,
110 requiredSemantics: Boolean
111): View {
112 val base =
113 fieldTextStyle(FieldTextRole.Title, theme).copy {
114 if (input.invalid)
115 this.color = theme.tokens.palette.destructive.background
116 }
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.Label
126 )
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.background
158 },
159 input.fonts
160 ),
161 1L
162 )
163 )
164 this.alignment = CrossAlignment.Baseline
165 this.spacing = 8.0 * theme.tokens.typography.body.fontSize / 14.0
166 }