@@ -17,7 +17,7 @@ type DateInputChangeEvent = ChangeEvent<HTMLInputElement> & {
1717
1818interface DateInputProps
1919 extends Omit < HTMLProps < HTMLDivElement > , 'value' | 'defaultValue' > ,
20- FormElementProps {
20+ FormElementProps {
2121 autoSelectNext ?: boolean ;
2222 value ?: Partial < DateInputValue > ;
2323 defaultValue ?: Partial < DateInputValue > ;
@@ -38,16 +38,47 @@ interface DateInput extends PureComponent<DateInputProps, DateInputState> {
3838}
3939
4040class DateInput extends PureComponent < DateInputProps , DateInputState > {
41+ static Day = DayInput ;
42+
43+ static Month = MonthInput ;
44+
45+ static Year = YearInput ;
46+
4147 constructor ( props : DateInputProps , ...rest : any [ ] ) {
4248 super ( props , ...rest ) ;
4349 this . state = {
44- values : { day : '' , month : '' , year : '' } ,
50+ values : {
51+ day : props . value ?. day || '' ,
52+ month : props . value ?. month || '' ,
53+ year : props . value ?. year || '' ,
54+ } ,
4555 } ;
4656
4757 this . monthRef = null ;
4858 this . yearRef = null ;
4959 }
5060
61+ componentDidUpdate ( prevProps : DateInputProps ) {
62+ if ( this . props . value && prevProps . value !== this . props . value ) {
63+ // This is the only way that we can update our internal state
64+ // when the value updates. We check if the value has changed first,
65+ // preventing an infinite loop.
66+ //
67+ // eslint-disable-next-line react/no-did-update-set-state
68+ this . setState ( state => {
69+ if ( ! this . props . value ) return state ;
70+
71+ const newState = { ...state } ;
72+ const { day, month, year } = this . props . value ;
73+ if ( day && day !== state . values . day ) newState . values . day = day ;
74+ if ( month && month !== state . values . month ) newState . values . month = month ;
75+ if ( year && year !== state . values . year ) newState . values . year = year ;
76+
77+ return newState ;
78+ } ) ;
79+ }
80+ }
81+
5182 handleSelectNext = ( inputType : 'day' | 'month' | 'year' , value : string ) => {
5283 if ( ! this . props . autoSelectNext ) return ;
5384 if ( inputType === 'day' && value . length === 2 && this . monthRef ) {
@@ -82,11 +113,6 @@ class DateInput extends PureComponent<DateInputProps, DateInputState> {
82113 if ( inputType === 'year' ) this . yearRef = ref ;
83114 } ;
84115
85- static Day = DayInput ;
86-
87- static Month = MonthInput ;
88-
89- static Year = YearInput ;
90116
91117 render ( ) {
92118 const { children, onChange, value, defaultValue, ...rest } = this . props ;
0 commit comments