Scoop API

scoop:scoop.ui.component:0.1.0

src/questionnaire/options.scoop

Selected production source · src/questionnaire/options.scoop

1package scoop.ui.component
3import scoop.graphics.text.FontCollection
4import scoop.ui.*
5import scoop.ui.style.*
7public enum QuestionnairePart {
8 Item, Title, Description, Choice, Choices, Input, Error, Progress, Actions
9}
11public class QuestionnaireStrings public constructor(
12 public val previous: String = "Previous",
13 public val next: String = "Next",
14 public val skip: String = "Skip",
15 public val submit: String = "Submit",
16 public val busy: String = "Submitting…",
17 public val optional: String = "Optional",
18 public val skipped: String = "Skipped",
19 public val answerRequired: String = "Answer this question to continue.",
20 public val textRequired: String = "Enter an answer to continue.",
21 public val progress: (Long, Long) -> String =
22 { position, total -> f"Question ${position} of ${total}" }
23) {
24 public fun issue(value: QuestionnaireIssueKind): String =
25 when (value) {
26 case AnswerRequired -> answerRequired;
27 case TextRequired -> textRequired;
28 case Message(val text) -> text
29 }
32public class QuestionnaireProps public constructor(
33 public val state: QuestionnaireState,
34 public val onSubmit: (QuestionnaireResult) -> Unit,
35 public val enabled: Boolean = true,
36 public val busy: Boolean = false,
37 public val continueEnabled: Boolean = true,
38 public val autoFocus: Boolean = false,
39 public val shortcuts: Boolean = true,
40 public val strings: QuestionnaireStrings = QuestionnaireStrings(),
41 public val onAnswerChange: (QuestionKey) -> Unit = { _ -> Unit },
42 public val part: (QuestionnairePart, View) -> View = { _, content -> content },
43 public val inputFocus: FocusOptions = FocusOptions(),
44 public val choiceFocus: (QuestionKey, QuestionChoiceKey?) -> FocusOptions =
45 { _, _ -> FocusOptions() },
46 public val focusOutline: OutlineStyle? = None,
47 public val fonts: FontCollection? = None,
48 public val theme: ThemeSource? = None,
49 public val style: StyleOverride = styleOverride()
52public class QuestionnaireBuilder internal constructor() {
53 public var enabled: Boolean = true
54 public var busy: Boolean = false
55 public var continueEnabled: Boolean = true
56 public var autoFocus: Boolean = false
57 public var shortcuts: Boolean = true
58 public var strings: QuestionnaireStrings = QuestionnaireStrings()
59 public var onAnswerChange: (QuestionKey) -> Unit = { _ -> Unit }
60 public var part: (QuestionnairePart, View) -> View = { _, content -> content }
61 public var inputFocus: FocusOptions = FocusOptions()
62 public var choiceFocus: (QuestionKey, QuestionChoiceKey?) -> FocusOptions =
63 { _, _ -> FocusOptions() }
64 public var focusOutline: OutlineStyle? = None
65 public var fonts: FontCollection? = None
66 public var theme: ThemeSource? = None
67 public var style: StyleOverride = styleOverride()
68 public var key: ViewKey = ViewKey.Position
69 public fun style(configure: StyleOverrideBuilder.() -> Unit) {
70 style = style.copy(configure)
71 }
72 internal fun snapshot(
73 state: QuestionnaireState,
74 submit: (QuestionnaireResult) -> Unit
75 ): QuestionnaireProps =
76 QuestionnaireProps(
77 state,
78 submit,
79 enabled,
80 busy,
81 continueEnabled,
82 autoFocus,
83 shortcuts,
84 strings,
85 onAnswerChange,
86 part,
87 inputFocus,
88 choiceFocus,
89 focusOutline,
90 fonts,
91 theme,
92 style
93 )
96public fun questionnaire(
97 state: QuestionnaireState,
98 onSubmit: (QuestionnaireResult) -> Unit,
99 configure: QuestionnaireBuilder.() -> Unit = {}
100): View {
101 val builder = QuestionnaireBuilder()
102 builder.configure()
103 return component(
104 builder.snapshot(state, onSubmit),
105 { current, scope -> Questionnaire(current, scope) },
106 builder.key
107 )
110internal fun questionShortcut(index: Long): String {
111 val labels =
112 [
113 "1",
114 "2",
115 "3",
116 "4",
117 "5",
118 "6",
119 "7",
120 "8",
121 "9",
122 "a",
123 "b",
124 "c",
125 "d",
126 "e",
127 "f",
128 "g",
129 "h",
130 "i",
131 "j",
132 "k",
133 "l",
134 "m",
135 "n",
136 "o",
137 "p",
138 "q",
139 "r",
140 "s",
141 "t",
142 "u",
143 "v",
144 "w",
145 "x",
146 "y",
147 "z"
148 ]
149 return if (index >= 0L && index < labels.size)
150 labels[index]
151 else
152 ""