@gateweb/react-utils 1.4.0 → 1.4.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.
@@ -1,4 +1,4 @@
1
- import { TExtractValueType } from './types.js';
1
+ import { TExtractValueType, AtLeastOne } from './types.js';
2
2
  export * from './types.js';
3
3
  import React from 'react';
4
4
 
@@ -347,6 +347,21 @@ type TCountdownActions = {
347
347
  */
348
348
  declare const useCountdown: (initialCountdown: number, enableReinitialize?: boolean) => TCountdownActions;
349
349
 
350
+ type TValueOptions<T> = AtLeastOne<{
351
+ /**
352
+ * The controlled value.
353
+ */
354
+ value?: T;
355
+ /**
356
+ * The default value.
357
+ */
358
+ defaultValue?: T;
359
+ }>;
360
+ /**
361
+ *
362
+ */
363
+ declare const useValue: <T>({ value, defaultValue }: TValueOptions<T>) => readonly [T, (newValue: T) => void];
364
+
350
365
  declare function invariant(condition: any, message?: string | (() => string)): asserts condition;
351
366
 
352
367
  /**
@@ -789,4 +804,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
789
804
  */
790
805
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
791
806
 
792
- export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, mergeRefs, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, validTaxId, validateDateString, validateFileType, wait };
807
+ export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, mergeRefs, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useValue, validTaxId, validateDateString, validateFileType, wait };
package/dist/cjs/index.js CHANGED
@@ -2,7 +2,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
3
  var dayjs = require('dayjs');
4
4
  var useCountdownClient = require('./useCountdown-client-CNjGBIUB.js');
5
- require('react');
5
+ var react = require('react');
6
6
  var downloadClient = require('./download-client-DKxkL92w.js');
7
7
  var webStorageClient = require('./webStorage-client-BGQKUfrO.js');
8
8
 
@@ -473,6 +473,28 @@ const transformObjectKey = (obj, transformFunName)=>{
473
473
  }
474
474
  };
475
475
 
476
+ /**
477
+ *
478
+ */ const useValue = ({ value, defaultValue })=>{
479
+ if (value === undefined && defaultValue === undefined) {
480
+ throw new Error('Either `value` or `defaultValue` must be provided.');
481
+ }
482
+ const isControlled = value !== undefined;
483
+ const [internalValue, setInternalValue] = react.useState(defaultValue);
484
+ const setValue = react.useCallback((newValue)=>{
485
+ if (!isControlled) {
486
+ setInternalValue(newValue);
487
+ }
488
+ }, [
489
+ isControlled
490
+ ]);
491
+ const currentValue = isControlled ? value : internalValue;
492
+ return [
493
+ currentValue,
494
+ setValue
495
+ ];
496
+ };
497
+
476
498
  // const isProduction: boolean = process.env.NODE_ENV === 'production';
477
499
  const prefix = 'Invariant failed';
478
500
  // Throw an error if the condition fails
@@ -926,6 +948,7 @@ exports.snakeCase2PascalCase = snakeCase2PascalCase;
926
948
  exports.snakeString2CamelString = snakeString2CamelString;
927
949
  exports.snakeString2PascalString = snakeString2PascalString;
928
950
  exports.throttle = throttle;
951
+ exports.useValue = useValue;
929
952
  exports.validTaxId = validTaxId;
930
953
  exports.validateDateString = validateDateString;
931
954
  exports.validateFileType = validateFileType;
@@ -1,4 +1,4 @@
1
- import { TExtractValueType } from './types.mjs';
1
+ import { TExtractValueType, AtLeastOne } from './types.mjs';
2
2
  export * from './types.mjs';
3
3
  import React from 'react';
4
4
 
@@ -347,6 +347,21 @@ type TCountdownActions = {
347
347
  */
348
348
  declare const useCountdown: (initialCountdown: number, enableReinitialize?: boolean) => TCountdownActions;
349
349
 
350
+ type TValueOptions<T> = AtLeastOne<{
351
+ /**
352
+ * The controlled value.
353
+ */
354
+ value?: T;
355
+ /**
356
+ * The default value.
357
+ */
358
+ defaultValue?: T;
359
+ }>;
360
+ /**
361
+ *
362
+ */
363
+ declare const useValue: <T>({ value, defaultValue }: TValueOptions<T>) => readonly [T, (newValue: T) => void];
364
+
350
365
  declare function invariant(condition: any, message?: string | (() => string)): asserts condition;
351
366
 
352
367
  /**
@@ -789,4 +804,4 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
789
804
  */
790
805
  declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
791
806
 
792
- export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, mergeRefs, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, validTaxId, validateDateString, validateFileType, wait };
807
+ export { type TCountdownActions, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, downloadFile, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, mergeRefs, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useValue, validTaxId, validateDateString, validateFileType, wait };
package/dist/es/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import dayjs from 'dayjs';
2
2
  export { u as useCountdown } from './useCountdown-client-t52WIHfq.mjs';
3
- import 'react';
3
+ import { useState, useCallback } from 'react';
4
4
  export { d as downloadFile } from './download-client-CnaJ0p_f.mjs';
5
5
  export { g as getLocalStorage, s as setLocalStorage } from './webStorage-client-Pd-loNCg.mjs';
6
6
 
@@ -467,6 +467,28 @@ const transformObjectKey = (obj, transformFunName)=>{
467
467
  }
468
468
  };
469
469
 
470
+ /**
471
+ *
472
+ */ const useValue = ({ value, defaultValue })=>{
473
+ if (value === undefined && defaultValue === undefined) {
474
+ throw new Error('Either `value` or `defaultValue` must be provided.');
475
+ }
476
+ const isControlled = value !== undefined;
477
+ const [internalValue, setInternalValue] = useState(defaultValue);
478
+ const setValue = useCallback((newValue)=>{
479
+ if (!isControlled) {
480
+ setInternalValue(newValue);
481
+ }
482
+ }, [
483
+ isControlled
484
+ ]);
485
+ const currentValue = isControlled ? value : internalValue;
486
+ return [
487
+ currentValue,
488
+ setValue
489
+ ];
490
+ };
491
+
470
492
  // const isProduction: boolean = process.env.NODE_ENV === 'production';
471
493
  const prefix = 'Invariant failed';
472
494
  // Throw an error if the condition fails
@@ -870,4 +892,4 @@ function mergeRefs(refs) {
870
892
  */ const wait = (ms)=>{
871
893
  };
872
894
 
873
- export { adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, mergeRefs, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, validTaxId, validateDateString, validateFileType, wait };
895
+ export { adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, createEnumLikeObject, debounce, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, mergeRefs, omit, omitByValue, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, rocEraToAd, 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.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "React Utils for GateWeb",
5
5
  "homepage": "https://github.com/GatewebSolutions/react-utils",
6
6
  "files": [