@@ -124,7 +124,7 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
124124 constructor ( props : DatePickerProps ) {
125125 super ( props ) ;
126126
127- const newState = this . getInitialState ( props , '' ) ;
127+ const newState = DatePicker . getInitialState ( props , '' ) ;
128128 this . state = {
129129 ...newState ,
130130 expanded : false ,
@@ -140,23 +140,23 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
140140 *
141141 * @param props DatePickerProps
142142 */
143- getInitialState ( props : DatePickerProps , currentValue : string ) : DatePickerState {
143+ static getInitialState ( props : DatePickerProps , currentValue : string , currentInitialValue ?: MethodDate ) : DatePickerState {
144144 const local = props . localTimezone ;
145145 let value = currentValue ;
146146 let initialValue : MethodDate = null ;
147147 if ( props . initialValue ) {
148148 if ( props . initialValue === 'invalid' ) {
149- if ( this . state && this . state . initialValue ) {
149+ if ( currentInitialValue ) {
150150 initialValue = MethodDate . fromString (
151151 props . localTimezone ,
152- this . state . initialValue . dateObject . toJSON ( )
152+ currentInitialValue . dateObject . toJSON ( )
153153 ) ;
154154 }
155155 } else if ( typeof ( props . initialValue ) === 'string' ) {
156156 const date = MethodDate . fromString ( local , props . initialValue ) ;
157157 if ( date && dateIsValid ( date . dateObject , local ) ) {
158158 initialValue = date ;
159- const parsed = this . parse ( currentValue ) ;
159+ const parsed = DatePicker . parse ( currentValue , props . format , props . localTimezone ) ;
160160 if (
161161 date . year !== parsed . year ||
162162 date . month !== ( parsed . month - 1 ) ||
@@ -203,18 +203,8 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
203203 } ;
204204 }
205205
206- /**
207- * Update the Date/Time object used internally with a new initialValue
208- *
209- * @param newProps new DatePickerProps
210- */
211- UNSAFE_componentWillReceiveProps ( newProps : DatePickerProps ) {
212- if ( ( this . props . initialValue !== newProps . initialValue || this . props . localTimezone !== newProps . localTimezone ) && newProps . initialValue !== 'invalid' ) {
213- const newState = this . getInitialState ( newProps , this . input . value ) ;
214- this . setState ( {
215- ...newState ,
216- } ) ;
217- }
206+ static getDerivedStateFromProps ( props : DatePickerProps , state : DatePickerState ) : Partial < DatePickerState > | null {
207+ return DatePicker . getInitialState ( props , state . value , state . initialValue ) ;
218208 }
219209
220210 componentDidMount ( ) {
@@ -227,7 +217,7 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
227217 window . removeEventListener ( 'keydown' , this . onKeydown ) ;
228218 }
229219
230- parse ( newValue : string ) {
220+ static parse ( newValue : string , format : DateFormat , localTimezone : boolean ) {
231221 let valid = true ;
232222
233223 let split = newValue . split ( '/' ) ;
@@ -239,17 +229,17 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
239229 }
240230
241231 let year , month , date ;
242- if ( this . props . format === DateFormat . DDMMYYYY ) {
232+ if ( format === DateFormat . DDMMYYYY ) {
243233 year = parseInt ( split [ 2 ] ) ;
244234 month = parseInt ( split [ 1 ] ) ;
245235 date = parseInt ( split [ 0 ] ) ;
246236 }
247- else if ( this . props . format === DateFormat . MMDDYYYY ) {
237+ else if ( format === DateFormat . MMDDYYYY ) {
248238 year = parseInt ( split [ 2 ] ) ;
249239 month = parseInt ( split [ 0 ] ) ;
250240 date = parseInt ( split [ 1 ] ) ;
251241 }
252- else if ( this . props . format === DateFormat . YYYYMMDD ) {
242+ else if ( format === DateFormat . YYYYMMDD ) {
253243 year = parseInt ( split [ 0 ] ) ;
254244 month = parseInt ( split [ 1 ] ) ;
255245 date = parseInt ( split [ 2 ] ) ;
@@ -271,7 +261,7 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
271261
272262 if ( valid ) {
273263 let parsed = new MethodDate (
274- this . props . localTimezone ,
264+ localTimezone ,
275265 year ,
276266 month - 1 ,
277267 date
@@ -305,7 +295,7 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
305295 this . setState ( { value : newValue } ) ;
306296 }
307297 } else {
308- let result = this . parse ( newValue ) ;
298+ let result = DatePicker . parse ( newValue , this . props . format , this . props . localTimezone ) ;
309299 if ( result . valid ) {
310300 const initialValue = this . state . initialValue ;
311301 const dateValue = new MethodDate (
@@ -387,7 +377,7 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
387377
388378 const placeholder = placeholders [ this . props . format ] ;
389379
390- const parsed = this . parse ( this . state . value ) ;
380+ const parsed = DatePicker . parse ( this . state . value , this . props . format , this . props . localTimezone ) ;
391381 const inputClassName = css ( 'date-picker-input' , {
392382 'error' : ! ! this . props . error || (
393383 ! parsed . valid && ! ! this . props . initialValue
0 commit comments