@@ -12,12 +12,18 @@ import * as React from 'react';
12
12
import { isDateValid , isYearFourDigits }
13
13
from '../static/scripts/edit/utility/dates' ;
14
14
import { applyPendingErrors } from '../utility/subfieldErrors' ;
15
+ import parseNaturalDate
16
+ from '../static/scripts/common/utility/parseNaturalDate' ;
15
17
16
18
/* eslint-disable flowtype/sort-keys */
17
19
export type ActionT =
18
20
| {
19
21
+ type : 'set-date' ,
20
- + date : { + year ? : string , + month ? : string , + day ? : string } ,
22
+ + date : {
23
+ + year ? : string ,
24
+ + month ? : string ,
25
+ + day ? : string ,
26
+ } ,
21
27
}
22
28
| { + type : 'show-pending-errors' } ;
23
29
/* eslint-enable flowtype/sort-keys */
@@ -101,6 +107,7 @@ const PartialDateInput = (props: Props): React.Element<'span'> => {
101
107
const yearProps = { } ;
102
108
const monthProps = { } ;
103
109
const dayProps = { } ;
110
+ const parserProps = { } ;
104
111
105
112
if ( props . uncontrolled ) {
106
113
yearProps . defaultValue = field . field . year . value ;
@@ -125,6 +132,7 @@ const PartialDateInput = (props: Props): React.Element<'span'> => {
125
132
yearProps . onBlur = handleBlur ;
126
133
monthProps . onBlur = handleBlur ;
127
134
dayProps . onBlur = handleBlur ;
135
+ parserProps . onBlur = handleBlur ;
128
136
129
137
yearProps . onChange = ( event ) => handleDateChange (
130
138
event ,
@@ -139,6 +147,14 @@ const PartialDateInput = (props: Props): React.Element<'span'> => {
139
147
'day' ,
140
148
) ;
141
149
150
+ parserProps . onChange = ( event ) => {
151
+ const date = parseNaturalDate ( event . currentTarget . value ) ;
152
+ props . dispatch ( {
153
+ date : date ,
154
+ type : 'set-date' ,
155
+ } ) ;
156
+ } ;
157
+
142
158
yearProps . value = field . field . year . value ?? '' ;
143
159
monthProps . value = field . field . month . value ?? '' ;
144
160
dayProps . value = field . field . day . value ?? '' ;
@@ -181,6 +197,21 @@ const PartialDateInput = (props: Props): React.Element<'span'> => {
181
197
type = "text"
182
198
{ ...dayProps }
183
199
/>
200
+ { props . uncontrolled ? null : (
201
+ < >
202
+ { ' ' }
203
+ < input
204
+ className = "partial-date-parser"
205
+ disabled = { disabled }
206
+ id = { 'id-' + field . html_name + '.partial-date-parser' }
207
+ name = { field . html_name + '.partial-date-parser' }
208
+ placeholder = { l ( 'Enter date string to parse' ) }
209
+ size = { 12 }
210
+ type = "text"
211
+ { ...parserProps }
212
+ />
213
+ </ >
214
+ ) }
184
215
</ span >
185
216
) ;
186
217
} ;
0 commit comments