-
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
581c7cc
commit a0fa8d2
Showing
3 changed files
with
84 additions
and
77 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,36 @@ | ||
import React, { createContext, PropsWithChildren } from 'react'; | ||
import merge from 'lodash/merge'; | ||
|
||
export const defaultFormat = (value: number | null | undefined, precision: number) => { | ||
if (typeof value !== 'number' || !Number.isFinite(value)) { | ||
return ''; | ||
} | ||
|
||
return value.toFixed(precision).toString(); | ||
}; | ||
|
||
export type DecimalFieldI18n = { | ||
required: string; | ||
invalidInput: string; | ||
minValue: (value: number, precision: number) => string; | ||
maxValue: (value: number, precision: number) => string; | ||
}; | ||
|
||
export const defaultDecimalFieldI18n: DecimalFieldI18n = { | ||
required: 'Field is required', | ||
invalidInput: 'Must be decimal', | ||
minValue: (min: number, precision: number) => `Value should not be less than ${defaultFormat(min, precision)}`, | ||
maxValue: (max: number, precision: number) => `Value should not be greater than ${defaultFormat(max, precision)}`, | ||
}; | ||
|
||
export const DecimalFieldI18nContext = createContext<DecimalFieldI18n>(defaultDecimalFieldI18n); | ||
|
||
export type DecimalFieldI18nContextProviderProps = PropsWithChildren<{ i18n?: Partial<DecimalFieldI18n> }>; | ||
|
||
export const DecimalFieldI18nContextProvider = ({ i18n, children }: DecimalFieldI18nContextProviderProps) => { | ||
return ( | ||
<DecimalFieldI18nContext.Provider value={merge(defaultDecimalFieldI18n, i18n)}> | ||
{children} | ||
</DecimalFieldI18nContext.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
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