Skip to content

Commit

Permalink
Fix some issues in DateTimePicker component
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Apr 30, 2024
1 parent 1165e60 commit 74d37e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
23 changes: 19 additions & 4 deletions backend/src/components/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
import { DateTimePicker as MuiDateTimePicker } from '@mui/x-date-pickers/DateTimePicker'
import { fr, enUS } from 'date-fns/locale'
import { TextFieldVariants } from '@mui/material'
import { DateTimeValidationError } from '@mui/x-date-pickers'
import { DateTimeValidationError, PickersActionBarAction } from '@mui/x-date-pickers'

interface DateTimePickerProps {
value?: Date
Expand All @@ -15,6 +15,7 @@ interface DateTimePickerProps {
language?: string
variant?: TextFieldVariants
readOnly?: boolean
showClear?: boolean
onChange?: (value: Date | null) => void
onError?: (error: DateTimeValidationError, value: Date | null) => void
}
Expand All @@ -28,6 +29,7 @@ const DateTimePicker = ({
variant,
language,
readOnly,
showClear,
onChange,
onError
}: DateTimePickerProps) => {
Expand All @@ -37,6 +39,12 @@ const DateTimePicker = ({
setValue(dateTimeValue || null)
}, [dateTimeValue])

const actions: PickersActionBarAction[] = ['accept', 'cancel']

if (showClear) {
actions.push('clear')
}

return (
<LocalizationProvider adapterLocale={language === 'fr' ? fr : enUS} dateAdapter={AdapterDateFns}>
<MuiDateTimePicker
Expand All @@ -50,8 +58,15 @@ const DateTimePicker = ({
onChange(_value)
}

if (_value && minDate && _value < minDate && onError) {
onError('minDate', _value)
if (_value && minDate) {
const val = new Date(_value)
val.setHours(0, 0, 0, 0)
const min = new Date(minDate)
min.setHours(0, 0, 0, 0)

if (val < min && onError) {
onError('minDate', _value)
}
}
}}
onError={onError}
Expand All @@ -64,7 +79,7 @@ const DateTimePicker = ({
required,
},
actionBar: {
actions: ['accept', 'cancel', 'clear'],
actions,
},
}}
/>
Expand Down
23 changes: 19 additions & 4 deletions frontend/src/components/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
import { DateTimePicker as MuiDateTimePicker } from '@mui/x-date-pickers/DateTimePicker'
import { fr, enUS } from 'date-fns/locale'
import { TextFieldVariants } from '@mui/material'
import { DateTimeValidationError } from '@mui/x-date-pickers'
import { DateTimeValidationError, PickersActionBarAction } from '@mui/x-date-pickers'

interface DateTimePickerProps {
value?: Date
Expand All @@ -15,6 +15,7 @@ interface DateTimePickerProps {
language?: string
variant?: TextFieldVariants
readOnly?: boolean
showClear?: boolean
onChange?: (value: Date | null) => void
onError?: (error: DateTimeValidationError, value: Date | null) => void
}
Expand All @@ -28,6 +29,7 @@ const DateTimePicker = ({
variant,
language,
readOnly,
showClear,
onChange,
onError
}: DateTimePickerProps) => {
Expand All @@ -37,6 +39,12 @@ const DateTimePicker = ({
setValue(dateTimeValue || null)
}, [dateTimeValue])

const actions: PickersActionBarAction[] = ['accept', 'cancel']

if (showClear) {
actions.push('clear')
}

return (
<LocalizationProvider adapterLocale={language === 'fr' ? fr : enUS} dateAdapter={AdapterDateFns}>
<MuiDateTimePicker
Expand All @@ -50,8 +58,15 @@ const DateTimePicker = ({
onChange(_value)
}

if (_value && minDate && _value < minDate && onError) {
onError('minDate', _value)
if (_value && minDate) {
const val = new Date(_value)
val.setHours(0, 0, 0, 0)
const min = new Date(minDate)
min.setHours(0, 0, 0, 0)

if (val < min && onError) {
onError('minDate', _value)
}
}
}}
onError={onError}
Expand All @@ -64,7 +79,7 @@ const DateTimePicker = ({
required,
},
actionBar: {
actions: ['accept', 'cancel', 'clear'],
actions,
},
}}
/>
Expand Down

0 comments on commit 74d37e7

Please sign in to comment.