Implement radio flag type

This commit is contained in:
nemunaire 2021-11-13 00:58:03 +01:00
parent 41565729fd
commit 49664c3dfe
2 changed files with 64 additions and 44 deletions

View file

@ -145,7 +145,7 @@ func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaul
}) })
f = &fl f = &fl
if len(flag.Choice) > 0 || flag.Type == "ucq" { if len(flag.Choice) > 0 || (flag.Type == "ucq" || flag.Type == "radio") {
// Import choices // Import choices
hasOne := false hasOne := false
@ -241,10 +241,12 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
flag.Type = "vector" flag.Type = "vector"
case "ucq": case "ucq":
flag.Type = "ucq" flag.Type = "ucq"
case "radio":
flag.Type = "radio"
case "mcq": case "mcq":
flag.Type = "mcq" flag.Type = "mcq"
default: default:
errs = append(errs, fmt.Sprintf("%q: flag #%d: invalid type of flag: should be 'key', 'number', 'text', 'mcq', 'ucq' or 'vector'.", path.Base(exercice.Path), nline+1)) errs = append(errs, fmt.Sprintf("%q: flag #%d: invalid type of flag: should be 'key', 'number', 'text', 'mcq', 'ucq', 'radio' or 'vector'.", path.Base(exercice.Path), nline+1))
return return
} }
@ -258,7 +260,7 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
} }
} }
if flag.Type == "key" || strings.HasPrefix(flag.Type, "number") || flag.Type == "text" || flag.Type == "ucq" || flag.Type == "vector" { if flag.Type == "key" || strings.HasPrefix(flag.Type, "number") || flag.Type == "text" || flag.Type == "ucq" || flag.Type == "radio" || flag.Type == "vector" {
addedFlag, choices, berrs := buildKeyFlag(exercice, flag, nline+1, "Flag") addedFlag, choices, berrs := buildKeyFlag(exercice, flag, nline+1, "Flag")
if len(berrs) > 0 { if len(berrs) > 0 {
errs = append(errs, berrs...) errs = append(errs, berrs...)

View file

@ -101,8 +101,8 @@
{/if} {/if}
{#if !flag.found} {#if !flag.found}
{#each values as v, index} {#each values as v, index}
<div class="input-group" class:mt-1={index != 0}>
{#if !flag.choices} {#if !flag.choices}
<div class="input-group" class:mt-1={index != 0}>
{#if flag.type == 'number'} {#if flag.type == 'number'}
<input <input
type="number" type="number"
@ -136,16 +136,8 @@
title="{flag.placeholder}" title="{flag.placeholder}"
></textarea> ></textarea>
{/if} {/if}
{:else} {#if flag.unit}
<select <span class="input-group-text">{flag.unit}</span>
class="form-select"
id="sol_{flag.type}{flag.id}_{index}"
bind:value={values[index]}
>
{#each Object.keys(flag.choices) as l}
<option value={l}>{flag.choices[l]}</option>
{/each}
</select>
{/if} {/if}
{#if flag.choices_cost > 0} {#if flag.choices_cost > 0}
<Button <Button
@ -171,10 +163,36 @@
<Icon name="plus" /> <Icon name="plus" />
</Button> </Button>
{/if} {/if}
{#if flag.unit}
<span class="input-group-text">{flag.unit}</span>
{/if}
</div> </div>
{:else if flag.type == 'radio'}
{#each Object.keys(flag.choices) as l, i}
<div class="form-check">
<input
id="sol_{flag.type}{flag.id}_{index}_{i}"
type="radio"
value={l}
bind:group={values[index]}
class="form-check-input"
>
<label
class="form-check-label"
for="sol_{flag.type}{flag.id}_{index}_{i}"
>
{flag.choices[l]}
</label>
</div>
{/each}
{:else}
<select
class="form-select"
id="sol_{flag.type}{flag.id}_{index}"
bind:value={values[index]}
>
{#each Object.keys(flag.choices) as l}
<option value={l}>{flag.choices[l]}</option>
{/each}
</select>
{/if}
{/each} {/each}
{#if flag.help} {#if flag.help}
<small class="form-text text-muted">{flag.help}</small> <small class="form-text text-muted">{flag.help}</small>