@applica-software-guru/react-admin 1.3.151 → 1.3.152

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.
@@ -1,11 +1,18 @@
1
+ type ILocale = {
2
+ name: string;
3
+ locale: string;
4
+ };
1
5
  type ILocalizedValue<T> = {
2
6
  [key: string]: T;
3
7
  };
4
- declare function localizedValueHasAllLocales<T>(value?: ILocalizedValue<T>, locales?: Array<{
5
- name: string;
6
- locale: string;
7
- }>, options?: {
8
+ declare function localizedValueHasAllLocales<T>(value?: ILocalizedValue<T>, locales?: Array<ILocale>, options?: {
8
9
  isEmpty?: (v: T) => boolean;
9
10
  }): boolean;
10
- export { localizedValueHasAllLocales };
11
+ type IGetLocalizedValueOptions = {
12
+ fallback?: boolean;
13
+ fallbackLocales?: Array<string>;
14
+ };
15
+ declare function getLocalizedValue<T>(value: ILocalizedValue<T>, locale: string, options?: IGetLocalizedValueOptions): T | undefined;
16
+ export type { ILocalizedValue, IGetLocalizedValueOptions };
17
+ export { localizedValueHasAllLocales, getLocalizedValue };
11
18
  //# sourceMappingURL=localizedValue.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"localizedValue.d.ts","sourceRoot":"","sources":["../../../src/utils/localizedValue.ts"],"names":[],"mappings":"AACA,KAAK,eAAe,CAAC,CAAC,IAAI;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,CAAA;CAAE,CAAC;AAE/C,iBAAS,2BAA2B,CAAC,CAAC,EACpC,KAAK,GAAE,eAAe,CAAC,CAAC,CAAM,EAC9B,OAAO,GAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAM,EACrD,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,CAAA;CAAO,GAC5C,OAAO,CAIT;AAED,OAAO,EAAE,2BAA2B,EAAE,CAAC"}
1
+ {"version":3,"file":"localizedValue.d.ts","sourceRoot":"","sources":["../../../src/utils/localizedValue.ts"],"names":[],"mappings":"AACA,KAAK,OAAO,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AACF,KAAK,eAAe,CAAC,CAAC,IAAI;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,CAAA;CAAE,CAAC;AAE/C,iBAAS,2BAA2B,CAAC,CAAC,EACpC,KAAK,GAAE,eAAe,CAAC,CAAC,CAAM,EAC9B,OAAO,GAAE,KAAK,CAAC,OAAO,CAAM,EAC5B,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,CAAA;CAAO,GAC5C,OAAO,CAIT;AAED,KAAK,yBAAyB,GAAG;IAC/B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,eAAe,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;CACjC,CAAC;AACF,iBAAS,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB,GAAG,CAAC,GAAG,SAAS,CAwB3H;AAED,YAAY,EAAE,eAAe,EAAE,yBAAyB,EAAE,CAAC;AAC3D,OAAO,EAAE,2BAA2B,EAAE,iBAAiB,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applica-software-guru/react-admin",
3
- "version": "1.3.151",
3
+ "version": "1.3.152",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,3 +21,4 @@ export {
21
21
  export { useSx } from './mui';
22
22
  export { useRefDimensions } from './useRefDimensions';
23
23
  export { useMemoizedObject } from './useMemoizedObject';
24
+ export { useLocalizedValue, useGetLocalizedValue } from './useLocalizedValue';
@@ -0,0 +1,34 @@
1
+ import { useLocaleState } from 'ra-core';
2
+ import { IGetLocalizedValueOptions, ILocalizedValue, getLocalizedValue } from '../utils';
3
+ import { useCallback } from 'react';
4
+
5
+ function useLocale(locale?: string): string {
6
+ const [defaultLocale] = useLocaleState();
7
+
8
+ return locale !== undefined ? String(locale) : defaultLocale;
9
+ }
10
+
11
+ type IUseLocalizeValueOptions = IGetLocalizedValueOptions & {
12
+ locale?: string;
13
+ };
14
+ function useLocalizedValue<T>(value: ILocalizedValue<T>, options?: IUseLocalizeValueOptions): T | undefined {
15
+ const language = useLocale(options?.locale),
16
+ localizedValue = getLocalizedValue<T>(value, language, options);
17
+
18
+ return localizedValue;
19
+ }
20
+
21
+ type IUseGetLocalizeValueOptions = IUseLocalizeValueOptions;
22
+ function useGetLocalizedValue<T>(options?: IUseGetLocalizeValueOptions): (v: ILocalizedValue<T>) => T | undefined {
23
+ const language = useLocale(options?.locale),
24
+ callback = useCallback(
25
+ (value: ILocalizedValue<T>) => {
26
+ return getLocalizedValue<T>(value, language, options);
27
+ },
28
+ [language, options]
29
+ );
30
+
31
+ return callback;
32
+ }
33
+
34
+ export { useLocalizedValue, useGetLocalizedValue };
@@ -1,9 +1,13 @@
1
1
  import _ from 'lodash';
2
+ type ILocale = {
3
+ name: string;
4
+ locale: string;
5
+ };
2
6
  type ILocalizedValue<T> = { [key: string]: T };
3
7
 
4
8
  function localizedValueHasAllLocales<T>(
5
9
  value: ILocalizedValue<T> = {},
6
- locales: Array<{ name: string; locale: string }> = [],
10
+ locales: Array<ILocale> = [],
7
11
  options: { isEmpty?: (v: T) => boolean } = {}
8
12
  ): boolean {
9
13
  const { isEmpty = _.isEmpty } = options;
@@ -11,4 +15,35 @@ function localizedValueHasAllLocales<T>(
11
15
  return !_.some(locales, (l) => isEmpty((value ?? {})[l.locale]));
12
16
  }
13
17
 
14
- export { localizedValueHasAllLocales };
18
+ type IGetLocalizedValueOptions = {
19
+ fallback?: boolean;
20
+ fallbackLocales?: Array<string>;
21
+ };
22
+ function getLocalizedValue<T>(value: ILocalizedValue<T>, locale: string, options?: IGetLocalizedValueOptions): T | undefined {
23
+ if (!_.isPlainObject(value) || locale === undefined) {
24
+ return undefined;
25
+ }
26
+
27
+ const _options: IGetLocalizedValueOptions = _.isPlainObject(options) ? (options as IGetLocalizedValueOptions) : {},
28
+ fallback = Boolean(_options?.fallback ?? true);
29
+
30
+ let localizedValue: T | undefined = value[String(locale)];
31
+
32
+ if (localizedValue === undefined && fallback) {
33
+ const fallbackLocales = _.isArray(_options.fallbackLocales) && !_.isEmpty(_options.fallbackLocales) ? _options.fallbackLocales : ['en'];
34
+ localizedValue = _.chain(fallbackLocales)
35
+ .map((l) => value[String(l)])
36
+ .reject(_.isUndefined)
37
+ .first()
38
+ .value() as T | undefined;
39
+
40
+ if (localizedValue === undefined) {
41
+ localizedValue = _.chain(value).values().first().value() as T | undefined;
42
+ }
43
+ }
44
+
45
+ return localizedValue;
46
+ }
47
+
48
+ export type { ILocalizedValue, IGetLocalizedValueOptions };
49
+ export { localizedValueHasAllLocales, getLocalizedValue };