66 FormLabel ,
77 Input
88} from '@chakra-ui/react' ;
9- import { useField } from 'formik' ;
9+ import { FastField , FastFieldProps } from 'formik' ;
1010import { SchemaFieldString , WidgetProps } from '@stac-manager/data-core' ;
1111import { CollecticonTrashBin } from '@devseed-ui/collecticons-chakra' ;
1212
@@ -21,6 +21,8 @@ interface WidgetInputProps extends WidgetProps {
2121 isDeleteDisabled ?: boolean ;
2222}
2323
24+ const identity = ( v : any ) => v ;
25+
2426export function WidgetInput ( props : WidgetInputProps ) {
2527 const {
2628 label,
@@ -30,47 +32,56 @@ export function WidgetInput(props: WidgetInputProps) {
3032 pointer,
3133 isRequired,
3234 type,
33- transformValue = ( v ) => v
35+ transformValue = identity
3436 } = props ;
3537 const field = props . field as SchemaFieldString ;
3638
37- const [ { onBlur } , meta , { setValue } ] = useField < number > ( props . pointer ) ;
38-
3939 const fieldLabel = label || field . label ;
4040
4141 return (
42- < FormControl isRequired = { isRequired } >
43- < Flex gap = { 4 } >
44- { fieldLabel && (
45- < FormLabel >
46- < FieldLabel size = 'xs' > { fieldLabel } </ FieldLabel >
47- </ FormLabel >
48- ) }
49- < Flex ml = 'auto' gap = { 2 } >
50- { isDeletable && (
51- < FieldIconBtn
52- aria-label = 'Remove item'
53- onClick = { onDeleteClick }
54- icon = { < CollecticonTrashBin size = { 3 } /> }
55- isDisabled = { isDeleteDisabled }
56- />
42+ < FastField name = { pointer } >
43+ { ( {
44+ field : { value, onBlur } ,
45+ meta,
46+ form : { setFieldValue }
47+ } : FastFieldProps ) => (
48+ < FormControl isRequired = { isRequired } >
49+ < Flex gap = { 4 } >
50+ { fieldLabel && (
51+ < FormLabel >
52+ < FieldLabel size = 'xs' > { fieldLabel } </ FieldLabel >
53+ </ FormLabel >
54+ ) }
55+ < Flex ml = 'auto' gap = { 2 } >
56+ { isDeletable && (
57+ < FieldIconBtn
58+ aria-label = 'Remove item'
59+ onClick = { onDeleteClick }
60+ icon = { < CollecticonTrashBin size = { 3 } /> }
61+ isDisabled = { isDeleteDisabled }
62+ />
63+ ) }
64+ </ Flex >
65+ </ Flex >
66+ < Input
67+ type = { type }
68+ size = 'sm'
69+ name = { pointer }
70+ bg = 'surface.500'
71+ borderColor = 'base.200'
72+ borderRadius = 'md'
73+ value = { value === null ? '' : value }
74+ onBlur = { onBlur }
75+ onChange = { ( e ) => {
76+ setFieldValue ( pointer , transformValue ( e . target . value ) ) ;
77+ } }
78+ isInvalid = { meta . touched && meta . error ? true : false }
79+ />
80+ { meta . touched && meta . error && (
81+ < FormErrorMessage > { meta . error } </ FormErrorMessage >
5782 ) }
58- </ Flex >
59- </ Flex >
60- < Input
61- type = { type }
62- size = 'sm'
63- name = { pointer }
64- bg = 'surface.500'
65- borderColor = 'base.200'
66- borderRadius = 'md'
67- onBlur = { onBlur }
68- onChange = { ( e ) => setValue ( transformValue ( e . target . value ) ) }
69- isInvalid = { meta . touched && meta . error ? true : false }
70- />
71- { meta . touched && meta . error && (
72- < FormErrorMessage > { meta . error } </ FormErrorMessage >
83+ </ FormControl >
7384 ) }
74- </ FormControl >
85+ </ FastField >
7586 ) ;
7687}
0 commit comments