pi-ask-user 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +1195 -1116
- package/package.json +1 -1
- package/single-select-layout.ts +21 -8
package/package.json
CHANGED
package/single-select-layout.ts
CHANGED
|
@@ -3,6 +3,11 @@ export interface QuestionOption {
|
|
|
3
3
|
description?: string;
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
+
export interface AnnotatedRow {
|
|
7
|
+
line: string;
|
|
8
|
+
selected: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
6
11
|
export interface RenderSingleSelectRowsParams {
|
|
7
12
|
options: QuestionOption[];
|
|
8
13
|
selectedIndex: number;
|
|
@@ -114,8 +119,13 @@ function buildItemBlocks(
|
|
|
114
119
|
});
|
|
115
120
|
}
|
|
116
121
|
|
|
117
|
-
function flatten(blocks: ItemBlock[]):
|
|
118
|
-
return blocks.flatMap((block) =>
|
|
122
|
+
function flatten(blocks: ItemBlock[], selectedIndex: number): AnnotatedRow[] {
|
|
123
|
+
return blocks.flatMap((block) =>
|
|
124
|
+
block.lines.map((line) => ({
|
|
125
|
+
line,
|
|
126
|
+
selected: block.itemIndex === selectedIndex,
|
|
127
|
+
})),
|
|
128
|
+
);
|
|
119
129
|
}
|
|
120
130
|
|
|
121
131
|
export function renderSingleSelectRows({
|
|
@@ -125,10 +135,10 @@ export function renderSingleSelectRows({
|
|
|
125
135
|
allowFreeform,
|
|
126
136
|
maxRows,
|
|
127
137
|
hideDescriptions,
|
|
128
|
-
}: RenderSingleSelectRowsParams):
|
|
138
|
+
}: RenderSingleSelectRowsParams): AnnotatedRow[] {
|
|
129
139
|
const itemCount = options.length + (allowFreeform ? 1 : 0);
|
|
130
140
|
const blocks = buildItemBlocks(options, width, allowFreeform, selectedIndex, hideDescriptions);
|
|
131
|
-
const allRows = flatten(blocks);
|
|
141
|
+
const allRows = flatten(blocks, selectedIndex);
|
|
132
142
|
|
|
133
143
|
if (!Number.isFinite(maxRows) || !maxRows || maxRows <= 0 || allRows.length <= maxRows) {
|
|
134
144
|
return allRows;
|
|
@@ -142,8 +152,11 @@ export function renderSingleSelectRows({
|
|
|
142
152
|
const availableRows = safeMaxRows > 1 ? safeMaxRows - 1 : 1;
|
|
143
153
|
|
|
144
154
|
if (selectedBlock.lines.length >= availableRows) {
|
|
145
|
-
const visible = selectedBlock.lines.slice(0, availableRows)
|
|
146
|
-
|
|
155
|
+
const visible = selectedBlock.lines.slice(0, availableRows).map((line) => ({
|
|
156
|
+
line,
|
|
157
|
+
selected: true,
|
|
158
|
+
}));
|
|
159
|
+
if (safeMaxRows > 1) visible.push({ line: indicator, selected: false });
|
|
147
160
|
return visible.slice(0, safeMaxRows);
|
|
148
161
|
}
|
|
149
162
|
|
|
@@ -169,7 +182,7 @@ export function renderSingleSelectRows({
|
|
|
169
182
|
break;
|
|
170
183
|
}
|
|
171
184
|
|
|
172
|
-
const visible = flatten(blocks.slice(start, end));
|
|
173
|
-
visible.push(indicator);
|
|
185
|
+
const visible = flatten(blocks.slice(start, end), selectedIndex);
|
|
186
|
+
visible.push({ line: indicator, selected: false });
|
|
174
187
|
return visible.slice(0, safeMaxRows);
|
|
175
188
|
}
|