@@ -12,9 +12,10 @@ import {
1212 EDITABLE_SEGMENTS ,
1313 addSegment ,
1414 getEditableSections ,
15+ isAllSegmentsValid ,
1516 parseDateFromString ,
1617 setSegment ,
17- splitFormatIntoSections ,
18+ useFormatSections ,
1819} from '../utils' ;
1920
2021import { useBaseDateFieldState } from './useBaseDateFieldState' ;
@@ -64,13 +65,13 @@ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState
6465 let validSegments = validSegmentsState [ 0 ] ;
6566 const setValidSegments = validSegmentsState [ 1 ] ;
6667
67- if ( value && Object . keys ( validSegments ) . length < Object . keys ( allSegments ) . length ) {
68+ if ( value && ! isAllSegmentsValid ( allSegments , validSegments ) ) {
6869 setValidSegments ( { ...allSegments } ) ;
6970 }
7071
7172 if (
7273 ! value &&
73- Object . keys ( validSegments ) . length > 0 &&
74+ isAllSegmentsValid ( allSegments , validSegments ) &&
7475 Object . keys ( validSegments ) . length === Object . keys ( allSegments ) . length
7576 ) {
7677 validSegments = { } ;
@@ -84,9 +85,7 @@ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState
8485 }
8586
8687 const displayValue =
87- value &&
88- isValid ( value ) &&
89- Object . keys ( validSegments ) . length >= Object . keys ( allSegments ) . length
88+ value && isValid ( value ) && isAllSegmentsValid ( allSegments , validSegments )
9089 ? value . timeZone ( timeZone )
9190 : placeholderDate . timeZone ( timeZone ) ;
9291 const sectionsState = useSectionsState ( sections , displayValue , validSegments ) ;
@@ -128,7 +127,7 @@ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState
128127 return ;
129128 }
130129
131- if ( Object . keys ( validSegments ) . length >= Object . keys ( allSegments ) . length ) {
130+ if ( isAllSegmentsValid ( allSegments , validSegments ) ) {
132131 if ( ! value || ! newValue . isSame ( value ) ) {
133132 handleUpdateDate ( newValue ) ;
134133 }
@@ -153,24 +152,31 @@ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState
153152
154153 function setSection ( sectionIndex : number , amount : number ) {
155154 const section = sectionsState . editableSections [ sectionIndex ] ;
156- markValid ( section . type ) ;
157- setValue ( setSegment ( section , displayValue , amount ) ) ;
155+ if ( section ) {
156+ markValid ( section . type ) ;
157+ setValue ( setSegment ( section , displayValue , amount ) ) ;
158+ }
158159 }
159160
160161 function adjustSection ( sectionIndex : number , amount : number ) {
161162 const section = sectionsState . editableSections [ sectionIndex ] ;
162- if ( validSegments [ section . type ] ) {
163- setValue ( addSegment ( section , displayValue , amount ) ) ;
164- } else {
165- markValid ( section . type ) ;
166- if ( Object . keys ( validSegments ) . length >= Object . keys ( allSegments ) . length ) {
167- setValue ( displayValue ) ;
163+ if ( section ) {
164+ if ( validSegments [ section . type ] ) {
165+ setValue ( addSegment ( section , displayValue , amount ) ) ;
166+ } else {
167+ markValid ( section . type ) ;
168+ if ( Object . keys ( validSegments ) . length >= Object . keys ( allSegments ) . length ) {
169+ setValue ( displayValue ) ;
170+ }
168171 }
169172 }
170173 }
171174
172175 function flushValidSection ( sectionIndex : number ) {
173- delete validSegments [ sectionsState . editableSections [ sectionIndex ] . type ] ;
176+ const section = sectionsState . editableSections [ sectionIndex ] ;
177+ if ( section ) {
178+ delete validSegments [ section . type ] ;
179+ }
174180 setValidSegments ( { ...validSegments } ) ;
175181 }
176182
@@ -208,7 +214,7 @@ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState
208214 ( isInvalid ( value , props . minValue , props . maxValue ) ? 'invalid' : undefined ) ||
209215 ( value && props . isDateUnavailable ?.( value ) ? 'invalid' : undefined ) ;
210216
211- return useBaseDateFieldState < DateTime > ( {
217+ return useBaseDateFieldState ( {
212218 value,
213219 displayValue,
214220 placeholderValue : props . placeholderValue ,
@@ -234,19 +240,6 @@ export function useDateFieldState(props: DateFieldStateOptions): DateFieldState
234240 } ) ;
235241}
236242
237- function useFormatSections ( format : string ) {
238- const usedFormat = format ;
239- const [ sections , setSections ] = React . useState ( ( ) => splitFormatIntoSections ( usedFormat ) ) ;
240-
241- const [ previousFormat , setFormat ] = React . useState ( usedFormat ) ;
242- if ( usedFormat !== previousFormat ) {
243- setFormat ( usedFormat ) ;
244- setSections ( splitFormatIntoSections ( usedFormat ) ) ;
245- }
246-
247- return sections ;
248- }
249-
250243function useSectionsState (
251244 sections : DateFieldSectionWithoutPosition [ ] ,
252245 value : DateTime ,
0 commit comments