PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / frontend / src / views / public / fields / CheckboxFormField.vue

CheckboxFormField.vue in The Innovative Form Builder – IvyForms 1.0, at frontend/src/views/public/fields/CheckboxFormField.vue

133 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <template>
2 <div
3 :class="[
4 'ivyforms-field',
5 'ivyforms-field__checkbox',
6 'ivyforms-field__checkbox_' + field.id,
7 field.cssClasses,
8 ]"
9 >
10 <IvyFormItem
11 :label="field.hideLabel ? '' : field.label"
12 :required="field.required"
13 :error="error"
14 :prop="field.type + '_' + field.fieldIndex"
15 :show-info="!!field.description"
16 :info-text="String(field.description || '')"
17 :show-info-icon="false"
18 :label-position="field.labelPosition === 'default' ? 'top' : field.labelPosition || 'top'"
19 >
20 <IvyCheckboxGroup
21 :id="fieldID"
22 v-model="localModelValue"
23 :disabled="isDisabled"
24 class="ivyforms-field__checkbox-group ivyforms-flex ivyforms-align-items-start ivyforms-justify-content-start ivyforms-flex-direction-column ivyforms-gap-8"
25 role="group"
26 :aria-readonly="field.readOnly ? 'true' : undefined"
27 >
28 <IvyCheckbox
29 v-for="option in checkboxOptions"
30 :id="String(option.id)"
31 :key="option.id || option.value"
32 :value="option.value"
33 :aria-label="field.label"
34 priority="secondary"
35 type="checkmark"
36 class="ivyforms-field__checkbox-option ivyforms-flex ivyforms-align-items-center ivyforms-gap-8 ivyforms-font-16 ivyforms-font-weight-500"
37 >
38 {{ option.label }}
39 </IvyCheckbox>
40 </IvyCheckboxGroup>
41 </IvyFormItem>
42 </div>
43 </template>
44
45 <script setup lang="ts">
46 import { computed, onMounted, watch, ref, inject } from 'vue'
47 import type { Field } from '@/types/field'
48 import IvyCheckbox from '@/views/_components/checkbox/IvyCheckbox.vue'
49 import IvyCheckboxGroup from '@/views/_components/checkbox/IvyCheckboxGroup.vue'
50 import IvyFormItem from '@/views/_components/form/IvyFormItem.vue'
51
52 const props = defineProps<{
53 modelValue: string[] | number[]
54 field: Field
55 disabled?: boolean
56 error?: string
57 }>()
58
59 const emit = defineEmits(['update:modelValue'])
60 const fieldID = `field_${props.field.fieldIndex}`
61 const isDisabled = computed(() => !!props.disabled || !!props.field.readOnly)
62 const clearFieldError = inject<(fieldKey: string) => void>('clearFieldError', () => {})
63 const fieldKey = computed(() => props.field.type + '_' + props.field.fieldIndex)
64 const localModelValue = computed<string[]>({
65 get() {
66 return Array.isArray(props.modelValue) ? (props.modelValue as string[]) : []
67 },
68 set(val: string[]) {
69 if (props.field.readOnly) return
70 emit('update:modelValue', val)
71 clearFieldError(fieldKey.value)
72 },
73 })
74
75 const checkboxOptions = computed(() => {
76 let opts = props.field.fieldOptions || []
77 if (props.field.shuffleOptions) {
78 opts = [...opts]
79 for (let i = opts.length - 1; i > 0; i--) {
80 const j = Math.floor(Math.random() * (i + 1))
81 ;[opts[i], opts[j]] = [opts[j], opts[i]]
82 }
83 }
84 return opts.map((opt) => ({
85 label: opt.label,
86 value: opt.value,
87 id: opt.id,
88 isDefault: opt.isDefault,
89 }))
90 })
91
92 const hasSetDefaults = ref(false)
93
94 onMounted(() => {
95 setDefaultsIfNeeded()
96 })
97
98 watch(
99 () => props.modelValue,
100 (newVal) => {
101 if (!hasSetDefaults.value && (!Array.isArray(newVal) || newVal.length === 0)) {
102 setDefaultsIfNeeded()
103 }
104 },
105 { immediate: true },
106 )
107
108 function setDefaultsIfNeeded() {
109 if (hasSetDefaults.value) return
110 const optionDefaults = (props.field.fieldOptions || [])
111 .filter((option) => option.isDefault)
112 .map((option) => String(option.value))
113 const fieldLevelDefaults =
114 typeof props.field.defaultValue === 'string' && props.field.defaultValue.trim().length
115 ? props.field.defaultValue
116 .split(',')
117 .map((val) => val.trim())
118 .filter((val) => val.length)
119 : []
120 const defaultsSet = new Set<string>([...optionDefaults, ...fieldLevelDefaults])
121 if (defaultsSet.size) {
122 emit('update:modelValue', Array.from(defaultsSet))
123 hasSetDefaults.value = true
124 }
125 }
126 </script>
127
128 <style scoped lang="scss">
129 .ivyforms-field__checkbox-option {
130 color: var(--map-base-text-0);
131 }
132 </style>
133