1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { useIntl } from 'react-intl' ;
4
+ import useFieldId from '../../../hooks/use-field-id' ;
5
+ import usePrevious from '../../../hooks/use-previous' ;
4
6
import filterDataAttributes from '../../../utils/filter-data-attributes' ;
5
- import getFieldId from '../../../utils/get-field-id' ;
6
7
import createSequentialId from '../../../utils/create-sequential-id' ;
7
8
import Constraints from '../../constraints' ;
8
9
import { parseTime } from '../../../utils/parse-time' ;
@@ -24,8 +25,10 @@ const format24hr = ({ hours, minutes, seconds, milliseconds }) => {
24
25
const hasMilliseconds = parsedTime => parsedTime . milliseconds !== 0 ;
25
26
26
27
const TimeInput = props => {
27
- const id = getFieldId ( props , { } , sequentialId ) ;
28
+ const id = useFieldId ( props . id , sequentialId ) ;
28
29
const intl = useIntl ( ) ;
30
+ const prevLocale = usePrevious ( intl . locale ) ;
31
+
29
32
const { name, value, onBlur, onChange } = props ;
30
33
31
34
const emitChange = React . useCallback (
@@ -53,15 +56,10 @@ const TimeInput = props => {
53
56
54
57
const onClear = React . useCallback ( ( ) => emitChange ( '' ) , [ emitChange ] ) ;
55
58
56
- // so that it doesn't run on initial mount
57
- const mounted = React . useRef ( ) ;
58
- React . useEffect ( ( ) => {
59
- if ( ! mounted . current ) {
60
- mounted . current = true ;
61
- } else {
62
- emitChange ( TimeInput . toLocaleTime ( value , intl . locale ) ) ;
63
- }
64
- } , [ intl . locale , emitChange , value ] ) ;
59
+ // if locale has changed
60
+ if ( typeof prevLocale !== 'undefined' && prevLocale !== intl . locale ) {
61
+ emitChange ( TimeInput . toLocaleTime ( value , intl . locale ) ) ;
62
+ }
65
63
66
64
return (
67
65
< Constraints . Horizontal constraint = { props . horizontalConstraint } >
0 commit comments