-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc396f8
commit fc47c39
Showing
4 changed files
with
142 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { createContext, PropsWithChildren } from 'react'; | ||
import merge from 'lodash/merge'; | ||
|
||
import { formatDate } from './formatDate'; | ||
|
||
export type DateFieldI18n = { | ||
required: string; | ||
invalidInput: string; | ||
minDate: (min: Date, pickTime: boolean) => string; | ||
maxDate: (max: Date, pickTime: boolean) => string; | ||
}; | ||
|
||
export const defaultDateFieldI18n: DateFieldI18n = { | ||
required: 'Field is required', | ||
invalidInput: 'Must be date', | ||
minDate: (min, pickTime) => `Date must not be earlier than ${formatDate(min, pickTime)}`, | ||
maxDate: (max, pickTime) => `Date must not be later than ${formatDate(max, pickTime)}`, | ||
}; | ||
|
||
export const DateFieldI18nContext = createContext<DateFieldI18n>(defaultDateFieldI18n); | ||
|
||
export type DateFieldI18nContextProviderProps = PropsWithChildren<{ i18n?: Partial<DateFieldI18n> }>; | ||
|
||
export const DateFieldI18nContextProvider = ({ i18n, children }: DateFieldI18nContextProviderProps) => { | ||
return ( | ||
<DateFieldI18nContext.Provider value={merge(defaultDateFieldI18n, i18n)}> | ||
{children} | ||
</DateFieldI18nContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import dayjs from 'dayjs'; | ||
|
||
export const formatDate = (value: Date | null | undefined, pickTime: boolean) => { | ||
if (!(value instanceof Date)) { | ||
return ''; | ||
} | ||
|
||
return dayjs(value).format(`YYYY-MM-DD${pickTime ? ' HH:mm' : ''}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.