Skip to content

Commit

Permalink
feat: date picker component show invalid dates warning (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
oribro authored Jan 16, 2024
1 parent dde15c7 commit aac6720
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/locale/he.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"bug_type_bug": "באג",
"bug_type_feature": "בקשה לפיתוח",
"bug_date_alert": "תאריך הסיום אינו יכול להיות מוקדם יותר מתאריך ההתחלה",
"bug_date_invalid_format": "נא להזין תאריך בפורמט תקין",
"funding_paragraph": "דאטאבוס פותח בסדנא לידע ציבורי, בעבודת מתנדבים, ומבוסס על",
"halufa_ride": "נסיעה חלופית מס",
"all_rides_completed": "כמעט / כל הנסיעות בוצעו",
Expand Down
27 changes: 20 additions & 7 deletions src/pages/components/DateSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { useState } from 'react'
import { useState, useMemo } from 'react'
import { DatePicker } from '@mui/x-date-pickers/DatePicker'
import { useTranslation } from 'react-i18next'
import { DataAndTimeSelectorProps } from './utils/dateAndTime'
import { DateValidationError } from '@mui/x-date-pickers'
import styled from 'styled-components'

const Error = styled.div`
color: 'red';
`

export function DateSelector({ time, onChange, customLabel }: DataAndTimeSelectorProps) {
const [error, setError] = useState<DateValidationError | null>(null)
const { t } = useTranslation()

const errorMessage = useMemo(() => {
switch (error) {
case 'maxDate':
case 'minDate':
case 'invalidDate': {
return t('bug_date_invalid_format')
}

default: {
return ''
}
}
}, [error])

return (
<>
<DatePicker
Expand All @@ -23,8 +32,12 @@ export function DateSelector({ time, onChange, customLabel }: DataAndTimeSelecto
label={customLabel || t('choose_date')}
disableFuture
onError={(err) => setError(err)}
slotProps={{
textField: {
helperText: errorMessage,
},
}}
/>
{error && <Error>{error}</Error>}
</>
)
}

0 comments on commit aac6720

Please sign in to comment.