1- import React from 'react' ;
1+ import React , { useState , useEffect } from 'react' ;
22import PropTypes from 'prop-types' ;
33import { FormField , Textarea , Input } from '@cloudscape-design/components' ;
44import { formatValueForInput , parseInputValue } from '../utils/schemaHelpers' ;
55
66const MetadataFields = ( { attribute, onUpdate } ) => {
7+ // Local state for buffering user input without immediate parsing
8+ const [ examplesInput , setExamplesInput ] = useState ( '' ) ;
9+ const [ defaultValueInput , setDefaultValueInput ] = useState ( '' ) ;
10+
11+ // Initialize local state from attribute values
12+ useEffect ( ( ) => {
13+ if ( ! attribute . examples ) {
14+ setExamplesInput ( '' ) ;
15+ } else if ( Array . isArray ( attribute . examples ) ) {
16+ setExamplesInput ( attribute . examples . map ( ( ex ) => ( typeof ex === 'object' ? JSON . stringify ( ex ) : ex ) ) . join ( ', ' ) ) ;
17+ }
18+ } , [ attribute . examples ] ) ;
19+
20+ useEffect ( ( ) => {
21+ setDefaultValueInput ( formatValueForInput ( attribute . default ) ) ;
22+ } , [ attribute . default ] ) ;
23+
24+ // Handle Examples field blur - parse and update parent state
25+ const handleExamplesBlur = ( ) => {
26+ if ( ! examplesInput . trim ( ) ) {
27+ const updates = { ...attribute } ;
28+ delete updates . examples ;
29+ onUpdate ( updates ) ;
30+ return ;
31+ }
32+ try {
33+ const parsed = JSON . parse ( `[${ examplesInput } ]` ) ;
34+ onUpdate ( { examples : parsed } ) ;
35+ } catch {
36+ const examples = examplesInput
37+ . split ( ',' )
38+ . map ( ( v ) => v . trim ( ) )
39+ . filter ( ( v ) => v ) ;
40+ onUpdate ( { examples : examples . length > 0 ? examples : undefined } ) ;
41+ }
42+ } ;
43+
44+ // Handle Default Value field blur - parse and update parent state
45+ const handleDefaultValueBlur = ( ) => {
46+ if ( ! defaultValueInput ) {
47+ const updates = { ...attribute } ;
48+ delete updates . default ;
49+ onUpdate ( updates ) ;
50+ return ;
51+ }
52+ const parsed = parseInputValue ( defaultValueInput , attribute . type ) ;
53+ onUpdate ( { default : parsed } ) ;
54+ } ;
55+
756 return (
857 < >
958 < FormField
@@ -23,29 +72,9 @@ const MetadataFields = ({ attribute, onUpdate }) => {
2372 description = "Provide example values to guide extraction. This helps the LLM understand the expected format and content. Enter comma-separated values or a JSON array."
2473 >
2574 < Textarea
26- value = { ( ( ) => {
27- if ( ! attribute . examples ) return '' ;
28- if ( ! Array . isArray ( attribute . examples ) ) return '' ;
29- return attribute . examples . map ( ( ex ) => ( typeof ex === 'object' ? JSON . stringify ( ex ) : ex ) ) . join ( ', ' ) ;
30- } ) ( ) }
31- onChange = { ( { detail } ) => {
32- if ( ! detail . value . trim ( ) ) {
33- const updates = { ...attribute } ;
34- delete updates . examples ;
35- onUpdate ( updates ) ;
36- return ;
37- }
38- try {
39- const parsed = JSON . parse ( `[${ detail . value } ]` ) ;
40- onUpdate ( { examples : parsed } ) ;
41- } catch {
42- const examples = detail . value
43- . split ( ',' )
44- . map ( ( v ) => v . trim ( ) )
45- . filter ( ( v ) => v ) ;
46- onUpdate ( { examples : examples . length > 0 ? examples : undefined } ) ;
47- }
48- } }
75+ value = { examplesInput }
76+ onChange = { ( { detail } ) => setExamplesInput ( detail . value ) }
77+ onBlur = { handleExamplesBlur }
4978 rows = { 2 }
5079 placeholder = 'e.g., "INV-2024-001", "PO-12345" or ["John Doe", "Jane Smith"]'
5180 />
@@ -56,17 +85,9 @@ const MetadataFields = ({ attribute, onUpdate }) => {
5685 description = "Fallback value to use if this field is not found or cannot be extracted from the document."
5786 >
5887 < Input
59- value = { formatValueForInput ( attribute . default ) }
60- onChange = { ( { detail } ) => {
61- if ( ! detail . value ) {
62- const updates = { ...attribute } ;
63- delete updates . default ;
64- onUpdate ( updates ) ;
65- return ;
66- }
67- const parsed = parseInputValue ( detail . value , attribute . type ) ;
68- onUpdate ( { default : parsed } ) ;
69- } }
88+ value = { defaultValueInput }
89+ onChange = { ( { detail } ) => setDefaultValueInput ( detail . value ) }
90+ onBlur = { handleDefaultValueBlur }
7091 placeholder = "e.g., 0, N/A, or a JSON value"
7192 />
7293 </ FormField >
0 commit comments