Scoop API

scoop:scoop.ui.component:0.1.0

src/field/error.scoop

Selected production source · src/field/error.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.style.*
7public class FieldErrorProps public constructor(
8 public val errors: Array<String> = [],
9 public val content: PanelContent? = None,
10 public val fonts: FontCollection? = None,
11 public val theme: ThemeSource? = None,
12 public val textStyle: SpanStyle? = None,
13 public val style: StyleOverride = styleOverride(),
14 public val target: FieldTarget? = None,
15 public val semantics: ComponentSemantics = ComponentSemantics()
18internal fun fieldErrors(errors: Array<String>): Array<String> {
19 val result = ArrayList<String>()
20 for (error in errors) {
21 if (error.byteLength == 0L)
22 continue
23 var duplicate = false
24 for (existing in result)
25 if (existing == error)
26 duplicate = true
27 if (!duplicate)
28 result.add(error)
29 }
30 return result.toArray()
33public class FieldError public constructor(
34 private val props: Props<FieldErrorProps>,
35 scope: ComponentScope
36): Component(scope) {
37 private val themes = ThemeAccess(scope)
38 public override fun onMount() {
39 themes.mount(props.value.theme)
40 }
41 public override fun render(): View {
42 val input = props.value
43 val theme = themes.read(input.theme)
44 val base = fieldTextStyle(FieldTextRole.Error, theme)
45 val text = input.textStyle?.resolve(base) ?: base
46 when (input.content) {
47 case Some(val content) -> return fieldAnnotated(
48 panelFrame(panelCaption(content, text, input.fonts), visual = input.style),
49 input.target,
50 FieldAnnotationKind.Error,
51 input.semantics
52 )
53 case None -> {}
54 }
55 val errors = fieldErrors(input.errors)
56 if (errors.size == 0L)
57 return empty()
58 val density = theme.tokens.typography.body.fontSize / 14.0
59 val marker =
60 PanelContent.Label(
61 LabelProps(
62 LabelText.Plain("•", None),
63 semantics = ComponentSemantics(ignored = true)
64 )
65 )
66 val content =
67 makeColumn {
68 for (error in errors) {
69 val caption = panelCaption(fieldPlain(error), text, input.fonts)
70 if (errors.size == 1L)
71 this.add(caption)
72 else
73 this.add(
74 makeRow {
75 this.add(
76 panelFrame(
77 panelCaption(marker, text, input.fonts),
78 width = Some(8.0 * density)
79 )
80 )
81 this.item(caption) { this.grow = 1.0 }
82 this.spacing = 8.0 * density
83 this.alignment = CrossAlignment.Baseline
84 }
85 )
86 }
87 this.spacing = 4.0 * density
88 this.alignment = CrossAlignment.Stretch
89 }
90 return fieldAnnotated(
91 panelFrame(content, visual = input.style),
92 input.target,
93 FieldAnnotationKind.Error,
94 input.semantics
95 )
96 }
99public class FieldErrorBuilder internal constructor(): FieldTextBuilder() {
100 public var content: PanelContent? = None
101 public fun content(text: String) {
102 content = Some(fieldPlain(text))
103 }
104 public fun content(text: StyledText) {
105 content = Some(fieldStyled(text))
106 }
107 public fun content(view: View) {
108 content = Some(PanelContent.Custom(view))
109 }
110 internal fun snapshot(errors: Array<String>): FieldErrorProps =
111 FieldErrorProps(errors, content, fonts, theme, textStyle, style, target, semantics)
114public fun fieldError(errors: Array<String>, configure: FieldErrorBuilder.() -> Unit = {}): View =
115 makeFieldError(errors, None, configure)
117public fun fieldError(text: String, configure: FieldErrorBuilder.() -> Unit = {}): View =
118 makeFieldError([], Some(fieldPlain(text)), configure)
120public fun fieldError(text: StyledText, configure: FieldErrorBuilder.() -> Unit = {}): View =
121 makeFieldError([], Some(fieldStyled(text)), configure)
123public fun fieldError(content: View, configure: FieldErrorBuilder.() -> Unit = {}): View =
124 makeFieldError([], Some(PanelContent.Custom(content)), configure)
126internal fun makeFieldError(
127 errors: Array<String>,
128 content: PanelContent?,
129 configure: FieldErrorBuilder.() -> Unit
130): View {
131 val builder = FieldErrorBuilder()
132 builder.content = content
133 builder.configure()
134 return component(
135 builder.snapshot(errors),
136 { current, mount -> FieldError(current, scope = mount) },
137 builder.key
138 )