2222 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323 THE SOFTWARE.
2424*/
25- import React , { useMemo } from 'react' ;
25+ import React , { useCallback , useMemo , useState } from 'react' ;
2626import merge from 'lodash/merge' ;
2727import {
2828 ControlProps ,
2929 isTimeControl ,
3030 isDescriptionHidden ,
3131 RankedTester ,
3232 rankWith ,
33+ defaultTimeFormat ,
3334} from '@jsonforms/core' ;
3435import { withJsonFormsControlProps } from '@jsonforms/react' ;
3536import { FormHelperText , Hidden } from '@mui/material' ;
3637import { TimePicker , LocalizationProvider } from '@mui/x-date-pickers' ;
3738import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs' ;
38- import { createOnChangeHandler , getData , useFocus } from '../util' ;
39+ import {
40+ createOnBlurHandler ,
41+ createOnChangeHandler ,
42+ getData ,
43+ useFocus ,
44+ } from '../util' ;
3945
4046export const MaterialTimeControl = ( props : ControlProps ) => {
4147 const [ focused , onFocus , onBlur ] = useFocus ( ) ;
@@ -56,6 +62,8 @@ export const MaterialTimeControl = (props: ControlProps) => {
5662 const appliedUiSchemaOptions = merge ( { } , config , uischema . options ) ;
5763 const isValid = errors . length === 0 ;
5864
65+ const [ key , setKey ] = useState < number > ( 0 ) ;
66+
5967 const showDescription = ! isDescriptionHidden (
6068 visible ,
6169 description ,
@@ -64,7 +72,7 @@ export const MaterialTimeControl = (props: ControlProps) => {
6472 ) ;
6573
6674 const format = appliedUiSchemaOptions . timeFormat ?? 'HH:mm' ;
67- const saveFormat = appliedUiSchemaOptions . timeSaveFormat ?? 'HH:mm:ss' ;
75+ const saveFormat = appliedUiSchemaOptions . timeSaveFormat ?? defaultTimeFormat ;
6876
6977 const views = appliedUiSchemaOptions . views ?? [ 'hours' , 'minutes' ] ;
7078
@@ -75,19 +83,35 @@ export const MaterialTimeControl = (props: ControlProps) => {
7583 : null ;
7684 const secondFormHelperText = showDescription && ! isValid ? errors : null ;
7785
86+ const updateChild = useCallback ( ( ) => setKey ( ( key ) => key + 1 ) , [ ] ) ;
87+
7888 const onChange = useMemo (
7989 ( ) => createOnChangeHandler ( path , handleChange , saveFormat ) ,
8090 [ path , handleChange , saveFormat ]
8191 ) ;
8292
93+ const onBlurHandler = useMemo (
94+ ( ) =>
95+ createOnBlurHandler (
96+ path ,
97+ handleChange ,
98+ format ,
99+ saveFormat ,
100+ updateChild ,
101+ onBlur
102+ ) ,
103+ [ path , handleChange , format , saveFormat , updateChild ]
104+ ) ;
83105 const value = getData ( data , saveFormat ) ;
106+
84107 return (
85108 < Hidden xsUp = { ! visible } >
86109 < LocalizationProvider dateAdapter = { AdapterDayjs } >
87110 < TimePicker
111+ key = { key }
88112 label = { label }
89113 value = { value }
90- onChange = { onChange }
114+ onAccept = { onChange }
91115 format = { format }
92116 ampm = { ! ! appliedUiSchemaOptions . ampm }
93117 views = { views }
@@ -111,7 +135,7 @@ export const MaterialTimeControl = (props: ControlProps) => {
111135 } ,
112136 InputLabelProps : data ? { shrink : true } : undefined ,
113137 onFocus : onFocus ,
114- onBlur : onBlur ,
138+ onBlur : onBlurHandler ,
115139 } ,
116140 } }
117141 />
0 commit comments