@@ -21,6 +21,7 @@ const props = withDefaults(defineProps<FieldProps>(), {
2121
2222const {
2323 value,
24+ setValue,
2425 label,
2526 hint,
2627 errorMessage,
@@ -44,16 +45,42 @@ const options = computed(() => props.schema.oneOf || props.schema.anyOf || []);
4445// Selected option index
4546const selectedIndex = ref (0 );
4647
47- // Try to determine initial selection based on data
48+ // Try to determine initial selection based on data and initialize defaults
4849onMounted (() => {
49- if (value .value ) {
50+ let initialIndex = 0 ;
51+
52+ if (value .value && typeof value .value === ' object' && Object .keys (value .value ).length > 0 ) {
53+ // Find matching schema based on existing data
5054 const index = options .value .findIndex ((optionSchema ) => {
5155 const result = schemaUtils .validate (optionSchema , value .value );
5256 return result .valid ;
5357 });
5458
5559 if (index !== - 1 ) {
56- selectedIndex .value = index ;
60+ initialIndex = index ;
61+ }
62+ }
63+
64+ selectedIndex .value = initialIndex ;
65+
66+ // Initialize default values for the selected schema
67+ // This ensures fields exist even if the form starts empty
68+ const initialSchema = options .value [initialIndex ];
69+ if (initialSchema && initialSchema .properties ) {
70+ const currentValue = (value .value && typeof value .value === ' object' ) ? value .value : {};
71+ const defaults = schemaUtils .getDefaultValue (initialSchema );
72+
73+ // Only proceed if defaults is a valid object
74+ if (defaults && typeof defaults === ' object' ) {
75+ // Merge defaults with current value
76+ const merged = { ... defaults , ... currentValue };
77+
78+ // Only update if we're adding new fields (don't overwrite existing)
79+ const hasNewFields = Object .keys (defaults ).some (key => ! (key in currentValue ));
80+ if (hasNewFields && setValue ) {
81+ // Pass false to skip validation during initialization
82+ setValue (merged , false );
83+ }
5784 }
5885 }
5986});
@@ -177,7 +204,25 @@ const filterFn = (val: string, update: (fn: () => void) => void) => {
177204// Handle manual switch
178205const handleOptionChange = (newIndex : number ) => {
179206 selectedIndex .value = newIndex ;
180- // Keep existing value to allow common fields to persist
207+
208+ // Initialize default values for the new schema's fields
209+ // This ensures fields exist even if user never touches them (e.g., empty string for 'value')
210+ const newSchema = options .value [newIndex ];
211+ if (newSchema && newSchema .properties ) {
212+ const currentValue = (value .value && typeof value .value === ' object' ) ? value .value : {};
213+ const defaults = schemaUtils .getDefaultValue (newSchema );
214+
215+ // Only proceed if defaults is a valid object
216+ if (defaults && typeof defaults === ' object' ) {
217+ // Merge defaults with current value, preferring current values for common fields
218+ const merged = { ... defaults , ... currentValue };
219+
220+ // Update form value - skip validation during option switch
221+ if (setValue ) {
222+ setValue (merged , false );
223+ }
224+ }
225+ }
181226};
182227
183228 </script >
0 commit comments