Skip to content

Commit

Permalink
improve checkboxes form
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBTurchyn committed Jul 30, 2023
1 parent 1f29e62 commit 5e4f207
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/javascript/elements/turbo_modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default actionable(class extends HTMLElement {
document.body.classList.remove('overflow-hidden')

document.removeEventListener('keyup', this.onEscKey)
document.removeEventListener('turbo:submit-end', this.handleSubmit)
document.removeEventListener('turbo:submit-end', this.onSubmit)
document.removeEventListener('turbo:before-cache', this.close)
}

Expand Down
7 changes: 6 additions & 1 deletion app/javascript/submission_form/area.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</span>
</div>
<div
v-if="isActive"
v-if="isActive && withLabel"
class="absolute -top-7 rounded bg-base-content text-base-100 px-2 text-sm whitespace-nowrap"
>
{{ field.name || fieldNames[field.type] }}
Expand Down Expand Up @@ -146,6 +146,11 @@ export default {
required: false,
default: false
},
withLabel: {
type: Boolean,
required: false,
default: true
},
fieldIndex: {
type: Number,
required: false,
Expand Down
6 changes: 6 additions & 0 deletions app/javascript/submission_form/areas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
:submittable="true"
:field-index="fieldIndex"
:is-active="currentStep === step"
:with-label="withLabel"
:is-value-set="step.some((f) => f.uuid in values)"
:attachments-index="attachmentsIndex"
@click="$emit('focus-step', step)"
Expand Down Expand Up @@ -56,6 +57,11 @@ export default {
required: false,
default: () => ({})
},
withLabel: {
type: Boolean,
required: false,
default: true
},
currentStep: {
type: Array,
required: false,
Expand Down
57 changes: 33 additions & 24 deletions app/javascript/submission_form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:steps="stepFields"
:values="values"
:attachments-index="attachmentsIndex"
:with-label="!isAnonymousChecboxes"
:current-step="currentStepFields"
@focus-step="[saveStep(), goToStep($event, false, true), currentField.type !== 'checkbox' ? isFormVisible = true : '']"
/>
Expand Down Expand Up @@ -182,7 +183,7 @@
/>
<div
v-else-if="currentField.type === 'checkbox'"
class="flex w-full"
class="flex w-full max-h-44 overflow-y-auto"
>
<input
type="hidden"
Expand All @@ -192,31 +193,36 @@
<div
class="space-y-3.5 mx-auto"
>
<div
v-for="(field, index) in currentStepFields"
:key="field.uuid"
>
<label
:for="field.uuid"
class="flex items-center space-x-3"
<template v-if="isAnonymousChecboxes">
Complete hightlighted checkboxes and click <span class="font-semibold">{{ stepFields.length === currentStep + 1 ? 'submit' : 'next' }}</span>.
</template>
<template v-else>
<div
v-for="(field, index) in currentStepFields"
:key="field.uuid"
>
<input
type="hidden"
:name="`values[${field.uuid}]`"
:value="!!values[field.uuid]"
>
<input
:id="field.uuid"
type="checkbox"
class="base-checkbox !h-7 !w-7"
:checked="!!values[field.uuid]"
@click="[$refs.areas.scrollIntoField(field), values[field.uuid] = !values[field.uuid]]"
<label
:for="field.uuid"
class="flex items-center space-x-3"
>
<span class="text-xl">
{{ field.name || field.type + ' ' + (index + 1) }}
</span>
</label>
</div>
<input
type="hidden"
:name="`values[${field.uuid}]`"
:value="!!values[field.uuid]"
>
<input
:id="field.uuid"
type="checkbox"
class="base-checkbox !h-7 !w-7"
:checked="!!values[field.uuid]"
@click="[$refs.areas.scrollIntoField(field), values[field.uuid] = !values[field.uuid]]"
>
<span class="text-xl">
{{ field.name || field.type + ' ' + (index + 1) }}
</span>
</label>
</div>
</template>
</div>
</div>
<ImageStep
Expand Down Expand Up @@ -374,6 +380,9 @@ export default {
currentStepFields () {
return this.stepFields[this.currentStep]
},
isAnonymousChecboxes () {
return this.currentField.type === 'checkbox' && this.currentStepFields.every((e) => !e.name) && this.currentStepFields.length > 4
},
isButtonDisabled () {
return this.isSubmitting ||
(this.currentField.required && ['image', 'file'].includes(this.currentField.type) && !this.values[this.currentField.uuid]?.length) ||
Expand Down

0 comments on commit 5e4f207

Please sign in to comment.