@gateweb/react-utils 1.10.0 → 1.12.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/dist/cjs/index.d.ts +45 -3
- package/dist/cjs/index.js +66 -1
- package/dist/es/index.d.mts +45 -3
- package/dist/es/index.mjs +65 -2
- package/package.json +2 -1
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
2
|
import { AtLeastOne } from './types.js';
|
|
3
3
|
export * from './types.js';
|
|
4
4
|
|
|
@@ -404,6 +404,7 @@ declare const extractEnumLikeObject: <T extends Record<string, { [P in K]: any;
|
|
|
404
404
|
*
|
|
405
405
|
* @param obj 要處理的物件
|
|
406
406
|
* @param name 生成的 enum 名稱
|
|
407
|
+
* @param scene 要使用的條件 (參考 example)
|
|
407
408
|
*
|
|
408
409
|
* @example
|
|
409
410
|
*
|
|
@@ -506,8 +507,12 @@ declare const validateFileType: (file: File, accepts: string[]) => boolean;
|
|
|
506
507
|
* getMimeType('txt') // 'text/plain'
|
|
507
508
|
* getMimeType('zip') // 'application/zip'
|
|
508
509
|
* getMimeType('mp4') // 'application/octet-stream'
|
|
510
|
+
* getMimeType('xls') // 'application/vnd.ms-excel'
|
|
511
|
+
* getMimeType('xlsx') // 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
512
|
+
* getMimeType('doc') // 'application/msword'
|
|
513
|
+
* getMimeType('docx') // 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
509
514
|
*/
|
|
510
|
-
declare const getMimeType: (fileName: string) => "image/jpeg" | "image/png" | "application/pdf" | "application/zip" | "text/csv" | "text/plain" | "application/octet-stream";
|
|
515
|
+
declare const getMimeType: (fileName: string) => "image/jpeg" | "image/png" | "application/pdf" | "application/zip" | "text/csv" | "application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "application/msword" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | "text/plain" | "application/octet-stream";
|
|
511
516
|
|
|
512
517
|
declare function invariant(condition: any, message?: string | (() => string)): asserts condition;
|
|
513
518
|
|
|
@@ -974,6 +979,43 @@ declare const QueryProvider: <Q>({ children, query, handleChangeQuery, }: React.
|
|
|
974
979
|
*/
|
|
975
980
|
declare const useQueryContext: <Q>() => <T>(selector: (state: TQueryState<Q>) => T, equalityFn?: (left: T, right: T) => boolean) => T;
|
|
976
981
|
|
|
982
|
+
/**
|
|
983
|
+
* Creates a strongly-typed React Context and Provider pair for data sharing.
|
|
984
|
+
*
|
|
985
|
+
* This utility helps you avoid prop drilling by providing a reusable way to define
|
|
986
|
+
* context with strict type inference. It returns a custom hook for consuming the context
|
|
987
|
+
* and a Provider component for supplying context values.
|
|
988
|
+
*
|
|
989
|
+
* @template T - The value type for the context.
|
|
990
|
+
* @returns {Object} An object containing:
|
|
991
|
+
* - useDataContext: A custom hook to access the context value. Throws an error if used outside the provider.
|
|
992
|
+
* - DataProvider: A Provider component to wrap your component tree and supply the context value.
|
|
993
|
+
*
|
|
994
|
+
* @example
|
|
995
|
+
* // Example usage:
|
|
996
|
+
* const { useDataContext, DataProvider } = createDataContext<{ count: number }>();
|
|
997
|
+
*
|
|
998
|
+
* function Counter() {
|
|
999
|
+
* const { count } = useDataContext();
|
|
1000
|
+
* return <span>{count}</span>;
|
|
1001
|
+
* }
|
|
1002
|
+
*
|
|
1003
|
+
* function App() {
|
|
1004
|
+
* return (
|
|
1005
|
+
* <DataProvider value={{ count: 42 }}>
|
|
1006
|
+
* <Counter />
|
|
1007
|
+
* </DataProvider>
|
|
1008
|
+
* );
|
|
1009
|
+
* }
|
|
1010
|
+
*/
|
|
1011
|
+
declare const createDataContext: <T>() => {
|
|
1012
|
+
readonly useDataContext: () => T & ({} | null);
|
|
1013
|
+
readonly DataProvider: ({ children, value }: {
|
|
1014
|
+
children: ReactNode;
|
|
1015
|
+
value: T;
|
|
1016
|
+
}) => React.JSX.Element;
|
|
1017
|
+
};
|
|
1018
|
+
|
|
977
1019
|
type TCountdownActions = {
|
|
978
1020
|
/** 目前秒數 */
|
|
979
1021
|
countdown: number;
|
|
@@ -1107,4 +1149,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
1107
1149
|
*/
|
|
1108
1150
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
1109
1151
|
|
|
1110
|
-
export { ByteSize, type PartialBy, QueryProvider, type RequiredBy, type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createEnumLikeObject, debounce, deepMerge, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeRefs, objectToSearchParams, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1152
|
+
export { ByteSize, type PartialBy, QueryProvider, type RequiredBy, type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepMerge, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeRefs, objectToSearchParams, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
package/dist/cjs/index.js
CHANGED
|
@@ -2,14 +2,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
2
2
|
|
|
3
3
|
var dayjs = require('dayjs');
|
|
4
4
|
var queryStoreClient = require('./queryStore-client-q_SLGgYH.js');
|
|
5
|
-
var useCountdownClient = require('./useCountdown-client-uiqhgllY.js');
|
|
6
5
|
var React = require('react');
|
|
6
|
+
var useCountdownClient = require('./useCountdown-client-uiqhgllY.js');
|
|
7
7
|
var downloadClient = require('./download-client-DKxkL92w.js');
|
|
8
8
|
var webStorageClient = require('./webStorage-client-BGQKUfrO.js');
|
|
9
9
|
|
|
10
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
|
|
12
12
|
var dayjs__default = /*#__PURE__*/_interopDefault(dayjs);
|
|
13
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
13
14
|
|
|
14
15
|
const FILE_SIZE_UNITS$1 = [
|
|
15
16
|
'Bytes',
|
|
@@ -286,6 +287,10 @@ function createEnumLikeObject(obj, name, scene) {
|
|
|
286
287
|
* getMimeType('txt') // 'text/plain'
|
|
287
288
|
* getMimeType('zip') // 'application/zip'
|
|
288
289
|
* getMimeType('mp4') // 'application/octet-stream'
|
|
290
|
+
* getMimeType('xls') // 'application/vnd.ms-excel'
|
|
291
|
+
* getMimeType('xlsx') // 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
292
|
+
* getMimeType('doc') // 'application/msword'
|
|
293
|
+
* getMimeType('docx') // 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
289
294
|
*/ const getMimeType = (fileName)=>{
|
|
290
295
|
switch((fileName.split('.').pop() || '').toLocaleLowerCase()){
|
|
291
296
|
case 'jpeg':
|
|
@@ -299,6 +304,14 @@ function createEnumLikeObject(obj, name, scene) {
|
|
|
299
304
|
return 'application/zip';
|
|
300
305
|
case 'csv':
|
|
301
306
|
return 'text/csv';
|
|
307
|
+
case 'xls':
|
|
308
|
+
return 'application/vnd.ms-excel';
|
|
309
|
+
case 'xlsx':
|
|
310
|
+
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
311
|
+
case 'doc':
|
|
312
|
+
return 'application/msword';
|
|
313
|
+
case 'docx':
|
|
314
|
+
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
|
302
315
|
case 'txt':
|
|
303
316
|
return 'text/plain';
|
|
304
317
|
default:
|
|
@@ -1043,6 +1056,57 @@ const FILE_SIZE_UNITS = [
|
|
|
1043
1056
|
return dayjs__default.default(dateString, format, true).isValid();
|
|
1044
1057
|
};
|
|
1045
1058
|
|
|
1059
|
+
/**
|
|
1060
|
+
* Creates a strongly-typed React Context and Provider pair for data sharing.
|
|
1061
|
+
*
|
|
1062
|
+
* This utility helps you avoid prop drilling by providing a reusable way to define
|
|
1063
|
+
* context with strict type inference. It returns a custom hook for consuming the context
|
|
1064
|
+
* and a Provider component for supplying context values.
|
|
1065
|
+
*
|
|
1066
|
+
* @template T - The value type for the context.
|
|
1067
|
+
* @returns {Object} An object containing:
|
|
1068
|
+
* - useDataContext: A custom hook to access the context value. Throws an error if used outside the provider.
|
|
1069
|
+
* - DataProvider: A Provider component to wrap your component tree and supply the context value.
|
|
1070
|
+
*
|
|
1071
|
+
* @example
|
|
1072
|
+
* // Example usage:
|
|
1073
|
+
* const { useDataContext, DataProvider } = createDataContext<{ count: number }>();
|
|
1074
|
+
*
|
|
1075
|
+
* function Counter() {
|
|
1076
|
+
* const { count } = useDataContext();
|
|
1077
|
+
* return <span>{count}</span>;
|
|
1078
|
+
* }
|
|
1079
|
+
*
|
|
1080
|
+
* function App() {
|
|
1081
|
+
* return (
|
|
1082
|
+
* <DataProvider value={{ count: 42 }}>
|
|
1083
|
+
* <Counter />
|
|
1084
|
+
* </DataProvider>
|
|
1085
|
+
* );
|
|
1086
|
+
* }
|
|
1087
|
+
*/ const createDataContext = ()=>{
|
|
1088
|
+
const Context = /*#__PURE__*/ React.createContext(undefined);
|
|
1089
|
+
const useDataContext = ()=>{
|
|
1090
|
+
const context = React.useContext(Context);
|
|
1091
|
+
if (context === undefined) {
|
|
1092
|
+
throw new Error(`useDataContext must be used within a DataProvider`);
|
|
1093
|
+
}
|
|
1094
|
+
return context;
|
|
1095
|
+
};
|
|
1096
|
+
const DataProvider = ({ children, value })=>{
|
|
1097
|
+
const memoized = React.useMemo(()=>value, [
|
|
1098
|
+
value
|
|
1099
|
+
]);
|
|
1100
|
+
return /*#__PURE__*/ React__default.default.createElement(Context.Provider, {
|
|
1101
|
+
value: memoized
|
|
1102
|
+
}, children);
|
|
1103
|
+
};
|
|
1104
|
+
return {
|
|
1105
|
+
useDataContext,
|
|
1106
|
+
DataProvider
|
|
1107
|
+
};
|
|
1108
|
+
};
|
|
1109
|
+
|
|
1046
1110
|
/**
|
|
1047
1111
|
* A hook to manage a value.
|
|
1048
1112
|
*
|
|
@@ -1180,6 +1244,7 @@ exports.camelCase2SnakeCase = camelCase2SnakeCase;
|
|
|
1180
1244
|
exports.camelString2PascalString = camelString2PascalString;
|
|
1181
1245
|
exports.camelString2SnakeString = camelString2SnakeString;
|
|
1182
1246
|
exports.convertBytes = convertBytes;
|
|
1247
|
+
exports.createDataContext = createDataContext;
|
|
1183
1248
|
exports.createEnumLikeObject = createEnumLikeObject;
|
|
1184
1249
|
exports.debounce = debounce;
|
|
1185
1250
|
exports.deepMerge = deepMerge;
|
package/dist/es/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
2
|
import { AtLeastOne } from './types.mjs';
|
|
3
3
|
export * from './types.mjs';
|
|
4
4
|
|
|
@@ -404,6 +404,7 @@ declare const extractEnumLikeObject: <T extends Record<string, { [P in K]: any;
|
|
|
404
404
|
*
|
|
405
405
|
* @param obj 要處理的物件
|
|
406
406
|
* @param name 生成的 enum 名稱
|
|
407
|
+
* @param scene 要使用的條件 (參考 example)
|
|
407
408
|
*
|
|
408
409
|
* @example
|
|
409
410
|
*
|
|
@@ -506,8 +507,12 @@ declare const validateFileType: (file: File, accepts: string[]) => boolean;
|
|
|
506
507
|
* getMimeType('txt') // 'text/plain'
|
|
507
508
|
* getMimeType('zip') // 'application/zip'
|
|
508
509
|
* getMimeType('mp4') // 'application/octet-stream'
|
|
510
|
+
* getMimeType('xls') // 'application/vnd.ms-excel'
|
|
511
|
+
* getMimeType('xlsx') // 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
512
|
+
* getMimeType('doc') // 'application/msword'
|
|
513
|
+
* getMimeType('docx') // 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
509
514
|
*/
|
|
510
|
-
declare const getMimeType: (fileName: string) => "image/jpeg" | "image/png" | "application/pdf" | "application/zip" | "text/csv" | "text/plain" | "application/octet-stream";
|
|
515
|
+
declare const getMimeType: (fileName: string) => "image/jpeg" | "image/png" | "application/pdf" | "application/zip" | "text/csv" | "application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "application/msword" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | "text/plain" | "application/octet-stream";
|
|
511
516
|
|
|
512
517
|
declare function invariant(condition: any, message?: string | (() => string)): asserts condition;
|
|
513
518
|
|
|
@@ -974,6 +979,43 @@ declare const QueryProvider: <Q>({ children, query, handleChangeQuery, }: React.
|
|
|
974
979
|
*/
|
|
975
980
|
declare const useQueryContext: <Q>() => <T>(selector: (state: TQueryState<Q>) => T, equalityFn?: (left: T, right: T) => boolean) => T;
|
|
976
981
|
|
|
982
|
+
/**
|
|
983
|
+
* Creates a strongly-typed React Context and Provider pair for data sharing.
|
|
984
|
+
*
|
|
985
|
+
* This utility helps you avoid prop drilling by providing a reusable way to define
|
|
986
|
+
* context with strict type inference. It returns a custom hook for consuming the context
|
|
987
|
+
* and a Provider component for supplying context values.
|
|
988
|
+
*
|
|
989
|
+
* @template T - The value type for the context.
|
|
990
|
+
* @returns {Object} An object containing:
|
|
991
|
+
* - useDataContext: A custom hook to access the context value. Throws an error if used outside the provider.
|
|
992
|
+
* - DataProvider: A Provider component to wrap your component tree and supply the context value.
|
|
993
|
+
*
|
|
994
|
+
* @example
|
|
995
|
+
* // Example usage:
|
|
996
|
+
* const { useDataContext, DataProvider } = createDataContext<{ count: number }>();
|
|
997
|
+
*
|
|
998
|
+
* function Counter() {
|
|
999
|
+
* const { count } = useDataContext();
|
|
1000
|
+
* return <span>{count}</span>;
|
|
1001
|
+
* }
|
|
1002
|
+
*
|
|
1003
|
+
* function App() {
|
|
1004
|
+
* return (
|
|
1005
|
+
* <DataProvider value={{ count: 42 }}>
|
|
1006
|
+
* <Counter />
|
|
1007
|
+
* </DataProvider>
|
|
1008
|
+
* );
|
|
1009
|
+
* }
|
|
1010
|
+
*/
|
|
1011
|
+
declare const createDataContext: <T>() => {
|
|
1012
|
+
readonly useDataContext: () => T & ({} | null);
|
|
1013
|
+
readonly DataProvider: ({ children, value }: {
|
|
1014
|
+
children: ReactNode;
|
|
1015
|
+
value: T;
|
|
1016
|
+
}) => React.JSX.Element;
|
|
1017
|
+
};
|
|
1018
|
+
|
|
977
1019
|
type TCountdownActions = {
|
|
978
1020
|
/** 目前秒數 */
|
|
979
1021
|
countdown: number;
|
|
@@ -1107,4 +1149,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
1107
1149
|
*/
|
|
1108
1150
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
1109
1151
|
|
|
1110
|
-
export { ByteSize, type PartialBy, QueryProvider, type RequiredBy, type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createEnumLikeObject, debounce, deepMerge, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeRefs, objectToSearchParams, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1152
|
+
export { ByteSize, type PartialBy, QueryProvider, type RequiredBy, type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepMerge, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeRefs, objectToSearchParams, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
package/dist/es/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import dayjs from 'dayjs';
|
|
2
2
|
export { Q as QueryProvider, u as useQueryContext } from './queryStore-client-vG-bXFYm.mjs';
|
|
3
|
+
import React, { useContext, useMemo, createContext, useState, useCallback } from 'react';
|
|
3
4
|
export { u as useCountdown } from './useCountdown-client-t52WIHfq.mjs';
|
|
4
|
-
import { useState, useCallback } from 'react';
|
|
5
5
|
export { d as downloadFile } from './download-client-CnaJ0p_f.mjs';
|
|
6
6
|
export { g as getLocalStorage, s as setLocalStorage } from './webStorage-client-Pd-loNCg.mjs';
|
|
7
7
|
|
|
@@ -280,6 +280,10 @@ function createEnumLikeObject(obj, name, scene) {
|
|
|
280
280
|
* getMimeType('txt') // 'text/plain'
|
|
281
281
|
* getMimeType('zip') // 'application/zip'
|
|
282
282
|
* getMimeType('mp4') // 'application/octet-stream'
|
|
283
|
+
* getMimeType('xls') // 'application/vnd.ms-excel'
|
|
284
|
+
* getMimeType('xlsx') // 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
285
|
+
* getMimeType('doc') // 'application/msword'
|
|
286
|
+
* getMimeType('docx') // 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
283
287
|
*/ const getMimeType = (fileName)=>{
|
|
284
288
|
switch((fileName.split('.').pop() || '').toLocaleLowerCase()){
|
|
285
289
|
case 'jpeg':
|
|
@@ -293,6 +297,14 @@ function createEnumLikeObject(obj, name, scene) {
|
|
|
293
297
|
return 'application/zip';
|
|
294
298
|
case 'csv':
|
|
295
299
|
return 'text/csv';
|
|
300
|
+
case 'xls':
|
|
301
|
+
return 'application/vnd.ms-excel';
|
|
302
|
+
case 'xlsx':
|
|
303
|
+
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
304
|
+
case 'doc':
|
|
305
|
+
return 'application/msword';
|
|
306
|
+
case 'docx':
|
|
307
|
+
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
|
296
308
|
case 'txt':
|
|
297
309
|
return 'text/plain';
|
|
298
310
|
default:
|
|
@@ -1037,6 +1049,57 @@ const FILE_SIZE_UNITS = [
|
|
|
1037
1049
|
return dayjs(dateString, format, true).isValid();
|
|
1038
1050
|
};
|
|
1039
1051
|
|
|
1052
|
+
/**
|
|
1053
|
+
* Creates a strongly-typed React Context and Provider pair for data sharing.
|
|
1054
|
+
*
|
|
1055
|
+
* This utility helps you avoid prop drilling by providing a reusable way to define
|
|
1056
|
+
* context with strict type inference. It returns a custom hook for consuming the context
|
|
1057
|
+
* and a Provider component for supplying context values.
|
|
1058
|
+
*
|
|
1059
|
+
* @template T - The value type for the context.
|
|
1060
|
+
* @returns {Object} An object containing:
|
|
1061
|
+
* - useDataContext: A custom hook to access the context value. Throws an error if used outside the provider.
|
|
1062
|
+
* - DataProvider: A Provider component to wrap your component tree and supply the context value.
|
|
1063
|
+
*
|
|
1064
|
+
* @example
|
|
1065
|
+
* // Example usage:
|
|
1066
|
+
* const { useDataContext, DataProvider } = createDataContext<{ count: number }>();
|
|
1067
|
+
*
|
|
1068
|
+
* function Counter() {
|
|
1069
|
+
* const { count } = useDataContext();
|
|
1070
|
+
* return <span>{count}</span>;
|
|
1071
|
+
* }
|
|
1072
|
+
*
|
|
1073
|
+
* function App() {
|
|
1074
|
+
* return (
|
|
1075
|
+
* <DataProvider value={{ count: 42 }}>
|
|
1076
|
+
* <Counter />
|
|
1077
|
+
* </DataProvider>
|
|
1078
|
+
* );
|
|
1079
|
+
* }
|
|
1080
|
+
*/ const createDataContext = ()=>{
|
|
1081
|
+
const Context = /*#__PURE__*/ createContext(undefined);
|
|
1082
|
+
const useDataContext = ()=>{
|
|
1083
|
+
const context = useContext(Context);
|
|
1084
|
+
if (context === undefined) {
|
|
1085
|
+
throw new Error(`useDataContext must be used within a DataProvider`);
|
|
1086
|
+
}
|
|
1087
|
+
return context;
|
|
1088
|
+
};
|
|
1089
|
+
const DataProvider = ({ children, value })=>{
|
|
1090
|
+
const memoized = useMemo(()=>value, [
|
|
1091
|
+
value
|
|
1092
|
+
]);
|
|
1093
|
+
return /*#__PURE__*/ React.createElement(Context.Provider, {
|
|
1094
|
+
value: memoized
|
|
1095
|
+
}, children);
|
|
1096
|
+
};
|
|
1097
|
+
return {
|
|
1098
|
+
useDataContext,
|
|
1099
|
+
DataProvider
|
|
1100
|
+
};
|
|
1101
|
+
};
|
|
1102
|
+
|
|
1040
1103
|
/**
|
|
1041
1104
|
* A hook to manage a value.
|
|
1042
1105
|
*
|
|
@@ -1161,4 +1224,4 @@ function mergeRefs(refs) {
|
|
|
1161
1224
|
return dayjs(endMonth, 'YYYYMM').subtract(1911, 'year').format('YYYYMM').substring(1);
|
|
1162
1225
|
};
|
|
1163
1226
|
|
|
1164
|
-
export { ByteSize, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createEnumLikeObject, debounce, deepMerge, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeRefs, objectToSearchParams, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1227
|
+
export { ByteSize, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, deepMerge, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeRefs, objectToSearchParams, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, searchParamsToObject, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useValue, validTaxId, validateDateString, validateFileType, wait };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gateweb/react-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.2",
|
|
4
4
|
"description": "React Utils for GateWeb",
|
|
5
5
|
"homepage": "https://github.com/GatewebSolutions/react-utils",
|
|
6
6
|
"files": [
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"@commitlint/cli": "^19.5.0",
|
|
46
46
|
"@commitlint/config-conventional": "^19.5.0",
|
|
47
47
|
"@gateweb/eslint-config-gateweb": "^1.0.6",
|
|
48
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
48
49
|
"@testing-library/react": "^16.2.0",
|
|
49
50
|
"@types/jest": "^29.5.13",
|
|
50
51
|
"@types/node": "^22.7.7",
|