1- import { memo , useEffect } from "react"
1+ import { memo } from "react"
22import type { ControlProps as JSFControlProps } from "@jsonforms/core"
33import { withJsonFormsControlProps } from "@jsonforms/react"
4- import { DatePicker , Form } from "antd"
4+ import { Col , DatePicker , Form } from "antd"
55import type { Rule } from "antd/es/form"
66import dayjs from "dayjs"
77
@@ -10,6 +10,7 @@ import {
1010 DateTimeControlOptions ,
1111 isDateTimeControlOptions ,
1212} from "../ui-schema"
13+ import { useNestedAntDFormContext } from "../hooks/useNestedAntDFormContext"
1314
1415type ControlProps = Omit < JSFControlProps , "uischema" > & {
1516 uischema : ControlUISchema < unknown > | JSFControlProps [ "uischema" ]
@@ -22,19 +23,6 @@ function getOverrides(options: unknown): DateTimeControlOptions {
2223 return { }
2324}
2425
25- function getInitialValue (
26- data : unknown ,
27- schemaDefault : unknown ,
28- ) : string | undefined {
29- if ( typeof data === "string" && data !== "" ) {
30- return data
31- }
32- if ( typeof schemaDefault === "string" && schemaDefault !== "" ) {
33- return schemaDefault
34- }
35- return undefined
36- }
37-
3826export function DateTimeControl ( {
3927 handleChange,
4028 path,
@@ -46,14 +34,13 @@ export function DateTimeControl({
4634 visible,
4735 data,
4836} : ControlProps ) {
49- const setInitialValue = createDateTimeInitialValueSetter ( handleChange , path )
50- const form = Form . useFormInstance ( )
51- useEffect ( ( ) => {
52- form . setFieldValue (
53- path ,
54- setInitialValue ( getInitialValue ( data , schema . default ) ) ,
55- )
56- } , [ data , form , path , schema . default , setInitialValue ] )
37+ const nestedAntdData = useNestedAntDFormContext ( )
38+ const name = nestedAntdData
39+ ? nestedAntdData . index !== undefined
40+ ? [ nestedAntdData . path , nestedAntdData . index ]
41+ : nestedAntdData . path
42+ : path
43+
5744 if ( ! visible ) return null
5845
5946 const rules : Rule [ ] = [ { required, message : `${ label } is required` } ]
@@ -64,36 +51,38 @@ export function DateTimeControl({
6451 const overrides = getOverrides ( uischema . options )
6552
6653 return (
67- < Form . Item
68- label = { label }
69- id = { id }
70- name = { path }
71- required = { required }
72- validateTrigger = { [ "onBlur" ] }
73- rules = { rules }
74- { ...formItemProps }
75- >
76- < DatePicker
77- format = { "YYYY-MM-DDTHH:mm:ssZ" }
78- onChange = { ( _ , dateString ) => handleChange ( path , dateString ) }
79- { ...overrides }
80- />
81- </ Form . Item >
54+ < Col >
55+ < Form . Item
56+ label = { label }
57+ id = { id }
58+ name = { name }
59+ required = { required }
60+ validateTrigger = { [ "onBlur" ] }
61+ rules = { rules }
62+ initialValue = { getInitialValue ( data , schema . default ) }
63+ { ...formItemProps }
64+ >
65+ < DatePicker
66+ format = { "YYYY-MM-DDTHH:mm:ssZ" }
67+ onChange = { ( _ , dateString ) => handleChange ( path , dateString ) }
68+ { ...overrides }
69+ />
70+ </ Form . Item >
71+ </ Col >
8272 )
8373}
8474
85- /**
86- * Creates an initial value setter for DateTimeControl that coerces string values to dayjs objects
87- */
88- function createDateTimeInitialValueSetter (
89- handleChange : ( path : string , value : string | undefined ) => void ,
90- path : string ,
91- ) {
92- return ( value : string | undefined ) => {
93- const coercedValue = value ? dayjs ( value ) : value
94- handleChange ( path , value )
95- return coercedValue
75+ function getInitialValue (
76+ data : unknown ,
77+ schemaDefault : unknown ,
78+ ) : dayjs . Dayjs | undefined {
79+ if ( typeof data === "string" && data !== "" ) {
80+ return dayjs ( data )
81+ }
82+ if ( typeof schemaDefault === "string" && schemaDefault !== "" ) {
83+ return dayjs ( schemaDefault )
9684 }
85+ return undefined
9786}
9887
9988export const DateTimeRenderer = withJsonFormsControlProps ( memo ( DateTimeControl ) )
0 commit comments