入力
bind: $name は TextBox、CheckBox、Select、Slider を状態変数に結び付けます。人が入力を変更すると、変数が変わりウィンドウが再描画されます。on_change では、$event.value が新しい値を保持します:
<KScript.Window title="Settings">
<let name="name" value="''" />
<let name="dark" value="false" />
<let name="size" value="14" />
<let name="theme" value="'teal'" />
<Column gap="8">
<TextBox bind="$name" placeholder="Your name" />
<CheckBox bind="$dark">Dark mode</CheckBox>
<Slider bind="$size" min="10" max="24" step="1" />
<Select bind="$theme" options="['teal', 'orange', 'purple']" />
</Column>
</KScript.Window>import KScript.UI
Window(title: 'Settings') {
$name = ''
$dark = false
$size = 14
$theme = 'teal'
Column(gap: 8) {
TextBox(bind: $name, placeholder: 'Your name')
CheckBox(bind: $dark) "Dark mode"
Slider(bind: $size, min: 10, max: 24, step: 1)
Select(bind: $theme, options: ['teal', 'orange', 'purple'])
}
}Select の options は、リスト、{ label, value } マップのリスト、またはラベルから値へのマップとして指定します。
TextBox は、ページ上の @bind と同じバインディングオプションを受け付けます。on: は input(入力するたびに更新、既定値)、change、または blur(フォーカスが外れたとき、または Enter を押したとき)です。trim は前後の空白を除去し、as:(string、int、number、bool)は値をその型として読み取り、型に合わない値にはエラーを表示します:
<KScript.Window title="Age">
<let name="age" value="0" />
<TextBox bind="$age" on="blur" trim="yes" as="int" />
<Text>Next year you'll be {$age + 1}</Text>
</KScript.Window>import KScript.UI
Window(title: 'Age') {
$age = 0
TextBox(bind: $age, on: blur, trim, as: int)
Text "Next year you'll be {$age + 1}"
}multiline: trueでTextBoxを複数行にし、Enterキーを押すとon_enterが実行されます。