scoop:scoop.ui.component:0.1.0
src/field/error.scoop
Selected production source · src/field/error.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*7
public 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()16
)18
internal fun fieldErrors(errors: Array<String>): Array<String> {19
val result = ArrayList<String>()20
for (error in errors) {21
if (error.byteLength == 0L)22
continue23
var duplicate = false24
for (existing in result)25
if (existing == error)26
duplicate = true27
if (!duplicate)28
result.add(error)29
}30
return result.toArray()31
}33
public class FieldError public constructor(34
private val props: Props<FieldErrorProps>,35
scope: ComponentScope36
): 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.value43
val theme = themes.read(input.theme)44
val base = fieldTextStyle(FieldTextRole.Error, theme)45
val text = input.textStyle?.resolve(base) ?: base46
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.semantics52
)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.059
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
else73
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 * density83
this.alignment = CrossAlignment.Baseline84
}85
)86
}87
this.spacing = 4.0 * density88
this.alignment = CrossAlignment.Stretch89
}90
return fieldAnnotated(91
panelFrame(content, visual = input.style),92
input.target,93
FieldAnnotationKind.Error,94
input.semantics95
)96
}97
}99
public class FieldErrorBuilder internal constructor(): FieldTextBuilder() {100
public var content: PanelContent? = None101
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)112
}114
public fun fieldError(errors: Array<String>, configure: FieldErrorBuilder.() -> Unit = {}): View =115
makeFieldError(errors, None, configure)117
public fun fieldError(text: String, configure: FieldErrorBuilder.() -> Unit = {}): View =118
makeFieldError([], Some(fieldPlain(text)), configure)120
public fun fieldError(text: StyledText, configure: FieldErrorBuilder.() -> Unit = {}): View =121
makeFieldError([], Some(fieldStyled(text)), configure)123
public fun fieldError(content: View, configure: FieldErrorBuilder.() -> Unit = {}): View =124
makeFieldError([], Some(PanelContent.Custom(content)), configure)126
internal fun makeFieldError(127
errors: Array<String>,128
content: PanelContent?,129
configure: FieldErrorBuilder.() -> Unit130
): View {131
val builder = FieldErrorBuilder()132
builder.content = content133
builder.configure()134
return component(135
builder.snapshot(errors),136
{ current, mount -> FieldError(current, scope = mount) },137
builder.key138
)139
}