@khanacademy/wonder-blocks-birthday-picker 4.0.1 → 4.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/dist/es/index.js +2 -287
- package/dist/index.js +2 -288
- package/package.json +6 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @khanacademy/wonder-blocks-birthday-picker
|
|
2
2
|
|
|
3
|
+
## 4.0.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [b9e4946]
|
|
8
|
+
- Updated dependencies [b9e4946]
|
|
9
|
+
- @khanacademy/wonder-blocks-tokens@10.0.0
|
|
10
|
+
- @khanacademy/wonder-blocks-typography@3.2.0
|
|
11
|
+
- @khanacademy/wonder-blocks-dropdown@10.0.5
|
|
12
|
+
- @khanacademy/wonder-blocks-layout@3.1.10
|
|
13
|
+
- @khanacademy/wonder-blocks-core@12.2.1
|
|
14
|
+
- @khanacademy/wonder-blocks-icon@5.1.3
|
|
15
|
+
|
|
16
|
+
## 4.0.2
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- @khanacademy/wonder-blocks-dropdown@10.0.4
|
|
21
|
+
|
|
3
22
|
## 4.0.1
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/dist/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
2
2
|
import { Temporal } from 'temporal-polyfill';
|
|
3
3
|
import * as React from 'react';
|
|
4
4
|
import { StyleSheet } from 'aphrodite';
|
|
@@ -10,291 +10,6 @@ import { PhosphorIcon } from '@khanacademy/wonder-blocks-icon';
|
|
|
10
10
|
import { SingleSelect, OptionItem } from '@khanacademy/wonder-blocks-dropdown';
|
|
11
11
|
import infoIcon from '@phosphor-icons/core/bold/info-bold.svg';
|
|
12
12
|
|
|
13
|
-
const CUR_YEAR = new Date().
|
|
14
|
-
const defaultLabels = Object.freeze({
|
|
15
|
-
errorMessage: "Please select a valid birthdate.",
|
|
16
|
-
month: "Month",
|
|
17
|
-
year: "Year",
|
|
18
|
-
day: "Day"
|
|
19
|
-
});
|
|
20
|
-
const FIELD_MIN_WIDTH_FULL = 110;
|
|
21
|
-
const FIELD_MIN_WIDTH_MONTH_YEAR = 167;
|
|
22
|
-
const FIELD_MIN_WIDTH_DAY = 100;
|
|
23
|
-
const xsMin = "520px";
|
|
24
|
-
const screenSizes = {
|
|
25
|
-
small: `@media (max-width: ${xsMin})`
|
|
26
|
-
};
|
|
27
|
-
const defaultStyles = StyleSheet.create({
|
|
28
|
-
wrapper: {
|
|
29
|
-
flexDirection: "row",
|
|
30
|
-
[screenSizes.small]: {
|
|
31
|
-
flexDirection: "column"
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
input: {
|
|
35
|
-
[screenSizes.small]: {
|
|
36
|
-
minWidth: "100%"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
class BirthdayPicker extends React.Component {
|
|
41
|
-
constructor(props) {
|
|
42
|
-
super(props);
|
|
43
|
-
this.labels = void 0;
|
|
44
|
-
this.lastChangeValue = null;
|
|
45
|
-
this.reportChange = value => {
|
|
46
|
-
if (value !== this.lastChangeValue) {
|
|
47
|
-
this.lastChangeValue = value;
|
|
48
|
-
this.props.onChange(value);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
this.handleChange = () => {
|
|
52
|
-
const {
|
|
53
|
-
month,
|
|
54
|
-
day,
|
|
55
|
-
year
|
|
56
|
-
} = this.state;
|
|
57
|
-
const {
|
|
58
|
-
monthYearOnly
|
|
59
|
-
} = this.props;
|
|
60
|
-
const dateFields = [year, month];
|
|
61
|
-
if (!monthYearOnly) {
|
|
62
|
-
dateFields.push(day);
|
|
63
|
-
}
|
|
64
|
-
if (dateFields.some(field => field === null)) {
|
|
65
|
-
this.reportChange(null);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
let date;
|
|
69
|
-
try {
|
|
70
|
-
if (monthYearOnly) {
|
|
71
|
-
date = Temporal.PlainDate.from({
|
|
72
|
-
year: Number(year),
|
|
73
|
-
month: Number(month),
|
|
74
|
-
day: 31
|
|
75
|
-
});
|
|
76
|
-
} else {
|
|
77
|
-
date = Temporal.PlainDate.from({
|
|
78
|
-
year: Number(year),
|
|
79
|
-
month: Number(month),
|
|
80
|
-
day: Number(day)
|
|
81
|
-
}, {
|
|
82
|
-
overflow: "reject"
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
} catch (err) {
|
|
86
|
-
this.setState({
|
|
87
|
-
error: this.labels.errorMessage
|
|
88
|
-
});
|
|
89
|
-
this.reportChange(null);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
if (this.isFutureDate(date)) {
|
|
93
|
-
this.setState({
|
|
94
|
-
error: this.labels.errorMessage
|
|
95
|
-
});
|
|
96
|
-
this.reportChange(null);
|
|
97
|
-
} else {
|
|
98
|
-
this.setState({
|
|
99
|
-
error: null
|
|
100
|
-
});
|
|
101
|
-
this.reportChange(date.toString());
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
this.handleMonthChange = month => {
|
|
105
|
-
this.setState({
|
|
106
|
-
month
|
|
107
|
-
}, this.handleChange);
|
|
108
|
-
};
|
|
109
|
-
this.handleDayChange = day => {
|
|
110
|
-
this.setState({
|
|
111
|
-
day
|
|
112
|
-
}, this.handleChange);
|
|
113
|
-
};
|
|
114
|
-
this.handleYearChange = year => {
|
|
115
|
-
this.setState({
|
|
116
|
-
year
|
|
117
|
-
}, this.handleChange);
|
|
118
|
-
};
|
|
119
|
-
this.lastChangeValue = props.defaultValue || null;
|
|
120
|
-
this.state = this.getStateFromDefault();
|
|
121
|
-
}
|
|
122
|
-
getStateFromDefault() {
|
|
123
|
-
const {
|
|
124
|
-
defaultValue,
|
|
125
|
-
monthYearOnly
|
|
126
|
-
} = this.props;
|
|
127
|
-
const initialState = {
|
|
128
|
-
month: null,
|
|
129
|
-
day: monthYearOnly ? "1" : null,
|
|
130
|
-
year: null,
|
|
131
|
-
error: null
|
|
132
|
-
};
|
|
133
|
-
this.labels = _extends({}, defaultLabels, this.props.labels);
|
|
134
|
-
if (defaultValue) {
|
|
135
|
-
let date = null;
|
|
136
|
-
try {
|
|
137
|
-
date = Temporal.PlainDate.from(defaultValue);
|
|
138
|
-
} catch (err) {
|
|
139
|
-
initialState.error = this.labels.errorMessage;
|
|
140
|
-
return initialState;
|
|
141
|
-
}
|
|
142
|
-
if (monthYearOnly) {
|
|
143
|
-
date = date.with({
|
|
144
|
-
day: date.daysInMonth
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
initialState.month = String(date.month);
|
|
148
|
-
initialState.day = String(date.day);
|
|
149
|
-
initialState.year = String(date.year);
|
|
150
|
-
if (this.isFutureDate(date)) {
|
|
151
|
-
initialState.error = this.labels.errorMessage;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return initialState;
|
|
155
|
-
}
|
|
156
|
-
isFutureDate(date) {
|
|
157
|
-
return Temporal.PlainDate.compare(date, Temporal.Now.plainDateISO()) === 1;
|
|
158
|
-
}
|
|
159
|
-
maybeRenderError() {
|
|
160
|
-
const {
|
|
161
|
-
error
|
|
162
|
-
} = this.state;
|
|
163
|
-
if (!error) {
|
|
164
|
-
return null;
|
|
165
|
-
}
|
|
166
|
-
return React.createElement(React.Fragment, null, React.createElement(Strut, {
|
|
167
|
-
size: spacing.xxxSmall_4
|
|
168
|
-
}), React.createElement(View, {
|
|
169
|
-
style: {
|
|
170
|
-
flexDirection: "row",
|
|
171
|
-
placeItems: "center"
|
|
172
|
-
},
|
|
173
|
-
role: "alert"
|
|
174
|
-
}, React.createElement(PhosphorIcon, {
|
|
175
|
-
size: "small",
|
|
176
|
-
icon: infoIcon,
|
|
177
|
-
color: semanticColor.icon.destructive,
|
|
178
|
-
"aria-hidden": "true"
|
|
179
|
-
}), React.createElement(Strut, {
|
|
180
|
-
size: spacing.xxxSmall_4
|
|
181
|
-
}), React.createElement(Body, {
|
|
182
|
-
style: {
|
|
183
|
-
color: semanticColor.status.critical.foreground
|
|
184
|
-
}
|
|
185
|
-
}, error)));
|
|
186
|
-
}
|
|
187
|
-
monthsShort() {
|
|
188
|
-
const format = new Intl.DateTimeFormat(navigator.language, {
|
|
189
|
-
month: "short"
|
|
190
|
-
}).format;
|
|
191
|
-
return [...Array(12).keys()].map(m => format(new Date(2021, m, 15)));
|
|
192
|
-
}
|
|
193
|
-
renderMonth() {
|
|
194
|
-
const {
|
|
195
|
-
disabled,
|
|
196
|
-
monthYearOnly,
|
|
197
|
-
dropdownStyle
|
|
198
|
-
} = this.props;
|
|
199
|
-
const {
|
|
200
|
-
month
|
|
201
|
-
} = this.state;
|
|
202
|
-
const minWidth = this.getMonthYearWidth(monthYearOnly);
|
|
203
|
-
return React.createElement(SingleSelect, {
|
|
204
|
-
"aria-label": this.labels.month,
|
|
205
|
-
"aria-invalid": !!this.state.error,
|
|
206
|
-
error: !!this.state.error,
|
|
207
|
-
disabled: disabled,
|
|
208
|
-
placeholder: this.labels.month,
|
|
209
|
-
onChange: this.handleMonthChange,
|
|
210
|
-
selectedValue: month,
|
|
211
|
-
style: [{
|
|
212
|
-
minWidth
|
|
213
|
-
}, defaultStyles.input, dropdownStyle],
|
|
214
|
-
testId: "birthday-picker-month"
|
|
215
|
-
}, this.monthsShort().map((monthShort, i) => React.createElement(OptionItem, {
|
|
216
|
-
key: monthShort,
|
|
217
|
-
label: monthShort,
|
|
218
|
-
value: String(i + 1)
|
|
219
|
-
})));
|
|
220
|
-
}
|
|
221
|
-
maybeRenderDay() {
|
|
222
|
-
const {
|
|
223
|
-
disabled,
|
|
224
|
-
monthYearOnly,
|
|
225
|
-
dropdownStyle
|
|
226
|
-
} = this.props;
|
|
227
|
-
const {
|
|
228
|
-
day
|
|
229
|
-
} = this.state;
|
|
230
|
-
if (monthYearOnly) {
|
|
231
|
-
return null;
|
|
232
|
-
}
|
|
233
|
-
return React.createElement(React.Fragment, null, React.createElement(Strut, {
|
|
234
|
-
size: spacing.xSmall_8
|
|
235
|
-
}), React.createElement(SingleSelect, {
|
|
236
|
-
"aria-label": this.labels.day,
|
|
237
|
-
"aria-invalid": !!this.state.error,
|
|
238
|
-
error: !!this.state.error,
|
|
239
|
-
disabled: disabled,
|
|
240
|
-
placeholder: this.labels.day,
|
|
241
|
-
onChange: this.handleDayChange,
|
|
242
|
-
selectedValue: day,
|
|
243
|
-
style: [{
|
|
244
|
-
minWidth: FIELD_MIN_WIDTH_DAY
|
|
245
|
-
}, defaultStyles.input, dropdownStyle],
|
|
246
|
-
testId: "birthday-picker-day"
|
|
247
|
-
}, Array.from(Array(31)).map((_, day) => React.createElement(OptionItem, {
|
|
248
|
-
key: String(day + 1),
|
|
249
|
-
label: String(day + 1),
|
|
250
|
-
value: String(day + 1)
|
|
251
|
-
}))));
|
|
252
|
-
}
|
|
253
|
-
getMonthYearWidth(monthYearOnly) {
|
|
254
|
-
return monthYearOnly ? FIELD_MIN_WIDTH_MONTH_YEAR : FIELD_MIN_WIDTH_FULL;
|
|
255
|
-
}
|
|
256
|
-
renderYear() {
|
|
257
|
-
const {
|
|
258
|
-
disabled,
|
|
259
|
-
monthYearOnly,
|
|
260
|
-
dropdownStyle
|
|
261
|
-
} = this.props;
|
|
262
|
-
const {
|
|
263
|
-
year
|
|
264
|
-
} = this.state;
|
|
265
|
-
const minWidth = this.getMonthYearWidth(monthYearOnly);
|
|
266
|
-
return React.createElement(SingleSelect, {
|
|
267
|
-
"aria-label": this.labels.year,
|
|
268
|
-
"aria-invalid": !!this.state.error,
|
|
269
|
-
error: !!this.state.error,
|
|
270
|
-
disabled: disabled,
|
|
271
|
-
placeholder: this.labels.year,
|
|
272
|
-
onChange: this.handleYearChange,
|
|
273
|
-
selectedValue: year,
|
|
274
|
-
style: [{
|
|
275
|
-
minWidth
|
|
276
|
-
}, defaultStyles.input, dropdownStyle],
|
|
277
|
-
dropdownStyle: {
|
|
278
|
-
minWidth: 150
|
|
279
|
-
},
|
|
280
|
-
testId: "birthday-picker-year"
|
|
281
|
-
}, Array.from(Array(120)).map((_, yearOffset) => React.createElement(OptionItem, {
|
|
282
|
-
key: String(CUR_YEAR - yearOffset),
|
|
283
|
-
label: String(CUR_YEAR - yearOffset),
|
|
284
|
-
value: String(CUR_YEAR - yearOffset)
|
|
285
|
-
})));
|
|
286
|
-
}
|
|
287
|
-
render() {
|
|
288
|
-
const {
|
|
289
|
-
style
|
|
290
|
-
} = this.props;
|
|
291
|
-
return React.createElement(React.Fragment, null, React.createElement(View, {
|
|
292
|
-
testId: "birthday-picker",
|
|
293
|
-
style: [defaultStyles.wrapper, style]
|
|
294
|
-
}, this.renderMonth(), this.maybeRenderDay(), React.createElement(Strut, {
|
|
295
|
-
size: spacing.xSmall_8
|
|
296
|
-
}), this.renderYear()), this.maybeRenderError());
|
|
297
|
-
}
|
|
298
|
-
}
|
|
13
|
+
const CUR_YEAR=new Date().getYear()+1900;const defaultLabels=Object.freeze({errorMessage:"Please select a valid birthdate.",month:"Month",year:"Year",day:"Day"});const FIELD_MIN_WIDTH_FULL=110;const FIELD_MIN_WIDTH_MONTH_YEAR=167;const FIELD_MIN_WIDTH_DAY=100;const xsMin="520px";const screenSizes={small:`@media (max-width: ${xsMin})`};const defaultStyles=StyleSheet.create({wrapper:{flexDirection:"row",[screenSizes.small]:{flexDirection:"column"}},input:{[screenSizes.small]:{minWidth:"100%"}}});class BirthdayPicker extends React.Component{getStateFromDefault(){const{defaultValue,monthYearOnly}=this.props;const initialState={month:null,day:monthYearOnly?"1":null,year:null,error:null};this.labels={...defaultLabels,...this.props.labels};if(defaultValue){let date=null;try{date=Temporal.PlainDate.from(defaultValue);}catch(err){initialState.error=this.labels.errorMessage;return initialState}if(monthYearOnly){date=date.with({day:date.daysInMonth});}initialState.month=String(date.month);initialState.day=String(date.day);initialState.year=String(date.year);if(this.isFutureDate(date)){initialState.error=this.labels.errorMessage;}}return initialState}isFutureDate(date){return Temporal.PlainDate.compare(date,Temporal.Now.plainDateISO())===1}maybeRenderError(){const{error}=this.state;if(!error){return null}return jsxs(Fragment,{children:[jsx(Strut,{size:spacing.xxxSmall_4}),jsxs(View,{style:{flexDirection:"row",placeItems:"center"},role:"alert",children:[jsx(PhosphorIcon,{size:"small",icon:infoIcon,color:semanticColor.icon.destructive,"aria-hidden":"true"}),jsx(Strut,{size:spacing.xxxSmall_4}),jsx(Body,{style:{color:semanticColor.status.critical.foreground},children:error})]})]})}monthsShort(){const format=new Intl.DateTimeFormat(navigator.language,{month:"short"}).format;return [...Array(12).keys()].map(m=>format(new Date(2021,m,15)))}renderMonth(){const{disabled,monthYearOnly,dropdownStyle}=this.props;const{month}=this.state;const minWidth=this.getMonthYearWidth(monthYearOnly);return jsx(SingleSelect,{"aria-label":this.labels.month,"aria-invalid":!!this.state.error,error:!!this.state.error,disabled:disabled,placeholder:this.labels.month,onChange:this.handleMonthChange,selectedValue:month,style:[{minWidth},defaultStyles.input,dropdownStyle],testId:"birthday-picker-month",children:this.monthsShort().map((monthShort,i)=>jsx(OptionItem,{label:monthShort,value:String(i+1)},monthShort))})}maybeRenderDay(){const{disabled,monthYearOnly,dropdownStyle}=this.props;const{day}=this.state;if(monthYearOnly){return null}return jsxs(Fragment,{children:[jsx(Strut,{size:spacing.xSmall_8}),jsx(SingleSelect,{"aria-label":this.labels.day,"aria-invalid":!!this.state.error,error:!!this.state.error,disabled:disabled,placeholder:this.labels.day,onChange:this.handleDayChange,selectedValue:day,style:[{minWidth:FIELD_MIN_WIDTH_DAY},defaultStyles.input,dropdownStyle],testId:"birthday-picker-day",children:Array.from(Array(31)).map((_,day)=>jsx(OptionItem,{label:String(day+1),value:String(day+1)},String(day+1)))})]})}getMonthYearWidth(monthYearOnly){return monthYearOnly?FIELD_MIN_WIDTH_MONTH_YEAR:FIELD_MIN_WIDTH_FULL}renderYear(){const{disabled,monthYearOnly,dropdownStyle}=this.props;const{year}=this.state;const minWidth=this.getMonthYearWidth(monthYearOnly);return jsx(SingleSelect,{"aria-label":this.labels.year,"aria-invalid":!!this.state.error,error:!!this.state.error,disabled:disabled,placeholder:this.labels.year,onChange:this.handleYearChange,selectedValue:year,style:[{minWidth},defaultStyles.input,dropdownStyle],dropdownStyle:{minWidth:150},testId:"birthday-picker-year",children:Array.from(Array(120)).map((_,yearOffset)=>jsx(OptionItem,{label:String(CUR_YEAR-yearOffset),value:String(CUR_YEAR-yearOffset)},String(CUR_YEAR-yearOffset)))})}render(){const{style}=this.props;return jsxs(Fragment,{children:[jsxs(View,{testId:"birthday-picker",style:[defaultStyles.wrapper,style],children:[this.renderMonth(),this.maybeRenderDay(),jsx(Strut,{size:spacing.xSmall_8}),this.renderYear()]}),this.maybeRenderError()]})}constructor(props){super(props),this.lastChangeValue=null,this.reportChange=value=>{if(value!==this.lastChangeValue){this.lastChangeValue=value;this.props.onChange(value);}},this.handleChange=()=>{const{month,day,year}=this.state;const{monthYearOnly}=this.props;const dateFields=[year,month];if(!monthYearOnly){dateFields.push(day);}if(dateFields.some(field=>field===null)){this.reportChange(null);return}let date;try{if(monthYearOnly){date=Temporal.PlainDate.from({year:Number(year),month:Number(month),day:31});}else {date=Temporal.PlainDate.from({year:Number(year),month:Number(month),day:Number(day)},{overflow:"reject"});}}catch(err){this.setState({error:this.labels.errorMessage});this.reportChange(null);return}if(this.isFutureDate(date)){this.setState({error:this.labels.errorMessage});this.reportChange(null);}else {this.setState({error:null});this.reportChange(date.toString());}},this.handleMonthChange=month=>{this.setState({month},this.handleChange);},this.handleDayChange=day=>{this.setState({day},this.handleChange);},this.handleYearChange=year=>{this.setState({year},this.handleChange);};this.lastChangeValue=props.defaultValue||null;this.state=this.getStateFromDefault();}}
|
|
299
14
|
|
|
300
15
|
export { BirthdayPicker as default };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
4
|
var temporalPolyfill = require('temporal-polyfill');
|
|
5
5
|
var React = require('react');
|
|
6
6
|
var aphrodite = require('aphrodite');
|
|
@@ -32,295 +32,9 @@ function _interopNamespace(e) {
|
|
|
32
32
|
return Object.freeze(n);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
|
|
36
35
|
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
37
36
|
var infoIcon__default = /*#__PURE__*/_interopDefaultLegacy(infoIcon);
|
|
38
37
|
|
|
39
|
-
const CUR_YEAR = new Date().
|
|
40
|
-
const defaultLabels = Object.freeze({
|
|
41
|
-
errorMessage: "Please select a valid birthdate.",
|
|
42
|
-
month: "Month",
|
|
43
|
-
year: "Year",
|
|
44
|
-
day: "Day"
|
|
45
|
-
});
|
|
46
|
-
const FIELD_MIN_WIDTH_FULL = 110;
|
|
47
|
-
const FIELD_MIN_WIDTH_MONTH_YEAR = 167;
|
|
48
|
-
const FIELD_MIN_WIDTH_DAY = 100;
|
|
49
|
-
const xsMin = "520px";
|
|
50
|
-
const screenSizes = {
|
|
51
|
-
small: `@media (max-width: ${xsMin})`
|
|
52
|
-
};
|
|
53
|
-
const defaultStyles = aphrodite.StyleSheet.create({
|
|
54
|
-
wrapper: {
|
|
55
|
-
flexDirection: "row",
|
|
56
|
-
[screenSizes.small]: {
|
|
57
|
-
flexDirection: "column"
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
input: {
|
|
61
|
-
[screenSizes.small]: {
|
|
62
|
-
minWidth: "100%"
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
class BirthdayPicker extends React__namespace.Component {
|
|
67
|
-
constructor(props) {
|
|
68
|
-
super(props);
|
|
69
|
-
this.labels = void 0;
|
|
70
|
-
this.lastChangeValue = null;
|
|
71
|
-
this.reportChange = value => {
|
|
72
|
-
if (value !== this.lastChangeValue) {
|
|
73
|
-
this.lastChangeValue = value;
|
|
74
|
-
this.props.onChange(value);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
this.handleChange = () => {
|
|
78
|
-
const {
|
|
79
|
-
month,
|
|
80
|
-
day,
|
|
81
|
-
year
|
|
82
|
-
} = this.state;
|
|
83
|
-
const {
|
|
84
|
-
monthYearOnly
|
|
85
|
-
} = this.props;
|
|
86
|
-
const dateFields = [year, month];
|
|
87
|
-
if (!monthYearOnly) {
|
|
88
|
-
dateFields.push(day);
|
|
89
|
-
}
|
|
90
|
-
if (dateFields.some(field => field === null)) {
|
|
91
|
-
this.reportChange(null);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
let date;
|
|
95
|
-
try {
|
|
96
|
-
if (monthYearOnly) {
|
|
97
|
-
date = temporalPolyfill.Temporal.PlainDate.from({
|
|
98
|
-
year: Number(year),
|
|
99
|
-
month: Number(month),
|
|
100
|
-
day: 31
|
|
101
|
-
});
|
|
102
|
-
} else {
|
|
103
|
-
date = temporalPolyfill.Temporal.PlainDate.from({
|
|
104
|
-
year: Number(year),
|
|
105
|
-
month: Number(month),
|
|
106
|
-
day: Number(day)
|
|
107
|
-
}, {
|
|
108
|
-
overflow: "reject"
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
} catch (err) {
|
|
112
|
-
this.setState({
|
|
113
|
-
error: this.labels.errorMessage
|
|
114
|
-
});
|
|
115
|
-
this.reportChange(null);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (this.isFutureDate(date)) {
|
|
119
|
-
this.setState({
|
|
120
|
-
error: this.labels.errorMessage
|
|
121
|
-
});
|
|
122
|
-
this.reportChange(null);
|
|
123
|
-
} else {
|
|
124
|
-
this.setState({
|
|
125
|
-
error: null
|
|
126
|
-
});
|
|
127
|
-
this.reportChange(date.toString());
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
this.handleMonthChange = month => {
|
|
131
|
-
this.setState({
|
|
132
|
-
month
|
|
133
|
-
}, this.handleChange);
|
|
134
|
-
};
|
|
135
|
-
this.handleDayChange = day => {
|
|
136
|
-
this.setState({
|
|
137
|
-
day
|
|
138
|
-
}, this.handleChange);
|
|
139
|
-
};
|
|
140
|
-
this.handleYearChange = year => {
|
|
141
|
-
this.setState({
|
|
142
|
-
year
|
|
143
|
-
}, this.handleChange);
|
|
144
|
-
};
|
|
145
|
-
this.lastChangeValue = props.defaultValue || null;
|
|
146
|
-
this.state = this.getStateFromDefault();
|
|
147
|
-
}
|
|
148
|
-
getStateFromDefault() {
|
|
149
|
-
const {
|
|
150
|
-
defaultValue,
|
|
151
|
-
monthYearOnly
|
|
152
|
-
} = this.props;
|
|
153
|
-
const initialState = {
|
|
154
|
-
month: null,
|
|
155
|
-
day: monthYearOnly ? "1" : null,
|
|
156
|
-
year: null,
|
|
157
|
-
error: null
|
|
158
|
-
};
|
|
159
|
-
this.labels = _extends__default["default"]({}, defaultLabels, this.props.labels);
|
|
160
|
-
if (defaultValue) {
|
|
161
|
-
let date = null;
|
|
162
|
-
try {
|
|
163
|
-
date = temporalPolyfill.Temporal.PlainDate.from(defaultValue);
|
|
164
|
-
} catch (err) {
|
|
165
|
-
initialState.error = this.labels.errorMessage;
|
|
166
|
-
return initialState;
|
|
167
|
-
}
|
|
168
|
-
if (monthYearOnly) {
|
|
169
|
-
date = date.with({
|
|
170
|
-
day: date.daysInMonth
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
initialState.month = String(date.month);
|
|
174
|
-
initialState.day = String(date.day);
|
|
175
|
-
initialState.year = String(date.year);
|
|
176
|
-
if (this.isFutureDate(date)) {
|
|
177
|
-
initialState.error = this.labels.errorMessage;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
return initialState;
|
|
181
|
-
}
|
|
182
|
-
isFutureDate(date) {
|
|
183
|
-
return temporalPolyfill.Temporal.PlainDate.compare(date, temporalPolyfill.Temporal.Now.plainDateISO()) === 1;
|
|
184
|
-
}
|
|
185
|
-
maybeRenderError() {
|
|
186
|
-
const {
|
|
187
|
-
error
|
|
188
|
-
} = this.state;
|
|
189
|
-
if (!error) {
|
|
190
|
-
return null;
|
|
191
|
-
}
|
|
192
|
-
return React__namespace.createElement(React__namespace.Fragment, null, React__namespace.createElement(wonderBlocksLayout.Strut, {
|
|
193
|
-
size: wonderBlocksTokens.spacing.xxxSmall_4
|
|
194
|
-
}), React__namespace.createElement(wonderBlocksCore.View, {
|
|
195
|
-
style: {
|
|
196
|
-
flexDirection: "row",
|
|
197
|
-
placeItems: "center"
|
|
198
|
-
},
|
|
199
|
-
role: "alert"
|
|
200
|
-
}, React__namespace.createElement(wonderBlocksIcon.PhosphorIcon, {
|
|
201
|
-
size: "small",
|
|
202
|
-
icon: infoIcon__default["default"],
|
|
203
|
-
color: wonderBlocksTokens.semanticColor.icon.destructive,
|
|
204
|
-
"aria-hidden": "true"
|
|
205
|
-
}), React__namespace.createElement(wonderBlocksLayout.Strut, {
|
|
206
|
-
size: wonderBlocksTokens.spacing.xxxSmall_4
|
|
207
|
-
}), React__namespace.createElement(wonderBlocksTypography.Body, {
|
|
208
|
-
style: {
|
|
209
|
-
color: wonderBlocksTokens.semanticColor.status.critical.foreground
|
|
210
|
-
}
|
|
211
|
-
}, error)));
|
|
212
|
-
}
|
|
213
|
-
monthsShort() {
|
|
214
|
-
const format = new Intl.DateTimeFormat(navigator.language, {
|
|
215
|
-
month: "short"
|
|
216
|
-
}).format;
|
|
217
|
-
return [...Array(12).keys()].map(m => format(new Date(2021, m, 15)));
|
|
218
|
-
}
|
|
219
|
-
renderMonth() {
|
|
220
|
-
const {
|
|
221
|
-
disabled,
|
|
222
|
-
monthYearOnly,
|
|
223
|
-
dropdownStyle
|
|
224
|
-
} = this.props;
|
|
225
|
-
const {
|
|
226
|
-
month
|
|
227
|
-
} = this.state;
|
|
228
|
-
const minWidth = this.getMonthYearWidth(monthYearOnly);
|
|
229
|
-
return React__namespace.createElement(wonderBlocksDropdown.SingleSelect, {
|
|
230
|
-
"aria-label": this.labels.month,
|
|
231
|
-
"aria-invalid": !!this.state.error,
|
|
232
|
-
error: !!this.state.error,
|
|
233
|
-
disabled: disabled,
|
|
234
|
-
placeholder: this.labels.month,
|
|
235
|
-
onChange: this.handleMonthChange,
|
|
236
|
-
selectedValue: month,
|
|
237
|
-
style: [{
|
|
238
|
-
minWidth
|
|
239
|
-
}, defaultStyles.input, dropdownStyle],
|
|
240
|
-
testId: "birthday-picker-month"
|
|
241
|
-
}, this.monthsShort().map((monthShort, i) => React__namespace.createElement(wonderBlocksDropdown.OptionItem, {
|
|
242
|
-
key: monthShort,
|
|
243
|
-
label: monthShort,
|
|
244
|
-
value: String(i + 1)
|
|
245
|
-
})));
|
|
246
|
-
}
|
|
247
|
-
maybeRenderDay() {
|
|
248
|
-
const {
|
|
249
|
-
disabled,
|
|
250
|
-
monthYearOnly,
|
|
251
|
-
dropdownStyle
|
|
252
|
-
} = this.props;
|
|
253
|
-
const {
|
|
254
|
-
day
|
|
255
|
-
} = this.state;
|
|
256
|
-
if (monthYearOnly) {
|
|
257
|
-
return null;
|
|
258
|
-
}
|
|
259
|
-
return React__namespace.createElement(React__namespace.Fragment, null, React__namespace.createElement(wonderBlocksLayout.Strut, {
|
|
260
|
-
size: wonderBlocksTokens.spacing.xSmall_8
|
|
261
|
-
}), React__namespace.createElement(wonderBlocksDropdown.SingleSelect, {
|
|
262
|
-
"aria-label": this.labels.day,
|
|
263
|
-
"aria-invalid": !!this.state.error,
|
|
264
|
-
error: !!this.state.error,
|
|
265
|
-
disabled: disabled,
|
|
266
|
-
placeholder: this.labels.day,
|
|
267
|
-
onChange: this.handleDayChange,
|
|
268
|
-
selectedValue: day,
|
|
269
|
-
style: [{
|
|
270
|
-
minWidth: FIELD_MIN_WIDTH_DAY
|
|
271
|
-
}, defaultStyles.input, dropdownStyle],
|
|
272
|
-
testId: "birthday-picker-day"
|
|
273
|
-
}, Array.from(Array(31)).map((_, day) => React__namespace.createElement(wonderBlocksDropdown.OptionItem, {
|
|
274
|
-
key: String(day + 1),
|
|
275
|
-
label: String(day + 1),
|
|
276
|
-
value: String(day + 1)
|
|
277
|
-
}))));
|
|
278
|
-
}
|
|
279
|
-
getMonthYearWidth(monthYearOnly) {
|
|
280
|
-
return monthYearOnly ? FIELD_MIN_WIDTH_MONTH_YEAR : FIELD_MIN_WIDTH_FULL;
|
|
281
|
-
}
|
|
282
|
-
renderYear() {
|
|
283
|
-
const {
|
|
284
|
-
disabled,
|
|
285
|
-
monthYearOnly,
|
|
286
|
-
dropdownStyle
|
|
287
|
-
} = this.props;
|
|
288
|
-
const {
|
|
289
|
-
year
|
|
290
|
-
} = this.state;
|
|
291
|
-
const minWidth = this.getMonthYearWidth(monthYearOnly);
|
|
292
|
-
return React__namespace.createElement(wonderBlocksDropdown.SingleSelect, {
|
|
293
|
-
"aria-label": this.labels.year,
|
|
294
|
-
"aria-invalid": !!this.state.error,
|
|
295
|
-
error: !!this.state.error,
|
|
296
|
-
disabled: disabled,
|
|
297
|
-
placeholder: this.labels.year,
|
|
298
|
-
onChange: this.handleYearChange,
|
|
299
|
-
selectedValue: year,
|
|
300
|
-
style: [{
|
|
301
|
-
minWidth
|
|
302
|
-
}, defaultStyles.input, dropdownStyle],
|
|
303
|
-
dropdownStyle: {
|
|
304
|
-
minWidth: 150
|
|
305
|
-
},
|
|
306
|
-
testId: "birthday-picker-year"
|
|
307
|
-
}, Array.from(Array(120)).map((_, yearOffset) => React__namespace.createElement(wonderBlocksDropdown.OptionItem, {
|
|
308
|
-
key: String(CUR_YEAR - yearOffset),
|
|
309
|
-
label: String(CUR_YEAR - yearOffset),
|
|
310
|
-
value: String(CUR_YEAR - yearOffset)
|
|
311
|
-
})));
|
|
312
|
-
}
|
|
313
|
-
render() {
|
|
314
|
-
const {
|
|
315
|
-
style
|
|
316
|
-
} = this.props;
|
|
317
|
-
return React__namespace.createElement(React__namespace.Fragment, null, React__namespace.createElement(wonderBlocksCore.View, {
|
|
318
|
-
testId: "birthday-picker",
|
|
319
|
-
style: [defaultStyles.wrapper, style]
|
|
320
|
-
}, this.renderMonth(), this.maybeRenderDay(), React__namespace.createElement(wonderBlocksLayout.Strut, {
|
|
321
|
-
size: wonderBlocksTokens.spacing.xSmall_8
|
|
322
|
-
}), this.renderYear()), this.maybeRenderError());
|
|
323
|
-
}
|
|
324
|
-
}
|
|
38
|
+
const CUR_YEAR=new Date().getYear()+1900;const defaultLabels=Object.freeze({errorMessage:"Please select a valid birthdate.",month:"Month",year:"Year",day:"Day"});const FIELD_MIN_WIDTH_FULL=110;const FIELD_MIN_WIDTH_MONTH_YEAR=167;const FIELD_MIN_WIDTH_DAY=100;const xsMin="520px";const screenSizes={small:`@media (max-width: ${xsMin})`};const defaultStyles=aphrodite.StyleSheet.create({wrapper:{flexDirection:"row",[screenSizes.small]:{flexDirection:"column"}},input:{[screenSizes.small]:{minWidth:"100%"}}});class BirthdayPicker extends React__namespace.Component{getStateFromDefault(){const{defaultValue,monthYearOnly}=this.props;const initialState={month:null,day:monthYearOnly?"1":null,year:null,error:null};this.labels={...defaultLabels,...this.props.labels};if(defaultValue){let date=null;try{date=temporalPolyfill.Temporal.PlainDate.from(defaultValue);}catch(err){initialState.error=this.labels.errorMessage;return initialState}if(monthYearOnly){date=date.with({day:date.daysInMonth});}initialState.month=String(date.month);initialState.day=String(date.day);initialState.year=String(date.year);if(this.isFutureDate(date)){initialState.error=this.labels.errorMessage;}}return initialState}isFutureDate(date){return temporalPolyfill.Temporal.PlainDate.compare(date,temporalPolyfill.Temporal.Now.plainDateISO())===1}maybeRenderError(){const{error}=this.state;if(!error){return null}return jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(wonderBlocksLayout.Strut,{size:wonderBlocksTokens.spacing.xxxSmall_4}),jsxRuntime.jsxs(wonderBlocksCore.View,{style:{flexDirection:"row",placeItems:"center"},role:"alert",children:[jsxRuntime.jsx(wonderBlocksIcon.PhosphorIcon,{size:"small",icon:infoIcon__default["default"],color:wonderBlocksTokens.semanticColor.icon.destructive,"aria-hidden":"true"}),jsxRuntime.jsx(wonderBlocksLayout.Strut,{size:wonderBlocksTokens.spacing.xxxSmall_4}),jsxRuntime.jsx(wonderBlocksTypography.Body,{style:{color:wonderBlocksTokens.semanticColor.status.critical.foreground},children:error})]})]})}monthsShort(){const format=new Intl.DateTimeFormat(navigator.language,{month:"short"}).format;return [...Array(12).keys()].map(m=>format(new Date(2021,m,15)))}renderMonth(){const{disabled,monthYearOnly,dropdownStyle}=this.props;const{month}=this.state;const minWidth=this.getMonthYearWidth(monthYearOnly);return jsxRuntime.jsx(wonderBlocksDropdown.SingleSelect,{"aria-label":this.labels.month,"aria-invalid":!!this.state.error,error:!!this.state.error,disabled:disabled,placeholder:this.labels.month,onChange:this.handleMonthChange,selectedValue:month,style:[{minWidth},defaultStyles.input,dropdownStyle],testId:"birthday-picker-month",children:this.monthsShort().map((monthShort,i)=>jsxRuntime.jsx(wonderBlocksDropdown.OptionItem,{label:monthShort,value:String(i+1)},monthShort))})}maybeRenderDay(){const{disabled,monthYearOnly,dropdownStyle}=this.props;const{day}=this.state;if(monthYearOnly){return null}return jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(wonderBlocksLayout.Strut,{size:wonderBlocksTokens.spacing.xSmall_8}),jsxRuntime.jsx(wonderBlocksDropdown.SingleSelect,{"aria-label":this.labels.day,"aria-invalid":!!this.state.error,error:!!this.state.error,disabled:disabled,placeholder:this.labels.day,onChange:this.handleDayChange,selectedValue:day,style:[{minWidth:FIELD_MIN_WIDTH_DAY},defaultStyles.input,dropdownStyle],testId:"birthday-picker-day",children:Array.from(Array(31)).map((_,day)=>jsxRuntime.jsx(wonderBlocksDropdown.OptionItem,{label:String(day+1),value:String(day+1)},String(day+1)))})]})}getMonthYearWidth(monthYearOnly){return monthYearOnly?FIELD_MIN_WIDTH_MONTH_YEAR:FIELD_MIN_WIDTH_FULL}renderYear(){const{disabled,monthYearOnly,dropdownStyle}=this.props;const{year}=this.state;const minWidth=this.getMonthYearWidth(monthYearOnly);return jsxRuntime.jsx(wonderBlocksDropdown.SingleSelect,{"aria-label":this.labels.year,"aria-invalid":!!this.state.error,error:!!this.state.error,disabled:disabled,placeholder:this.labels.year,onChange:this.handleYearChange,selectedValue:year,style:[{minWidth},defaultStyles.input,dropdownStyle],dropdownStyle:{minWidth:150},testId:"birthday-picker-year",children:Array.from(Array(120)).map((_,yearOffset)=>jsxRuntime.jsx(wonderBlocksDropdown.OptionItem,{label:String(CUR_YEAR-yearOffset),value:String(CUR_YEAR-yearOffset)},String(CUR_YEAR-yearOffset)))})}render(){const{style}=this.props;return jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsxs(wonderBlocksCore.View,{testId:"birthday-picker",style:[defaultStyles.wrapper,style],children:[this.renderMonth(),this.maybeRenderDay(),jsxRuntime.jsx(wonderBlocksLayout.Strut,{size:wonderBlocksTokens.spacing.xSmall_8}),this.renderYear()]}),this.maybeRenderError()]})}constructor(props){super(props),this.lastChangeValue=null,this.reportChange=value=>{if(value!==this.lastChangeValue){this.lastChangeValue=value;this.props.onChange(value);}},this.handleChange=()=>{const{month,day,year}=this.state;const{monthYearOnly}=this.props;const dateFields=[year,month];if(!monthYearOnly){dateFields.push(day);}if(dateFields.some(field=>field===null)){this.reportChange(null);return}let date;try{if(monthYearOnly){date=temporalPolyfill.Temporal.PlainDate.from({year:Number(year),month:Number(month),day:31});}else {date=temporalPolyfill.Temporal.PlainDate.from({year:Number(year),month:Number(month),day:Number(day)},{overflow:"reject"});}}catch(err){this.setState({error:this.labels.errorMessage});this.reportChange(null);return}if(this.isFutureDate(date)){this.setState({error:this.labels.errorMessage});this.reportChange(null);}else {this.setState({error:null});this.reportChange(date.toString());}},this.handleMonthChange=month=>{this.setState({month},this.handleChange);},this.handleDayChange=day=>{this.setState({day},this.handleChange);},this.handleYearChange=year=>{this.setState({year},this.handleChange);};this.lastChangeValue=props.defaultValue||null;this.state=this.getStateFromDefault();}}
|
|
325
39
|
|
|
326
40
|
module.exports = BirthdayPicker;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khanacademy/wonder-blocks-birthday-picker",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.3",
|
|
4
4
|
"design": "v1",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -10,13 +10,12 @@
|
|
|
10
10
|
"module": "dist/es/index.js",
|
|
11
11
|
"types": "dist/index.d.ts",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@babel/runtime": "^7.24.5",
|
|
14
13
|
"@khanacademy/wonder-blocks-core": "12.2.1",
|
|
15
|
-
"@khanacademy/wonder-blocks-dropdown": "10.0.
|
|
14
|
+
"@khanacademy/wonder-blocks-dropdown": "10.0.5",
|
|
16
15
|
"@khanacademy/wonder-blocks-icon": "5.1.3",
|
|
17
|
-
"@khanacademy/wonder-blocks-layout": "3.1.
|
|
18
|
-
"@khanacademy/wonder-blocks-tokens": "
|
|
19
|
-
"@khanacademy/wonder-blocks-typography": "3.
|
|
16
|
+
"@khanacademy/wonder-blocks-layout": "3.1.10",
|
|
17
|
+
"@khanacademy/wonder-blocks-tokens": "10.0.0",
|
|
18
|
+
"@khanacademy/wonder-blocks-typography": "3.2.0"
|
|
20
19
|
},
|
|
21
20
|
"peerDependencies": {
|
|
22
21
|
"@phosphor-icons/core": "^2.0.2",
|
|
@@ -25,7 +24,7 @@
|
|
|
25
24
|
"react": "18.2.0"
|
|
26
25
|
},
|
|
27
26
|
"devDependencies": {
|
|
28
|
-
"@khanacademy/wb-dev-build-settings": "
|
|
27
|
+
"@khanacademy/wb-dev-build-settings": "3.0.0"
|
|
29
28
|
},
|
|
30
29
|
"author": "",
|
|
31
30
|
"license": "MIT",
|