|
1 | 1 | <script setup lang="ts"> |
2 | | -import { computed } from 'vue'; |
| 2 | +import { computed, watch } from 'vue'; |
3 | 3 | import type { JSONSchema, UISchemaElement } from '@quickflo/quickforms'; |
4 | 4 | import { useFormContext } from '../composables/useFormContext.js'; |
5 | 5 |
|
@@ -143,6 +143,27 @@ const visibleWhenResult = computed(() => { |
143 | 143 | return evaluateCondition(condition, visibleWhenFieldValue.value); |
144 | 144 | }); |
145 | 145 |
|
| 146 | +// Check if field should clear its value when hidden |
| 147 | +// Defaults to true for x-visible-when fields, but can be overridden with x-clear-on-hide: false |
| 148 | +const shouldClearOnHide = computed(() => { |
| 149 | + const schema = props.schema as any; |
| 150 | + // If explicitly set, use that value |
| 151 | + if (schema['x-clear-on-hide'] !== undefined) { |
| 152 | + return schema['x-clear-on-hide'] === true; |
| 153 | + } |
| 154 | + // Default: clear on hide only if x-visible-when is configured |
| 155 | + return visibleWhenConfig.value !== undefined; |
| 156 | +}); |
| 157 | +
|
| 158 | +// Watch visibility changes and clear value when field becomes hidden |
| 159 | +watch(visibleWhenResult, (isNowVisible, wasVisible) => { |
| 160 | + // Only clear when transitioning from visible to hidden |
| 161 | + if (wasVisible === true && isNowVisible === false && shouldClearOnHide.value) { |
| 162 | + // Clear the field value |
| 163 | + context.setFieldValue(props.path, undefined); |
| 164 | + } |
| 165 | +}); |
| 166 | +
|
146 | 167 | // Get reactive field value for x-readonly-when condition |
147 | 168 | const readonlyWhenConfig = computed(() => { |
148 | 169 | const schema = props.schema as any; |
|
0 commit comments