Skip to content

Commit 2bd7a0d

Browse files
committed
Add input_width option for string response and make size adapt
1 parent 5e14cd9 commit 2bd7a0d

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ String questions are specified by setting the "type" property to "string". These
237237

238238
You can optionally include a default answer pattern by specifying an object with a "type" property set to "default" and a "feedback" string. If no other answer patterns match the user's submission, the question will be marked incorrect and the provided feedback will be displayed.
239239

240+
You can also specify an `input_width` property (integer, approximate number of characters) on the question to control the width of the text input field (in em units) in the rendered quiz.
241+
240242
The schema for String Questions is shown below:
241243

242244
![Schema for String Questions](schema/string_schema.png)

jupyterquiz/js/string.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ function make_string(qa, outerqDiv, qDiv, aDiv, id) {
175175
inp.id = id + "-0";
176176
inp.className = "Input-text";
177177
inp.setAttribute('data-answers', JSON.stringify(qa.answers));
178+
// Apply optional input width (approx. number of characters, in em units)
179+
if (qa.input_width != null) {
180+
inp.style['min-width'] = qa.input_width + 'em';
181+
}
178182
aDiv.append(inp);
179183

180184
inp.onkeypress = string_keypress;

jupyterquiz/styles.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@
7575
display: block;
7676
margin: 10px;
7777
color: inherit;
78-
width: 140px;
78+
width: unset;
79+
min-width: 140px;
80+
max-width: 93%;
81+
field-sizing: content;
7982
background-color: var(--jq-numeric-input-bg);
8083
color: var(--jq-text-color);
8184
padding: 5px;

schema/string_schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
},
1919
"answers": {
2020
"type": "array",
21-
"items": { "$ref": "#/$defs/possible_answer" }
21+
"items": { "$ref": "#/$defs/possible_answer" }
22+
},
23+
"input_width": {
24+
"type": "integer",
25+
"minimum": 0,
26+
"description": "Approximate width of the input text field in number of characters (applied in em units)"
2227
}
2328
},
2429
"$defs": {

0 commit comments

Comments
 (0)