Skip to content

Commit

Permalink
fix: String to object conversion in Date Pickers
Browse files Browse the repository at this point in the history
  • Loading branch information
sadik-malik committed Mar 10, 2024
1 parent 4eda2c0 commit 90d0c60
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
12 changes: 4 additions & 8 deletions packages/rhf-mui/src/DatePickerElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
validateDate,
} from '@mui/x-date-pickers/internals'
import useTransform from './useTransform'
import {getTimezone} from './utils'

export type DatePickerElementProps<
TFieldValues extends FieldValues = FieldValues,
Expand Down Expand Up @@ -101,11 +102,6 @@ const DatePickerElement = forwardRef(function DatePickerElement<
}),
validate: {
internal: (value: TValue | null) => {
const inputTimezone =
value == null || !adapter.utils.isValid(value)
? null
: adapter.utils.getTimezone(value)

const internalError = validateDate({
props: {
shouldDisableDate: rest.shouldDisableDate,
Expand All @@ -115,7 +111,7 @@ const DatePickerElement = forwardRef(function DatePickerElement<
disableFuture: Boolean(rest.disableFuture),
minDate: rest.minDate,
maxDate: rest.maxDate,
timezone: rest.timezone ?? inputTimezone ?? 'default',
timezone: rest.timezone ?? getTimezone(adapter, value) ?? 'default',
},
value,
adapter,
Expand Down Expand Up @@ -145,8 +141,8 @@ const DatePickerElement = forwardRef(function DatePickerElement<
typeof transform?.input === 'function'
? transform.input
: (newValue) => {
return newValue && newValue === 'string'
? (new Date(newValue) as TValue) // need to see if this works for all localization adaptors
return newValue && typeof newValue === 'string'
? (adapter.utils.date(newValue) as TValue) // need to see if this works for all localization adaptors
: newValue
},
output:
Expand Down
12 changes: 4 additions & 8 deletions packages/rhf-mui/src/DateTimePickerElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
DateTimeValidationError,
PickerChangeHandlerContext,
} from '@mui/x-date-pickers'
import {getTimezone} from './utils'

export type DateTimePickerElementProps<
TFieldValues extends FieldValues = FieldValues,
Expand Down Expand Up @@ -100,11 +101,6 @@ const DateTimePickerElement = forwardRef(function DateTimePickerElement<
}),
validate: {
internal: (value: TValue | null) => {
const inputTimezone =
value == null || !adapter.utils.isValid(value)
? null
: adapter.utils.getTimezone(value)

const internalError = validateDateTime({
props: {
shouldDisableDate: rest.shouldDisableDate,
Expand All @@ -114,7 +110,7 @@ const DateTimePickerElement = forwardRef(function DateTimePickerElement<
disableFuture: Boolean(rest.disableFuture),
minDate: rest.minDate,
maxDate: rest.maxDate,
timezone: rest.timezone ?? inputTimezone ?? 'default',
timezone: rest.timezone ?? getTimezone(adapter, value) ?? 'default',
disableIgnoringDatePartForTimeValidation:
rest.disableIgnoringDatePartForTimeValidation,
maxTime: rest.maxTime,
Expand Down Expand Up @@ -151,8 +147,8 @@ const DateTimePickerElement = forwardRef(function DateTimePickerElement<
typeof transform?.input === 'function'
? transform.input
: (newValue) => {
return newValue && newValue === 'string'
? (new Date(newValue) as TValue) // need to see if this works for all localization adaptors
return newValue && typeof newValue === 'string'
? (adapter.utils.date(newValue) as TValue) // need to see if this works for all localization adaptors
: newValue
},
output:
Expand Down
10 changes: 3 additions & 7 deletions packages/rhf-mui/src/MobileDatePickerElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
DateValidationError,
PickerChangeHandlerContext,
} from '@mui/x-date-pickers'
import {getTimezone} from './utils'

export type MobileDatePickerElementProps<
TFieldValues extends FieldValues = FieldValues,
Expand Down Expand Up @@ -99,11 +100,6 @@ const MobileDatePickerElement = forwardRef(function MobileDatePickerElement<
}),
validate: {
internal: (value: TValue | null) => {
const inputTimezone =
value == null || !adapter.utils.isValid(value)
? null
: adapter.utils.getTimezone(value)

const internalError = validateDate({
props: {
shouldDisableDate: rest.shouldDisableDate,
Expand All @@ -113,7 +109,7 @@ const MobileDatePickerElement = forwardRef(function MobileDatePickerElement<
disableFuture: Boolean(rest.disableFuture),
minDate: rest.minDate,
maxDate: rest.maxDate,
timezone: rest.timezone ?? inputTimezone ?? 'default',
timezone: rest.timezone ?? getTimezone(adapter, value) ?? 'default',
},
value,
adapter,
Expand Down Expand Up @@ -144,7 +140,7 @@ const MobileDatePickerElement = forwardRef(function MobileDatePickerElement<
? transform.input
: (newValue) => {
return newValue && typeof newValue === 'string'
? (new Date(newValue) as TValue) // need to see if this works for all localization adaptors
? (adapter.utils.date(newValue) as TValue) // need to see if this works for all localization adaptors
: newValue
},
output:
Expand Down
10 changes: 3 additions & 7 deletions packages/rhf-mui/src/TimePickerElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
PickerChangeHandlerContext,
TimeValidationError,
} from '@mui/x-date-pickers'
import {getTimezone} from './utils'

export type TimePickerElementProps<
TFieldValues extends FieldValues = FieldValues,
Expand Down Expand Up @@ -100,11 +101,6 @@ const TimePickerElement = forwardRef(function TimePickerElement<
}),
validate: {
internal: (value: TValue | null) => {
const inputTimezone =
value == null || !adapter.utils.isValid(value)
? null
: adapter.utils.getTimezone(value)

const internalError = validateTime({
props: {
minTime: rest.minTime,
Expand All @@ -116,7 +112,7 @@ const TimePickerElement = forwardRef(function TimePickerElement<
rest.disableIgnoringDatePartForTimeValidation,
disablePast: Boolean(rest.disablePast),
disableFuture: Boolean(rest.disableFuture),
timezone: rest.timezone ?? inputTimezone ?? 'default',
timezone: rest.timezone ?? getTimezone(adapter, value) ?? 'default',
},
value,
adapter,
Expand Down Expand Up @@ -147,7 +143,7 @@ const TimePickerElement = forwardRef(function TimePickerElement<
? transform.input
: (newValue) => {
return newValue && typeof newValue === 'string'
? (new Date(newValue) as TValue) // need to see if this works for all localization adaptors
? (adapter.utils.date(newValue) as TValue) // need to see if this works for all localization adaptors
: newValue
},
output:
Expand Down
13 changes: 12 additions & 1 deletion packages/rhf-mui/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import {useLocalizationContext} from '@mui/x-date-pickers/internals'

export function hasOwnProperty<X, Y extends PropertyKey>(
obj: X,
prop: Y
): obj is X & Record<Y, unknown> {
return (
typeof obj === 'object' &&
obj !== null &&
Object.hasOwnProperty.call(obj, prop)
Object.prototype.hasOwnProperty.call(obj, prop)
)
}

export function getTimezone<TDate>(
adapter: ReturnType<typeof useLocalizationContext>,
value: TDate | null
): string | null {
return value == null || !adapter.utils.isValid(value)
? null
: adapter.utils.getTimezone(value)
}

0 comments on commit 90d0c60

Please sign in to comment.