2525import {
2626 useState ,
2727 useEffect ,
28- useContext ,
2928 forwardRef ,
3029 ForwardedRef ,
3130 ValidationMap
@@ -41,8 +40,7 @@ import {
4140import { Popover } from '@instructure/ui-popover'
4241import { TextInput } from '@instructure/ui-text-input'
4342import { callRenderProp , passthroughProps } from '@instructure/ui-react-utils'
44-
45- import { ApplyLocaleContext , Locale } from '@instructure/ui-i18n'
43+ import { getLocale , getTimezone } from '@instructure/ui-i18n'
4644
4745import { propTypes } from './props'
4846import type { DateInput2Props } from './props'
@@ -93,11 +91,6 @@ function parseLocaleDate(
9391 // create utc date from year, month (zero indexed) and day
9492 const date = new Date ( Date . UTC ( year , month - 1 , day ) )
9593
96- if ( date . getMonth ( ) !== month - 1 || date . getDate ( ) !== day ) {
97- // Check if the Date object adjusts the values. If it does, the input is invalid.
98- return null
99- }
100-
10194 // Format date string in the provided timezone. The locale here is irrelevant, we only care about how to time is adjusted for the timezone.
10295 const parts = new Intl . DateTimeFormat ( 'en-US' , {
10396 timeZone,
@@ -166,27 +159,8 @@ const DateInput2 = forwardRef(
166159 } : DateInput2Props ,
167160 ref : ForwardedRef < TextInput >
168161 ) => {
169- const localeContext = useContext ( ApplyLocaleContext )
170-
171- const getLocale = ( ) => {
172- if ( locale ) {
173- return locale
174- } else if ( localeContext . locale ) {
175- return localeContext . locale
176- }
177- // default to the system's locale
178- return Locale . browserLocale ( )
179- }
180-
181- const getTimezone = ( ) => {
182- if ( timezone ) {
183- return timezone
184- } else if ( localeContext . timezone ) {
185- return localeContext . timezone
186- }
187- // default to the system's timezone
188- return Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone
189- }
162+ const userLocale = locale || getLocale ( )
163+ const userTimezone = timezone || getTimezone ( )
190164
191165 const [ inputMessages , setInputMessages ] = useState < FormMessage [ ] > (
192166 messages || [ ]
@@ -213,28 +187,28 @@ const DateInput2 = forwardRef(
213187 if ( dateFormat ) {
214188 if ( typeof dateFormat === 'string' ) {
215189 // use dateFormat instead of the user locale
216- date = parseLocaleDate ( dateString , dateFormat , getTimezone ( ) )
190+ date = parseLocaleDate ( dateString , dateFormat , userTimezone )
217191 } else if ( dateFormat . parser ) {
218192 date = dateFormat . parser ( dateString )
219193 }
220194 } else {
221195 // no dateFormat prop passed, use locale for formatting
222- date = parseLocaleDate ( dateString , getLocale ( ) , getTimezone ( ) )
196+ date = parseLocaleDate ( dateString , userLocale , userTimezone )
223197 }
224198 return date ? [ formatDate ( date ) , date . toISOString ( ) ] : [ '' , '' ]
225199 }
226200
227201 const formatDate = (
228202 date : Date ,
229- timeZone : string = getTimezone ( )
203+ timeZone : string = userTimezone
230204 ) : string => {
231205 // use formatter function if provided
232206 if ( typeof dateFormat !== 'string' && dateFormat ?. formatter ) {
233207 return dateFormat . formatter ( date )
234208 }
235209 // if dateFormat set to a locale, use that, otherwise default to the user's locale
236210 return date . toLocaleDateString (
237- typeof dateFormat === 'string' ? dateFormat : getLocale ( ) ,
211+ typeof dateFormat === 'string' ? dateFormat : userLocale ,
238212 {
239213 timeZone,
240214 calendar : 'gregory' ,
@@ -253,9 +227,9 @@ const DateInput2 = forwardRef(
253227 }
254228
255229 // Replace the matched number with the same number of dashes
256- const year = ` ${ exampleDate . getFullYear ( ) } `
257- const month = ` ${ exampleDate . getMonth ( ) + 1 } `
258- const day = ` ${ exampleDate . getDate ( ) } `
230+ const year = '2024'
231+ const month = '9'
232+ const day = '1'
259233 return formattedDate
260234 . replace ( regex ( year ) , ( match ) => 'Y' . repeat ( match . length ) )
261235 . replace ( regex ( month ) , ( match ) => 'M' . repeat ( match . length ) )
@@ -340,8 +314,8 @@ const DateInput2 = forwardRef(
340314 selectedDate = { selectedDate }
341315 disabledDates = { disabledDates }
342316 visibleMonth = { selectedDate }
343- locale = { getLocale ( ) }
344- timezone = { getTimezone ( ) }
317+ locale = { userLocale }
318+ timezone = { userTimezone }
345319 renderNextMonthButton = {
346320 < IconButton
347321 size = "small"
0 commit comments