From c05cedd9d91f4f8f090a813180b1d4b80ba5cfe5 Mon Sep 17 00:00:00 2001 From: Felix Beceic Date: Mon, 6 Mar 2023 09:29:43 +0100 Subject: [PATCH] #65 Refactor useLabel to exclude Intl if not present --- libs/intl/src/useLabel.tsx | 15 ++++++++++----- storybook/src/intl/Intl.mdx | 7 ------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/libs/intl/src/useLabel.tsx b/libs/intl/src/useLabel.tsx index 4749bd3..1767c76 100644 --- a/libs/intl/src/useLabel.tsx +++ b/libs/intl/src/useLabel.tsx @@ -22,13 +22,18 @@ import { CommonKeys } from "./IntlProvider"; import { useIntlContext } from "./useIntlContext"; export function useLabel(name: keyof CommonKeys): string { - const { dictionary, lang, commonKeys } = useIntlContext(); + const intlContext = useIntlContext(); - const key = commonKeys[name] || name; + if (intlContext) { + const { lang, dictionary, commonKeys } = intlContext; + const key = commonKeys[name] || name; - if (!dictionary?.translations?.[lang]?.[key] && !dictionary?.messages?.[key]) { - return defaultDictionary.translations?.[lang]?.[key] || defaultDictionary.messages[name]; + if (!dictionary?.translations?.[lang]?.[key] && !dictionary?.messages?.[key]) { + return defaultDictionary.translations?.[lang]?.[key] || defaultDictionary.messages[name]; + } + + return (dictionary?.translations?.[lang]?.[key] || dictionary?.messages?.[key]) as string; } - return (dictionary?.translations?.[lang]?.[key] || dictionary?.messages?.[key]) as string; + return "Required field"; } diff --git a/storybook/src/intl/Intl.mdx b/storybook/src/intl/Intl.mdx index faaa6b6..61eeb39 100644 --- a/storybook/src/intl/Intl.mdx +++ b/storybook/src/intl/Intl.mdx @@ -17,7 +17,6 @@ display components which depend on your location. **The list of components which require your app to be wrapped inside `Intl Provider`**: - Amount -- CheckboxGroup(field) - Date - DateInput(field) - DatePicker @@ -25,16 +24,10 @@ display components which depend on your location. - DateTimeInput(field) - TimeInput(field) - DragZone(field) -- Input(field) - Login - Number - NumberInput(field) -- RadioGroup(field) - Select(field) -- Slider(field) -- Textarea(field) -- Toggle(field) -- TreeSelect(field) ## Default Intl Dictionary