Skip to content

Beautiful Open-Source Form Builder

Add shuffle options feature to select fields (#XXX) (#976)

Chirag Chhatralaf1d4acd6b35f
diff --git a/client/components/open/forms/BlockRenderer.vue b/client/components/open/forms/BlockRenderer.vue
index f1a1d3671..a584d9550 100644
--- a/client/components/open/forms/BlockRenderer.vue
+++ b/client/components/open/forms/BlockRenderer.vue
@@ -99,6 +99,7 @@
import ClientOnlyWrapper from '~/components/global/ClientOnlyWrapper.vue'
import { useComponentRegistry } from '~/composables/components/useComponentRegistry'
import TextBlock from '~/components/forms/core/TextBlock.vue'
+import { shuffleArray } from '~/lib/utils.js'
const props = defineProps({
block: { type: Object, required: false, default: null },
@@ -201,6 +202,14 @@ const roundedClass = computed(() => {
return map[radius] || 'rounded-lg'
})
+// Map select options once at component creation (shuffle if enabled)
+const selectOptions = (() => {
+ const field = props.block
+ if (!field || !['select', 'multi_select'].includes(field.type)) return null
+ const options = field[field.type]?.options?.map(option => ({ name: option.name, value: option.name })) ?? []
+ return field.shuffle_options && options.length > 1 ? shuffleArray(options) : options
+})()
+
const boundProps = computed(() => {
const field = props.block
if (!field) return {}
@@ -233,9 +242,7 @@ const boundProps = computed(() => {
if (field.type === 'barcode') inputProperties.decoders = field.decoders
if (['select', 'multi_select'].includes(field.type)) {
- inputProperties.options = (field[field.type])
- ? field[field.type].options.map(option => ({ name: option.name, value: option.name }))
- : []
+ inputProperties.options = selectOptions ?? []
inputProperties.multiple = (field.type === 'multi_select')
inputProperties.allowCreation = (field.allow_creation === true)
inputProperties.searchable = (inputProperties.options.length > 4)
diff --git a/client/components/open/forms/fields/components/FieldOptions.vue b/client/components/open/forms/fields/components/FieldOptions.vue
index df9bb45be..cf5583399 100644
--- a/client/components/open/forms/fields/components/FieldOptions.vue
+++ b/client/components/open/forms/fields/components/FieldOptions.vue
@@ -354,6 +354,11 @@
label="Use radio buttons"
@update:model-value="onFieldWithoutDropdownChange"
/>
+ <toggle-switch-input
+ :form="field"
+ name="shuffle_options"
+ label="Randomize options order"
+ />
<!-- Min/Max Selection Constraints for multi_select only -->
<template v-if="field.type === 'multi_select'">
diff --git a/client/lib/utils.js b/client/lib/utils.js
index 89a7fd4c1..7a699dd7c 100644
--- a/client/lib/utils.js
+++ b/client/lib/utils.js
@@ -149,3 +149,17 @@ export function cleanQuotes(str) {
if (!str) return ''
return str.replace(/^"|"$/g, '')
}
+
+/**
+ * Shuffles an array
+ * @param {Array} array - The array to shuffle
+ * @returns {Array} - A new shuffled array
+ */
+export const shuffleArray = (array) => {
+ const shuffled = [...array]
+ for (let i = shuffled.length - 1; i > 0; i--) {
+ const j = Math.floor(Math.random() * (i + 1));
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]
+ }
+ return shuffled
+}