@swan-io/shared-business 8.14.1 → 8.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-io/shared-business",
3
- "version": "8.14.1",
3
+ "version": "8.14.2",
4
4
  "engines": {
5
5
  "node": ">=20.9.0",
6
6
  "yarn": "^1.22.0"
@@ -5,6 +5,5 @@ export type InlineDatePickerProps = {
5
5
  onValueChange: (value: ExtractedDate) => void;
6
6
  error?: string;
7
7
  onBlur?: () => void;
8
- order: "DMY" | "MDY" | "YMD";
9
8
  };
10
- export declare const InlineDatePicker: ({ value, label, onValueChange, error, onBlur, order, }: InlineDatePickerProps) => import("react/jsx-runtime").JSX.Element;
9
+ export declare const InlineDatePicker: ({ value, label, onValueChange, error, onBlur, }: InlineDatePickerProps) => import("react/jsx-runtime").JSX.Element;
@@ -9,7 +9,8 @@ import { breakpoints, colors } from "@swan-io/lake/src/constants/design";
9
9
  import { useResponsive } from "@swan-io/lake/src/hooks/useResponsive";
10
10
  import { isNotNullish } from "@swan-io/lake/src/utils/nullish";
11
11
  import { StyleSheet, View } from "react-native";
12
- import { t } from "../utils/i18n";
12
+ import { match } from "ts-pattern";
13
+ import { getCountry, t } from "../utils/i18n";
13
14
  const months = [
14
15
  { value: "01", name: t("datePicker.month.january") },
15
16
  { value: "02", name: t("datePicker.month.february") },
@@ -26,7 +27,7 @@ const months = [
26
27
  ];
27
28
  const styles = StyleSheet.create({
28
29
  dayMobile: {
29
- maxWidth: 60,
30
+ maxWidth: 70,
30
31
  flexGrow: 0,
31
32
  },
32
33
  day: {
@@ -45,7 +46,13 @@ const styles = StyleSheet.create({
45
46
  borderColor: colors.negative[400],
46
47
  },
47
48
  });
48
- export const InlineDatePicker = ({ value = { day: "", month: "", year: "" }, label, onValueChange, error, onBlur, order, }) => {
49
+ // https://en.wikipedia.org/wiki/List_of_date_formats_by_country
50
+ const order = match(getCountry().cca2)
51
+ .returnType()
52
+ .with("US", () => "MDY")
53
+ .with("CN", "JP", "KR", "KP", "TW", "HU", "MN", "LT", "BT", () => "YMD")
54
+ .otherwise(() => "DMY");
55
+ export const InlineDatePicker = ({ value = { day: "", month: "", year: "" }, label, onValueChange, error, onBlur, }) => {
49
56
  const { desktop } = useResponsive(breakpoints.small);
50
57
  return (_jsx(LakeLabel, { label: label, render: id => {
51
58
  const day = (_jsx(View, { style: desktop ? styles.day : styles.dayMobile, children: _jsx(LakeTextInput, { id: id, style: isNotNullish(error) && styles.error, placeholder: t("datePicker.day"), value: value.day, onBlur: onBlur, hideErrors: true, onChangeText: day => {
@@ -1,6 +1,7 @@
1
1
  import { RifmProps } from "@swan-io/lake/src/utils/rifm";
2
2
  import { ReactElement, ReactNode } from "react";
3
3
  import type { DateFormat } from "../components/DatePicker";
4
+ import { Country } from "../constants/countries";
4
5
  import translationEN from "../locales/en.json";
5
6
  declare const supportedLanguages: readonly ["en", "es", "de", "fr", "it", "nl", "pt", "fi"];
6
7
  type SupportedLanguage = (typeof supportedLanguages)[number];
@@ -21,4 +22,5 @@ export declare const formatNestedMessage: (key: TranslationKey, params: Record<s
21
22
  export declare const rifmDateProps: RifmProps;
22
23
  export declare const isTranslationKey: (value: unknown) => value is TranslationKey;
23
24
  export declare const translateError: (error: unknown) => string;
25
+ export declare const getCountry: () => Country;
24
26
  export {};
package/src/utils/i18n.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createIntl, createIntlCache } from "@formatjs/intl";
2
+ import { isNotNullish } from "@swan-io/lake/src/utils/nullish";
2
3
  import { getRifmProps } from "@swan-io/lake/src/utils/rifm";
3
4
  import { BadStatusError } from "@swan-io/request";
4
5
  import dayjs from "dayjs";
@@ -16,6 +17,7 @@ import relativeTime from "dayjs/plugin/relativeTime";
16
17
  import utc from "dayjs/plugin/utc";
17
18
  import { cloneElement, isValidElement } from "react";
18
19
  import { P, match } from "ts-pattern";
20
+ import { countries, france } from "../constants/countries";
19
21
  import translationDE from "../locales/de.json";
20
22
  import translationEN from "../locales/en.json";
21
23
  import translationES from "../locales/es.json";
@@ -138,3 +140,17 @@ export const translateError = (error) => {
138
140
  .otherwise(() => "error.generic");
139
141
  return t(isTranslationKey(key) ? key : "error.generic");
140
142
  };
143
+ export const getCountry = () => {
144
+ const navigatorCountries = navigator.languages
145
+ .map(language => language.split("-")[1])
146
+ .filter(isNotNullish);
147
+ for (let index = 0; index < navigatorCountries.length; index++) {
148
+ const navigatorCountry = navigatorCountries[index];
149
+ const country = countries.find(({ cca2 }) => cca2 === navigatorCountry);
150
+ if (isNotNullish(country)) {
151
+ return country;
152
+ }
153
+ }
154
+ // fallback to france when no valid country found in navigator locales
155
+ return france;
156
+ };