scoop:scoop.ui.component:0.1.0
src/date_picker/component.scoop
Selected production source · src/date_picker/component.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*8
import scoop.ui.component.column as dateColumn9
import scoop.ui.component.row as dateRow10
import scoop.ui.component.button as dateButton12
public class DatePicker public constructor(13
private val props: Props<DatePickerProps>,14
scope: ComponentScope15
): Component(scope) {16
private val control = DatePickerControl(props, scope)17
private val themes = ThemeAccess(scope)18
public override fun onMount() {19
themes.mount(props.value.theme);20
control.mount()21
}22
public override fun render(): View {23
val control = this.control24
val input = props.value25
val theme = themes.read(input.theme)26
val selected = calendarRead(input.selection)27
val open = control.open.value28
val popupSemantics =29
input.popupSemantics.copy {30
reference = Some(input.popupSemantics.reference ?: control.popupReference)31
}32
val semantics =33
input.semantics.copy {34
relations = semanticControls(relations, (popupSemantics.reference?.id ?: None))35
}36
val d = theme.tokens.typography.body.fontSize / 14.037
val focusVisual = InteractionVisual(focusVisible = true)38
val buttonRing =39
theme.surfaceMetrics(40
SurfaceVisual(input.buttonStyle.kind, focusVisual),41
actionOptions(42
theme,43
ActionShape.Button(ButtonSize.Icon),44
ButtonVariant.Outline,45
input.buttonStyle,46
focusVisual47
),48
scope.layoutDirection49
).focusOutset50
val inputRing =51
when (input.draft) {52
case Some(_) -> theme.surfaceMetrics(53
SurfaceVisual(SurfaceKind.Plain, focusVisual),54
input.inputStyle.options(focusVisual),55
scope.layoutDirection56
).focusOutset57
case None -> 0.058
}59
val triggerRing = lowerBound(buttonRing, inputRing)60
val calendarProps =61
CalendarProps(62
control.selection(),63
calendarFirst(selected) ?: (input.calendar.today ?: input.initialPage),64
input.calendar,65
enabled = input.mutable,66
handle = Some(control.calendar),67
fonts = input.fonts,68
theme = input.theme,69
day = input.day70
)71
val appearance = CalendarAppearance(calendarProps, theme, scope.layoutDirection)72
val columns =73
if (input.calendar.weekNumbers == CalendarWeekNumbers.None)74
7.075
else76
8.077
val width =78
(columns * appearance.cell + 16.0 * d) *79
input.calendar.monthCount.toDouble() +80
input.calendar.monthGap *81
d *82
(input.calendar.monthCount - 1L).toDouble() +83
20.0 *84
d85
val popupContent =86
dateColumn {87
if (input.presets.size > 0L)88
this.add(89
dateColumn {90
for (index in 0L ..< input.presets.size) {91
val preset = input.presets[index]92
this.add(93
dateButton(94
preset.label,95
{96
control.acceptValue(preset.value);97
Unit98
}99
) {100
this.variant = ButtonVariant.Ghost101
this.enabled =102
input.mutable &&103
calendarValueAllowed(104
preset.value,105
input.calendar.rules106
)107
this.fonts = input.fonts108
this.theme = input.theme109
this.key = ViewKey.Number(index)110
}111
)112
}113
this.spacing = 2.0 * d114
this.alignment = CrossAlignment.Stretch115
}116
)117
this.add(118
component(119
calendarProps,120
{ props, scope -> Calendar(props, scope) },121
ViewKey.Text("calendar")122
)123
)124
when (input.footer) {125
case Some(val content) -> this.add(content);126
case None -> {}127
}128
this.spacing = 8.0 * d129
this.alignment = CrossAlignment.Stretch130
}131
val popup =132
component(133
PopoverProps(134
control.open,135
popupContent,136
anchor = Some(control.anchor),137
width = width / d,138
alignment = OverlayAlignment.Start,139
mode = input.mode,140
focus =141
OverlayFocusOptions(autoFocus = false, restoreTarget = Some(control.focus)),142
handle = Some(control.layer),143
enabled = input.mutable,144
transition = input.transition,145
onCloseRequest = { reason -> control.closeRequest(reason) },146
onClosed = { reason -> control.closed(reason) },147
fonts = input.fonts,148
theme = input.theme,149
style = input.popupStyle,150
semantics = popupSemantics151
),152
{ props, scope -> Popover(props, scope) }153
)154
val trigger =155
when (input.draft) {156
case None -> {157
val text = control.text()158
popupSemanticButton(159
ButtonProps(160
ButtonContent.Resolved {161
style -> dateRow {162
this.add(dateCalendarIcon(style.iconSize, style.text.color))163
this.add(164
calendarLabel(165
if (text.byteLength == 0L)166
input.placeholder167
else168
text,169
style.text,170
input.fonts171
)172
)173
this.spacing = style.spacing174
this.alignment = CrossAlignment.Center175
}176
},177
{ control.toggle() },178
input.mutable,179
input.buttonStyle,180
input.theme,181
input.focus.copy { this.handle = Some(control.focus) },182
input.invalid,183
ButtonVariant.Outline,184
labelTarget = input.labelTarget,185
semantics = semantics186
),187
open,188
{ next -> control.expand(next) }189
)190
}191
case Some(val draft) -> dateColumn {192
this.add(193
dateRow {194
this.item(195
component(196
InputProps(197
draft.editor,198
input.placeholder,199
input.enabled,200
input.readOnly,201
input.invalid || draft.error != None,202
fonts = input.fonts,203
style = input.inputStyle,204
theme = input.theme,205
focus =206
input.focus.copy { this.handle = Some(control.focus) },207
onEdited = { _ -> control.edited() },208
onSubmit =209
{210
_ ->211
control.commit();212
Unit213
},214
labelTarget = input.labelTarget,215
semantics =216
semantics.copy {217
error =218
when (val supplied = input.semantics.error) {219
case Some(_) -> supplied220
case None -> draft.error221
}222
},223
expansion =224
Some(225
TextFieldExpansion(226
open,227
{ next -> control.expand(next) }228
)229
)230
),231
{ props, scope -> Input(props, scope) },232
ViewKey.Text("input")233
)234
) { this.grow = 1.0 }235
this.item(236
popupSemanticButton(237
ButtonProps(238
ButtonContent.Resolved {239
style -> dateCalendarIcon(240
style.iconSize,241
style.text.color242
)243
},244
{ control.toggle() },245
enabled = input.mutable,246
variant = ButtonVariant.Outline,247
size = ButtonSize.Icon,248
style = input.buttonStyle,249
theme = input.theme,250
semantics =251
input.calendarButtonSemantics.copy {252
relations =253
semanticControls(254
relations,255
(popupSemantics.reference?.id ?: None)256
)257
}258
),259
open,260
{ next -> control.expand(next) }261
)262
) { this.shrink = 0.0 }263
this.spacing = lowerBound(6.0 * d, buttonRing + inputRing)264
this.alignment = CrossAlignment.Center265
}266
)267
when (draft.error) {268
case Some(val error) -> this.add(269
calendarLabel(270
error,271
theme.tokens.typography.body.copy {272
this.color = theme.tokens.palette.destructive.background273
},274
input.fonts275
)276
)277
case None -> {}278
}279
this.spacing = lowerBound(6.0 * d, inputRing)280
this.alignment = CrossAlignment.Stretch281
}282
}283
return element(284
DatePickerRootInput(control, selected, open),285
{ props, scope -> DatePickerRoot(props, scope) },286
[panelFrame(trigger, Insets.all(triggerRing)), popup]287
)288
}289
}