PluginProbe
The Innovative Form Builder – IvyForms / 0.7
The Innovative Form Builder – IvyForms v0.7
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 0.7, at frontend/src/views/public/fields/CheckboxFormField.vue

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