9
9
10
10
import type { CowContext } from 'mutate-cow' ;
11
11
12
+ import formatDate from '../../common/utility/formatDate.js' ;
12
13
import parseNaturalDate from '../../common/utility/parseNaturalDate.js' ;
13
14
import { isDateValid , isYearFourDigits } from '../utility/dates.js' ;
14
15
import { applyPendingErrors } from '../utility/subfieldErrors.js' ;
@@ -17,12 +18,9 @@ import {applyPendingErrors} from '../utility/subfieldErrors.js';
17
18
export type ActionT =
18
19
| {
19
20
+ type : 'set-date' ,
20
- + date : {
21
- + year ? : string ,
22
- + month ? : string ,
23
- + day ? : string ,
24
- } ,
21
+ + date : PartialDateStringsT ,
25
22
}
23
+ | { + type : 'set-parsed-date' , + date : string }
26
24
| { + type : 'show-pending-errors' } ;
27
25
/* eslint-enable ft-flow/sort-keys */
28
26
@@ -32,6 +30,39 @@ type ControlledPropsT =
32
30
33
31
export type StateT = PartialDateFieldT ;
34
32
33
+ export function createInitialState (
34
+ date : StateT ,
35
+ ) : StateT {
36
+ return {
37
+ ...date ,
38
+ formattedDate : formatParserDate ( date ) ,
39
+ } ;
40
+ }
41
+
42
+ export function formatParserDate ( date : StateT ) : string {
43
+ return formatDate ( {
44
+ day : date . field . day . value ,
45
+ month : date . field . month . value ,
46
+ year : date . field . year . value ,
47
+ } ) ;
48
+ }
49
+
50
+ function updateDate ( dateCtx : CowContext < StateT > , date : PartialDateStringsT ) {
51
+ const newYear = date . year ;
52
+ const newMonth = date . month ;
53
+ const newDay = date . day ;
54
+ if ( newYear != null ) {
55
+ dateCtx . set ( 'field' , 'year' , 'value' , newYear ) ;
56
+ }
57
+ if ( newMonth != null ) {
58
+ dateCtx . set ( 'field' , 'month' , 'value' , newMonth ) ;
59
+ }
60
+ if ( newDay != null ) {
61
+ dateCtx . set ( 'field' , 'day' , 'value' , newDay ) ;
62
+ }
63
+ validateDate ( dateCtx ) ;
64
+ }
65
+
35
66
function validateDate ( dateCtx : CowContext < StateT > ) {
36
67
const date = dateCtx . read ( ) ;
37
68
const year = String ( date . field . year . value ?? '' ) ;
@@ -66,20 +97,20 @@ export function runReducer(
66
97
applyPendingErrors ( state ) ;
67
98
break ;
68
99
}
100
+ case 'set-parsed-date' : {
101
+ const date = parseNaturalDate ( action . date ) ;
102
+ updateDate ( state , date ) ;
103
+ state . set ( 'formattedDate' , action . date ) ;
104
+ break ;
105
+ }
69
106
case 'set-date' : {
70
- const newYear = action . date . year ;
71
- const newMonth = action . date . month ;
72
- const newDay = action . date . day ;
73
- if ( newYear != null ) {
74
- state . set ( 'field' , 'year' , 'value' , newYear ) ;
107
+ updateDate ( state , action . date ) ;
108
+ const formattedDate = formatParserDate ( state . read ( ) ) ;
109
+ if ( nonEmpty ( formattedDate ) ) {
110
+ state . set ( 'formattedDate' , formatParserDate ( state . read ( ) ) ) ;
111
+ } else {
112
+ state . set ( 'formattedDate' , formatParserDate ( state . read ( ) ) ) ;
75
113
}
76
- if ( newMonth != null ) {
77
- state . set ( 'field' , 'month' , 'value' , newMonth ) ;
78
- }
79
- if ( newDay != null ) {
80
- state . set ( 'field' , 'day' , 'value' , newDay ) ;
81
- }
82
- validateDate ( state ) ;
83
114
break ;
84
115
}
85
116
}
@@ -95,6 +126,7 @@ type DatePartPropsT = {
95
126
type DateParserPropsT = {
96
127
onBlur ?: ( ) => void ,
97
128
onChange ?: ( SyntheticEvent < HTMLInputElement > ) => void ,
129
+ value ? : string ,
98
130
} ;
99
131
100
132
component PartialDateInput (
@@ -147,16 +179,16 @@ component PartialDateInput(
147
179
) ;
148
180
149
181
parserProps . onChange = ( event ) => {
150
- const date = parseNaturalDate ( event . currentTarget . value ) ;
151
182
controlledProps . dispatch ( {
152
- date,
153
- type : 'set-date' ,
183
+ date : event . currentTarget . value ,
184
+ type : 'set-parsed- date' ,
154
185
} ) ;
155
186
} ;
156
187
157
188
yearProps . value = field . field . year . value ?? '' ;
158
189
monthProps . value = field . field . month . value ?? '' ;
159
190
dayProps . value = field . field . day . value ?? '' ;
191
+ parserProps . value = field . formattedDate ?? '' ;
160
192
}
161
193
162
194
return (
0 commit comments