@fnet/form-ranking 0.0.0-stage → 0.1.1
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/README.md +30 -2
- package/dist/default/cjs/index.cjs +272 -0
- package/dist/default/esm/index.js +263 -0
- package/package.json +33 -4
package/README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fnet/form-ranking
|
|
2
|
+
|
|
3
|
+
The Flownet form for a question that is answered by putting things in order.
|
|
4
|
+
|
|
5
|
+
```yaml
|
|
6
|
+
- matters:
|
|
7
|
+
form: npm:@fnet/form-ranking
|
|
8
|
+
props:
|
|
9
|
+
title: What matters most to you?
|
|
10
|
+
description: Pick them in order.
|
|
11
|
+
input:
|
|
12
|
+
options: [Price, Quality, Speed, Support]
|
|
13
|
+
required: true
|
|
14
|
+
result: order # ['Speed', 'Price', 'Support', 'Quality']
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
- The person picks the options one after another: the first is number 1, the next number 2.
|
|
18
|
+
What is ranked stands at the top in its order; picking it again takes its number away.
|
|
19
|
+
- A ranked option moves a place up or down with the two buttons beside it, or with Alt and
|
|
20
|
+
an arrow key.
|
|
21
|
+
- `max: 3` asks for a top three of a longer list; `min` for at least so many.
|
|
22
|
+
- The answer is a list of values, the first one first, and the result of the step.
|
|
23
|
+
|
|
24
|
+
The reference:
|
|
25
|
+
[.justai/expert/reference/form-ranking.md](https://github.com/fnetai/form-ranking/blob/main/.justai/expert/reference/form-ranking.md).
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
2
31
|
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var Layout = require('@fnet/react-layout-asya');
|
|
7
|
+
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
|
|
10
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
11
|
+
var Layout__default = /*#__PURE__*/_interopDefault(Layout);
|
|
12
|
+
|
|
13
|
+
function _extends() {
|
|
14
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
15
|
+
for (var e = 1; e < arguments.length; e++) {
|
|
16
|
+
var t = arguments[e];
|
|
17
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
18
|
+
}
|
|
19
|
+
return n;
|
|
20
|
+
}, _extends.apply(null, arguments);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A question that is answered by putting things in order: which matters most, which next.
|
|
25
|
+
*
|
|
26
|
+
* It is the frame (@fnet/react-layout-asya) with a list in it. The person picks the options
|
|
27
|
+
* one after another: the first one picked is number 1, the next number 2. What is ranked
|
|
28
|
+
* stands at the top, in its order; picking it again takes its number away. A ranked option
|
|
29
|
+
* can be moved a place up or down with the two buttons beside it.
|
|
30
|
+
*
|
|
31
|
+
* What is about the list is under `input`:
|
|
32
|
+
*
|
|
33
|
+
* options the list. Each is a text, or { value, label, description }
|
|
34
|
+
* required true: every option has to be ranked (with max: that many)
|
|
35
|
+
* min the least number that has to be ranked
|
|
36
|
+
* max the most that can be ranked: "your top three"
|
|
37
|
+
* value the order to start with: a list of values
|
|
38
|
+
* labels the words the form says itself: up, down, ranked, unranked
|
|
39
|
+
* messages the words to say: required, min
|
|
40
|
+
* hint the words beside the button, or false for none
|
|
41
|
+
*
|
|
42
|
+
* The answer is a list: the values of the ranked options, the first one first.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
const LABELS = {
|
|
46
|
+
up: 'Move {label} up',
|
|
47
|
+
down: 'Move {label} down',
|
|
48
|
+
ranked: '{label} is number {n}.',
|
|
49
|
+
unranked: '{label} is not ranked.'
|
|
50
|
+
};
|
|
51
|
+
const MESSAGES = {
|
|
52
|
+
required: 'Please put all of them in order.',
|
|
53
|
+
min: 'Please rank at least {n}.'
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// An option as the form uses it, from a text or an object.
|
|
57
|
+
const optionOf = option => option !== null && typeof option === 'object' ? {
|
|
58
|
+
value: Object.hasOwn(option, 'value') ? option.value : option.label,
|
|
59
|
+
label: option.label ?? String(option.value),
|
|
60
|
+
description: option.description
|
|
61
|
+
} : {
|
|
62
|
+
value: option,
|
|
63
|
+
label: String(option)
|
|
64
|
+
};
|
|
65
|
+
const same = a => b => Object.is(a, b);
|
|
66
|
+
|
|
67
|
+
// Read by a screen reader, not seen.
|
|
68
|
+
const UNSEEN = {
|
|
69
|
+
position: 'absolute',
|
|
70
|
+
inlineSize: '1px',
|
|
71
|
+
blockSize: '1px',
|
|
72
|
+
overflow: 'hidden',
|
|
73
|
+
clipPath: 'inset(50%)',
|
|
74
|
+
whiteSpace: 'nowrap'
|
|
75
|
+
};
|
|
76
|
+
var index = props => {
|
|
77
|
+
const input = props.input || {};
|
|
78
|
+
const labels = {
|
|
79
|
+
...LABELS,
|
|
80
|
+
...input.labels
|
|
81
|
+
};
|
|
82
|
+
const say = (name, n) => (input.messages?.[name] || MESSAGES[name]).replace('{n}', n);
|
|
83
|
+
const word = (name, option, n) => labels[name].replace('{label}', option.label).replace('{n}', n);
|
|
84
|
+
const options = React__default.default.useMemo(() => (Array.isArray(input.options) ? input.options : []).map(optionOf), [input.options]);
|
|
85
|
+
const optionWith = value => options.find(option => Object.is(option.value, value));
|
|
86
|
+
|
|
87
|
+
// How many can be ranked, and how many have to be.
|
|
88
|
+
const most = input.max !== undefined ? Math.min(Number(input.max), options.length) : options.length;
|
|
89
|
+
const least = Math.max(input.required === true ? most : 0, input.min !== undefined ? Math.min(Number(input.min), most) : 0);
|
|
90
|
+
|
|
91
|
+
// What is ranked: the values, the first one first.
|
|
92
|
+
const [ranked, setRanked] = React__default.default.useState(() => {
|
|
93
|
+
const start = Array.isArray(input.value) ? input.value : [];
|
|
94
|
+
const known = [];
|
|
95
|
+
for (const value of start) if (optionWith(value) && !known.some(same(value))) known.push(value);
|
|
96
|
+
return known.slice(0, most);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// The order as of the last change, also before the screen was drawn again.
|
|
100
|
+
const latest = React__default.default.useRef(ranked);
|
|
101
|
+
latest.current = ranked;
|
|
102
|
+
const [told, setTold] = React__default.default.useState('');
|
|
103
|
+
|
|
104
|
+
// The row to give the keyboard to once the list is drawn again: a row that moved keeps it.
|
|
105
|
+
const group = React__default.default.useRef(null);
|
|
106
|
+
const follow = React__default.default.useRef(null);
|
|
107
|
+
React__default.default.useEffect(() => {
|
|
108
|
+
if (!follow.current) return;
|
|
109
|
+
const {
|
|
110
|
+
value,
|
|
111
|
+
part
|
|
112
|
+
} = follow.current;
|
|
113
|
+
follow.current = null;
|
|
114
|
+
const at = rows.findIndex(option => Object.is(option.value, value));
|
|
115
|
+
const row = group.current?.querySelectorAll('[data-row]')[at];
|
|
116
|
+
(row?.querySelector(`[data-part="${part}"]:not(:disabled)`) || row?.querySelector('[data-option]'))?.focus({
|
|
117
|
+
preventScroll: true
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
const change = (next, option, part = 'option') => {
|
|
121
|
+
latest.current = next;
|
|
122
|
+
follow.current = {
|
|
123
|
+
value: option.value,
|
|
124
|
+
part
|
|
125
|
+
};
|
|
126
|
+
setRanked(next);
|
|
127
|
+
const n = next.findIndex(same(option.value)) + 1;
|
|
128
|
+
setTold(n > 0 ? word('ranked', option, n) : word('unranked', option));
|
|
129
|
+
input.onChange?.({
|
|
130
|
+
value: next
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// Pick an option: it gets the next number, or gives its number back.
|
|
135
|
+
const pick = option => {
|
|
136
|
+
const now = latest.current;
|
|
137
|
+
if (now.some(same(option.value))) return change(now.filter(value => !Object.is(value, option.value)), option);
|
|
138
|
+
if (now.length >= most) return undefined;
|
|
139
|
+
return change([...now, option.value], option);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// Move a ranked option a place up (-1) or down (+1).
|
|
143
|
+
const move = (option, step, part) => {
|
|
144
|
+
const now = latest.current;
|
|
145
|
+
const at = now.findIndex(same(option.value));
|
|
146
|
+
const to = at + step;
|
|
147
|
+
if (at === -1 || to < 0 || to >= now.length) return;
|
|
148
|
+
const next = now.slice();
|
|
149
|
+
[next[at], next[to]] = [next[to], next[at]];
|
|
150
|
+
change(next, option, part);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// The rows as they are shown: what is ranked first, in its order, then the rest.
|
|
154
|
+
const rows = [...ranked.map(optionWith), ...options.filter(option => !ranked.some(same(option.value)))];
|
|
155
|
+
const full = ranked.length >= most;
|
|
156
|
+
const send = value => {
|
|
157
|
+
if (typeof props.actionAnswer === 'function') props.actionAnswer(value);else props.actionClick?.();
|
|
158
|
+
};
|
|
159
|
+
const validate = value => {
|
|
160
|
+
if (value.length >= least) return true;
|
|
161
|
+
// "All of them" is said only when all of them are asked for.
|
|
162
|
+
return least === options.length ? say('required') : say('min', least);
|
|
163
|
+
};
|
|
164
|
+
const onKeyDown = event => {
|
|
165
|
+
const all = [...(group.current?.querySelectorAll('[data-option]') || [])];
|
|
166
|
+
const at = all.indexOf(event.target);
|
|
167
|
+
if (at === -1) return;
|
|
168
|
+
const option = rows[at];
|
|
169
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
170
|
+
event.preventDefault();
|
|
171
|
+
const step = event.key === 'ArrowDown' ? 1 : -1;
|
|
172
|
+
// With Alt the row moves; without, the keyboard moves to the next row.
|
|
173
|
+
if (event.altKey) move(option, step, 'option');else all[(at + step + all.length) % all.length]?.focus();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (event.key === ' ') {
|
|
177
|
+
event.preventDefault();
|
|
178
|
+
pick(option);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Enter on an option that is not ranked ranks it; on a ranked one it sends.
|
|
183
|
+
if (event.key === 'Enter' && !ranked.some(same(option.value)) && !full) {
|
|
184
|
+
event.preventDefault();
|
|
185
|
+
pick(option);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
return /*#__PURE__*/React__default.default.createElement(Layout__default.default, _extends({}, props, {
|
|
189
|
+
// A list is always something to send; whether it may go is what validate says.
|
|
190
|
+
answer: {
|
|
191
|
+
value: ranked,
|
|
192
|
+
validate,
|
|
193
|
+
hint: input.hint
|
|
194
|
+
},
|
|
195
|
+
actionAnswer: send
|
|
196
|
+
}), /*#__PURE__*/React__default.default.createElement("div", {
|
|
197
|
+
ref: group,
|
|
198
|
+
className: "asya-options",
|
|
199
|
+
role: "group",
|
|
200
|
+
"aria-label": typeof props.title === 'string' ? props.title : undefined,
|
|
201
|
+
onKeyDown: onKeyDown
|
|
202
|
+
}, rows.map((option, index) => {
|
|
203
|
+
const n = ranked.findIndex(same(option.value)) + 1;
|
|
204
|
+
const closed = n === 0 && full;
|
|
205
|
+
return /*#__PURE__*/React__default.default.createElement("div", {
|
|
206
|
+
key: String(option.value),
|
|
207
|
+
"data-row": "",
|
|
208
|
+
style: {
|
|
209
|
+
display: 'flex',
|
|
210
|
+
gap: '0.5rem'
|
|
211
|
+
}
|
|
212
|
+
}, /*#__PURE__*/React__default.default.createElement("div", {
|
|
213
|
+
"data-option": "",
|
|
214
|
+
"data-part": "option",
|
|
215
|
+
className: "asya-option",
|
|
216
|
+
role: "checkbox",
|
|
217
|
+
"aria-checked": n > 0,
|
|
218
|
+
"aria-disabled": closed || undefined,
|
|
219
|
+
"aria-label": n > 0 ? `${n}. ${option.label}` : option.label,
|
|
220
|
+
tabIndex: index === 0 ? 0 : -1,
|
|
221
|
+
onClick: () => pick(option),
|
|
222
|
+
style: {
|
|
223
|
+
flex: 1,
|
|
224
|
+
minInlineSize: 0,
|
|
225
|
+
...(closed ? {
|
|
226
|
+
opacity: 0.4,
|
|
227
|
+
cursor: 'default'
|
|
228
|
+
} : {})
|
|
229
|
+
}
|
|
230
|
+
}, /*#__PURE__*/React__default.default.createElement("span", {
|
|
231
|
+
className: "asya-key",
|
|
232
|
+
"aria-hidden": "true"
|
|
233
|
+
}, n > 0 ? n : ' '), /*#__PURE__*/React__default.default.createElement("span", null, option.label, option.description && /*#__PURE__*/React__default.default.createElement("span", {
|
|
234
|
+
className: "asya-label",
|
|
235
|
+
style: {
|
|
236
|
+
margin: '0.125rem 0 0',
|
|
237
|
+
fontWeight: 400
|
|
238
|
+
}
|
|
239
|
+
}, option.description))), n > 0 && ranked.length > 1 && /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, /*#__PURE__*/React__default.default.createElement("button", {
|
|
240
|
+
type: "button",
|
|
241
|
+
"data-part": "up",
|
|
242
|
+
className: "asya-option",
|
|
243
|
+
disabled: n === 1,
|
|
244
|
+
"aria-label": word('up', option),
|
|
245
|
+
onClick: () => move(option, -1, 'up'),
|
|
246
|
+
style: {
|
|
247
|
+
inlineSize: '3rem',
|
|
248
|
+
flex: 'none',
|
|
249
|
+
justifyContent: 'center',
|
|
250
|
+
padding: 0
|
|
251
|
+
}
|
|
252
|
+
}, "\u2191"), /*#__PURE__*/React__default.default.createElement("button", {
|
|
253
|
+
type: "button",
|
|
254
|
+
"data-part": "down",
|
|
255
|
+
className: "asya-option",
|
|
256
|
+
disabled: n === ranked.length,
|
|
257
|
+
"aria-label": word('down', option),
|
|
258
|
+
onClick: () => move(option, 1, 'down'),
|
|
259
|
+
style: {
|
|
260
|
+
inlineSize: '3rem',
|
|
261
|
+
flex: 'none',
|
|
262
|
+
justifyContent: 'center',
|
|
263
|
+
padding: 0
|
|
264
|
+
}
|
|
265
|
+
}, "\u2193")));
|
|
266
|
+
})), /*#__PURE__*/React__default.default.createElement("span", {
|
|
267
|
+
"aria-live": "polite",
|
|
268
|
+
style: UNSEEN
|
|
269
|
+
}, told));
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
exports.default = index;
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import Layout from '@fnet/react-layout-asya';
|
|
3
|
+
|
|
4
|
+
function _extends() {
|
|
5
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
6
|
+
for (var e = 1; e < arguments.length; e++) {
|
|
7
|
+
var t = arguments[e];
|
|
8
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
9
|
+
}
|
|
10
|
+
return n;
|
|
11
|
+
}, _extends.apply(null, arguments);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A question that is answered by putting things in order: which matters most, which next.
|
|
16
|
+
*
|
|
17
|
+
* It is the frame (@fnet/react-layout-asya) with a list in it. The person picks the options
|
|
18
|
+
* one after another: the first one picked is number 1, the next number 2. What is ranked
|
|
19
|
+
* stands at the top, in its order; picking it again takes its number away. A ranked option
|
|
20
|
+
* can be moved a place up or down with the two buttons beside it.
|
|
21
|
+
*
|
|
22
|
+
* What is about the list is under `input`:
|
|
23
|
+
*
|
|
24
|
+
* options the list. Each is a text, or { value, label, description }
|
|
25
|
+
* required true: every option has to be ranked (with max: that many)
|
|
26
|
+
* min the least number that has to be ranked
|
|
27
|
+
* max the most that can be ranked: "your top three"
|
|
28
|
+
* value the order to start with: a list of values
|
|
29
|
+
* labels the words the form says itself: up, down, ranked, unranked
|
|
30
|
+
* messages the words to say: required, min
|
|
31
|
+
* hint the words beside the button, or false for none
|
|
32
|
+
*
|
|
33
|
+
* The answer is a list: the values of the ranked options, the first one first.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
const LABELS = {
|
|
37
|
+
up: 'Move {label} up',
|
|
38
|
+
down: 'Move {label} down',
|
|
39
|
+
ranked: '{label} is number {n}.',
|
|
40
|
+
unranked: '{label} is not ranked.'
|
|
41
|
+
};
|
|
42
|
+
const MESSAGES = {
|
|
43
|
+
required: 'Please put all of them in order.',
|
|
44
|
+
min: 'Please rank at least {n}.'
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// An option as the form uses it, from a text or an object.
|
|
48
|
+
const optionOf = option => option !== null && typeof option === 'object' ? {
|
|
49
|
+
value: Object.hasOwn(option, 'value') ? option.value : option.label,
|
|
50
|
+
label: option.label ?? String(option.value),
|
|
51
|
+
description: option.description
|
|
52
|
+
} : {
|
|
53
|
+
value: option,
|
|
54
|
+
label: String(option)
|
|
55
|
+
};
|
|
56
|
+
const same = a => b => Object.is(a, b);
|
|
57
|
+
|
|
58
|
+
// Read by a screen reader, not seen.
|
|
59
|
+
const UNSEEN = {
|
|
60
|
+
position: 'absolute',
|
|
61
|
+
inlineSize: '1px',
|
|
62
|
+
blockSize: '1px',
|
|
63
|
+
overflow: 'hidden',
|
|
64
|
+
clipPath: 'inset(50%)',
|
|
65
|
+
whiteSpace: 'nowrap'
|
|
66
|
+
};
|
|
67
|
+
var index = props => {
|
|
68
|
+
const input = props.input || {};
|
|
69
|
+
const labels = {
|
|
70
|
+
...LABELS,
|
|
71
|
+
...input.labels
|
|
72
|
+
};
|
|
73
|
+
const say = (name, n) => (input.messages?.[name] || MESSAGES[name]).replace('{n}', n);
|
|
74
|
+
const word = (name, option, n) => labels[name].replace('{label}', option.label).replace('{n}', n);
|
|
75
|
+
const options = React.useMemo(() => (Array.isArray(input.options) ? input.options : []).map(optionOf), [input.options]);
|
|
76
|
+
const optionWith = value => options.find(option => Object.is(option.value, value));
|
|
77
|
+
|
|
78
|
+
// How many can be ranked, and how many have to be.
|
|
79
|
+
const most = input.max !== undefined ? Math.min(Number(input.max), options.length) : options.length;
|
|
80
|
+
const least = Math.max(input.required === true ? most : 0, input.min !== undefined ? Math.min(Number(input.min), most) : 0);
|
|
81
|
+
|
|
82
|
+
// What is ranked: the values, the first one first.
|
|
83
|
+
const [ranked, setRanked] = React.useState(() => {
|
|
84
|
+
const start = Array.isArray(input.value) ? input.value : [];
|
|
85
|
+
const known = [];
|
|
86
|
+
for (const value of start) if (optionWith(value) && !known.some(same(value))) known.push(value);
|
|
87
|
+
return known.slice(0, most);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// The order as of the last change, also before the screen was drawn again.
|
|
91
|
+
const latest = React.useRef(ranked);
|
|
92
|
+
latest.current = ranked;
|
|
93
|
+
const [told, setTold] = React.useState('');
|
|
94
|
+
|
|
95
|
+
// The row to give the keyboard to once the list is drawn again: a row that moved keeps it.
|
|
96
|
+
const group = React.useRef(null);
|
|
97
|
+
const follow = React.useRef(null);
|
|
98
|
+
React.useEffect(() => {
|
|
99
|
+
if (!follow.current) return;
|
|
100
|
+
const {
|
|
101
|
+
value,
|
|
102
|
+
part
|
|
103
|
+
} = follow.current;
|
|
104
|
+
follow.current = null;
|
|
105
|
+
const at = rows.findIndex(option => Object.is(option.value, value));
|
|
106
|
+
const row = group.current?.querySelectorAll('[data-row]')[at];
|
|
107
|
+
(row?.querySelector(`[data-part="${part}"]:not(:disabled)`) || row?.querySelector('[data-option]'))?.focus({
|
|
108
|
+
preventScroll: true
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
const change = (next, option, part = 'option') => {
|
|
112
|
+
latest.current = next;
|
|
113
|
+
follow.current = {
|
|
114
|
+
value: option.value,
|
|
115
|
+
part
|
|
116
|
+
};
|
|
117
|
+
setRanked(next);
|
|
118
|
+
const n = next.findIndex(same(option.value)) + 1;
|
|
119
|
+
setTold(n > 0 ? word('ranked', option, n) : word('unranked', option));
|
|
120
|
+
input.onChange?.({
|
|
121
|
+
value: next
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// Pick an option: it gets the next number, or gives its number back.
|
|
126
|
+
const pick = option => {
|
|
127
|
+
const now = latest.current;
|
|
128
|
+
if (now.some(same(option.value))) return change(now.filter(value => !Object.is(value, option.value)), option);
|
|
129
|
+
if (now.length >= most) return undefined;
|
|
130
|
+
return change([...now, option.value], option);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Move a ranked option a place up (-1) or down (+1).
|
|
134
|
+
const move = (option, step, part) => {
|
|
135
|
+
const now = latest.current;
|
|
136
|
+
const at = now.findIndex(same(option.value));
|
|
137
|
+
const to = at + step;
|
|
138
|
+
if (at === -1 || to < 0 || to >= now.length) return;
|
|
139
|
+
const next = now.slice();
|
|
140
|
+
[next[at], next[to]] = [next[to], next[at]];
|
|
141
|
+
change(next, option, part);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// The rows as they are shown: what is ranked first, in its order, then the rest.
|
|
145
|
+
const rows = [...ranked.map(optionWith), ...options.filter(option => !ranked.some(same(option.value)))];
|
|
146
|
+
const full = ranked.length >= most;
|
|
147
|
+
const send = value => {
|
|
148
|
+
if (typeof props.actionAnswer === 'function') props.actionAnswer(value);else props.actionClick?.();
|
|
149
|
+
};
|
|
150
|
+
const validate = value => {
|
|
151
|
+
if (value.length >= least) return true;
|
|
152
|
+
// "All of them" is said only when all of them are asked for.
|
|
153
|
+
return least === options.length ? say('required') : say('min', least);
|
|
154
|
+
};
|
|
155
|
+
const onKeyDown = event => {
|
|
156
|
+
const all = [...(group.current?.querySelectorAll('[data-option]') || [])];
|
|
157
|
+
const at = all.indexOf(event.target);
|
|
158
|
+
if (at === -1) return;
|
|
159
|
+
const option = rows[at];
|
|
160
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
161
|
+
event.preventDefault();
|
|
162
|
+
const step = event.key === 'ArrowDown' ? 1 : -1;
|
|
163
|
+
// With Alt the row moves; without, the keyboard moves to the next row.
|
|
164
|
+
if (event.altKey) move(option, step, 'option');else all[(at + step + all.length) % all.length]?.focus();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (event.key === ' ') {
|
|
168
|
+
event.preventDefault();
|
|
169
|
+
pick(option);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Enter on an option that is not ranked ranks it; on a ranked one it sends.
|
|
174
|
+
if (event.key === 'Enter' && !ranked.some(same(option.value)) && !full) {
|
|
175
|
+
event.preventDefault();
|
|
176
|
+
pick(option);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
return /*#__PURE__*/React.createElement(Layout, _extends({}, props, {
|
|
180
|
+
// A list is always something to send; whether it may go is what validate says.
|
|
181
|
+
answer: {
|
|
182
|
+
value: ranked,
|
|
183
|
+
validate,
|
|
184
|
+
hint: input.hint
|
|
185
|
+
},
|
|
186
|
+
actionAnswer: send
|
|
187
|
+
}), /*#__PURE__*/React.createElement("div", {
|
|
188
|
+
ref: group,
|
|
189
|
+
className: "asya-options",
|
|
190
|
+
role: "group",
|
|
191
|
+
"aria-label": typeof props.title === 'string' ? props.title : undefined,
|
|
192
|
+
onKeyDown: onKeyDown
|
|
193
|
+
}, rows.map((option, index) => {
|
|
194
|
+
const n = ranked.findIndex(same(option.value)) + 1;
|
|
195
|
+
const closed = n === 0 && full;
|
|
196
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
197
|
+
key: String(option.value),
|
|
198
|
+
"data-row": "",
|
|
199
|
+
style: {
|
|
200
|
+
display: 'flex',
|
|
201
|
+
gap: '0.5rem'
|
|
202
|
+
}
|
|
203
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
204
|
+
"data-option": "",
|
|
205
|
+
"data-part": "option",
|
|
206
|
+
className: "asya-option",
|
|
207
|
+
role: "checkbox",
|
|
208
|
+
"aria-checked": n > 0,
|
|
209
|
+
"aria-disabled": closed || undefined,
|
|
210
|
+
"aria-label": n > 0 ? `${n}. ${option.label}` : option.label,
|
|
211
|
+
tabIndex: index === 0 ? 0 : -1,
|
|
212
|
+
onClick: () => pick(option),
|
|
213
|
+
style: {
|
|
214
|
+
flex: 1,
|
|
215
|
+
minInlineSize: 0,
|
|
216
|
+
...(closed ? {
|
|
217
|
+
opacity: 0.4,
|
|
218
|
+
cursor: 'default'
|
|
219
|
+
} : {})
|
|
220
|
+
}
|
|
221
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
222
|
+
className: "asya-key",
|
|
223
|
+
"aria-hidden": "true"
|
|
224
|
+
}, n > 0 ? n : ' '), /*#__PURE__*/React.createElement("span", null, option.label, option.description && /*#__PURE__*/React.createElement("span", {
|
|
225
|
+
className: "asya-label",
|
|
226
|
+
style: {
|
|
227
|
+
margin: '0.125rem 0 0',
|
|
228
|
+
fontWeight: 400
|
|
229
|
+
}
|
|
230
|
+
}, option.description))), n > 0 && ranked.length > 1 && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("button", {
|
|
231
|
+
type: "button",
|
|
232
|
+
"data-part": "up",
|
|
233
|
+
className: "asya-option",
|
|
234
|
+
disabled: n === 1,
|
|
235
|
+
"aria-label": word('up', option),
|
|
236
|
+
onClick: () => move(option, -1, 'up'),
|
|
237
|
+
style: {
|
|
238
|
+
inlineSize: '3rem',
|
|
239
|
+
flex: 'none',
|
|
240
|
+
justifyContent: 'center',
|
|
241
|
+
padding: 0
|
|
242
|
+
}
|
|
243
|
+
}, "\u2191"), /*#__PURE__*/React.createElement("button", {
|
|
244
|
+
type: "button",
|
|
245
|
+
"data-part": "down",
|
|
246
|
+
className: "asya-option",
|
|
247
|
+
disabled: n === ranked.length,
|
|
248
|
+
"aria-label": word('down', option),
|
|
249
|
+
onClick: () => move(option, 1, 'down'),
|
|
250
|
+
style: {
|
|
251
|
+
inlineSize: '3rem',
|
|
252
|
+
flex: 'none',
|
|
253
|
+
justifyContent: 'center',
|
|
254
|
+
padding: 0
|
|
255
|
+
}
|
|
256
|
+
}, "\u2193")));
|
|
257
|
+
})), /*#__PURE__*/React.createElement("span", {
|
|
258
|
+
"aria-live": "polite",
|
|
259
|
+
style: UNSEEN
|
|
260
|
+
}, told));
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
export { index as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
"name": "@fnet/form-ranking",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist/*",
|
|
6
|
+
"fnet/input.yaml",
|
|
7
|
+
"fnet/output.yaml"
|
|
8
|
+
],
|
|
9
|
+
"main": "dist/default/esm/index.js",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/default/esm/index.js",
|
|
14
|
+
"require": "./dist/default/cjs/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"flownet": {
|
|
18
|
+
"type": "node"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/fnetai/form-ranking.git"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@fnet/react-layout-asya": "^0.2.5",
|
|
27
|
+
"react": "^18",
|
|
28
|
+
"react-dom": "^18",
|
|
29
|
+
"@emotion/react": "^11",
|
|
30
|
+
"@emotion/styled": "^11"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"serve": "npx serve ."
|
|
34
|
+
}
|
|
6
35
|
}
|