@aiszlab/relax 2.1.1 → 2.1.2-beta.0

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.
@@ -3,13 +3,14 @@
3
3
  var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
4
  var react = require('react');
5
5
  var isUndefined = require('../is/is-undefined.cjs');
6
- var useUpdateEffect = require('./use-update-effect.cjs');
7
6
  var isFunction = require('../is/is-function.cjs');
7
+ var useEvent = require('./use-event.cjs');
8
+ var reactDom = require('react-dom');
8
9
 
9
10
  function useControlledState(controlledState) {
10
11
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
11
12
  defaultState = _ref.defaultState,
12
- onUpdate = _ref.onUpdate;
13
+ onChange = _ref.onChange;
13
14
  // initialize state
14
15
  var _useState = react.useState(function () {
15
16
  // default use controlled state
@@ -25,19 +26,27 @@ function useControlledState(controlledState) {
25
26
  _useState2 = _slicedToArray(_useState, 2),
26
27
  _state = _useState2[0],
27
28
  _setState = _useState2[1];
28
- // sync value back to `undefined` when it from control to un-control
29
- useUpdateEffect.useUpdateEffect(function () {
30
- if (controlledState !== _state) {
31
- onUpdate === null || onUpdate === void 0 || onUpdate(controlledState, _state);
32
- }
33
- if (!isUndefined.isUndefined(controlledState)) {
29
+ // 变更状态函数
30
+ // 变更内容为函数,且需要触发`onChange`,那么本次状态更新必为同步
31
+ // 其余继续保持批处理逻辑
32
+ var setState = useEvent.useEvent(function (action) {
33
+ if (isFunction.isFunction(action)) {
34
+ if (onChange) {
35
+ reactDom.flushSync(function () {
36
+ _setState(action);
37
+ });
38
+ onChange(_state);
39
+ return;
40
+ }
41
+ _setState(action);
34
42
  return;
35
43
  }
36
- _setState(defaultState !== null && defaultState !== void 0 ? defaultState : controlledState);
37
- }, [controlledState]);
44
+ _setState(action);
45
+ onChange === null || onChange === void 0 || onChange(action);
46
+ });
38
47
  // use controlled
39
48
  var state = isUndefined.isUndefined(controlledState) ? _state : controlledState;
40
- return [state, _setState];
49
+ return [state, setState];
41
50
  }
42
51
 
43
52
  exports.useControlledState = useControlledState;
@@ -7,10 +7,10 @@ type UsingControlledState<S> = {
7
7
  */
8
8
  defaultState?: State<S>;
9
9
  /**
10
- * @description
11
- * value update callback
10
+ * 状态发生改变后触发的回调事件
11
+ * @default undefined
12
12
  */
13
- onUpdate?: (next: Partialable<S>, prev: Partialable<S>) => void;
13
+ onChange?: (state: Partialable<S>) => void;
14
14
  };
15
15
  type UsedControlledState<T> = [T, Dispatch<SetStateAction<T>>];
16
16
  /**
@@ -21,7 +21,7 @@ type UsedControlledState<T> = [T, Dispatch<SetStateAction<T>>];
21
21
  */
22
22
  declare function useControlledState<T>(): UsedControlledState<Partialable<T>>;
23
23
  declare function useControlledState<T>(controlledState: T): UsedControlledState<T>;
24
- declare function useControlledState<T>(controlledState: T, usingControlledState: UsingControlledState<undefined>): UsedControlledState<T>;
24
+ declare function useControlledState<T>(controlledState: T, usingControlledState: UsingControlledState<T>): UsedControlledState<T>;
25
25
  declare function useControlledState<T>(controlledState: Partialable<T>, usingControlledState: RequiredIn<UsingControlledState<T>, "defaultState">): UsedControlledState<T>;
26
26
  declare function useControlledState<T>(controlledState: T, usingControlledState: UsingControlledState<T>): UsedControlledState<T>;
27
27
  export { useControlledState };
@@ -1,13 +1,14 @@
1
1
  import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
2
2
  import { useState } from 'react';
3
3
  import { isUndefined } from '../is/is-undefined.mjs';
4
- import { useUpdateEffect } from './use-update-effect.mjs';
5
4
  import { isFunction } from '../is/is-function.mjs';
5
+ import { useEvent } from './use-event.mjs';
6
+ import { flushSync } from 'react-dom';
6
7
 
7
8
  function useControlledState(controlledState) {
8
9
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
9
10
  defaultState = _ref.defaultState,
10
- onUpdate = _ref.onUpdate;
11
+ onChange = _ref.onChange;
11
12
  // initialize state
12
13
  var _useState = useState(function () {
13
14
  // default use controlled state
@@ -23,19 +24,27 @@ function useControlledState(controlledState) {
23
24
  _useState2 = _slicedToArray(_useState, 2),
24
25
  _state = _useState2[0],
25
26
  _setState = _useState2[1];
26
- // sync value back to `undefined` when it from control to un-control
27
- useUpdateEffect(function () {
28
- if (controlledState !== _state) {
29
- onUpdate === null || onUpdate === void 0 || onUpdate(controlledState, _state);
30
- }
31
- if (!isUndefined(controlledState)) {
27
+ // 变更状态函数
28
+ // 变更内容为函数,且需要触发`onChange`,那么本次状态更新必为同步
29
+ // 其余继续保持批处理逻辑
30
+ var setState = useEvent(function (action) {
31
+ if (isFunction(action)) {
32
+ if (onChange) {
33
+ flushSync(function () {
34
+ _setState(action);
35
+ });
36
+ onChange(_state);
37
+ return;
38
+ }
39
+ _setState(action);
32
40
  return;
33
41
  }
34
- _setState(defaultState !== null && defaultState !== void 0 ? defaultState : controlledState);
35
- }, [controlledState]);
42
+ _setState(action);
43
+ onChange === null || onChange === void 0 || onChange(action);
44
+ });
36
45
  // use controlled
37
46
  var state = isUndefined(controlledState) ? _state : controlledState;
38
- return [state, _setState];
47
+ return [state, setState];
39
48
  }
40
49
 
41
50
  export { useControlledState };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "2.1.1",
3
+ "version": "2.1.2-beta.0",
4
4
  "description": "react utils collection",
5
5
  "exports": {
6
6
  ".": {