Skip to content

Commit 631dfe7

Browse files
author
余聪
committed
feat: add eq option
1 parent 2bfd87b commit 631dfe7

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ should be named as `use.{{lowerCase}}`, eg. `@rp/use.i18n`.
5454

5555
- [@rcp/c.keepalive](packages/c.keepalive) - Keep react component view / store when switched view.
5656
- [@rcp/c.loadingwrapper](packages/c.loadingwrapper) - A component for easy create loading mask
57+
- [@rcp/c.prompt](packages/c.prompt) - Advanced React router prompt support beforeunload
5758
- [@rcp/hoc.i18n](packages/hoc.i18n) - React Component's high order component about internationalization
5859
- [@rcp/hoc.mount](packages/hoc.mount) - The high order component for mounting component
5960
- [@rcp/hoc.uncontrolled](packages/hoc.uncontrolled) - The high order component for creating uncontrolled component
@@ -66,6 +67,7 @@ should be named as `use.{{lowerCase}}`, eg. `@rp/use.i18n`.
6667
- [@rcp/util.displayname](packages/util.displayname) - The utility for getting display name
6768
- [@rcp/util.iscompclass](packages/util.iscompclass) - The utility for checking component class
6869
- [@rcp/util.iselemof](packages/util.iselemof) - The utility determinating the input is element of the component class
70+
- [@rcp/util.open](packages/util.open) - Open react element standalone
6971
- [@rcp/util.tocompclass](packages/util.tocompclass) - The utility converting stateless to be component class
7072

7173

packages/use.uncontrolled/__tests__/spec.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import useUncontrolled from '..'
1+
import useUncontrolled from '../index'
22
import * as TestRenderer from 'react-test-renderer'
33
import { renderHook, act } from '@testing-library/react-hooks'
44
import * as React from 'react'

packages/use.uncontrolled/index.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,47 @@
66

77
import * as React from 'react'
88

9+
const defaultEq = (a, b) => a === b
10+
911
export function useUncontrolledCore<T = any>(
1012
propValue?: T,
1113
{
1214
initialValue = propValue,
13-
useEffect = React.useEffect,
15+
useEffect = React.useLayoutEffect,
16+
eq = defaultEq,
1417
onChange
15-
}: { initialValue?: T; useEffect?: typeof React.useEffect; onChange?: (value: T) => void } = {}
18+
}: {
19+
eq?: (a: T, b: T) => boolean
20+
initialValue?: T
21+
useEffect?: typeof React.useEffect
22+
onChange?: (value: T) => void
23+
} = {}
1624
): [T | undefined, (newValue: ((value: T) => T) | T) => void] {
1725
const [value, setStateValue] = React.useState(initialValue)
1826
const setValueFinal = React.useCallback(
1927
(newValue: Function | boolean | any) => {
2028
if (typeof newValue === 'function') {
2129
newValue = newValue(value)
2230
}
23-
if (value === newValue) {
31+
if (eq(value, newValue)) {
2432
return
2533
}
2634
setStateValue(newValue)
2735
onChange && onChange(newValue)
2836
},
29-
[value, onChange, setStateValue]
37+
[value, eq, onChange, setStateValue]
3038
)
3139

40+
const fnRef = React.useRef(setValueFinal)
41+
fnRef.current = setValueFinal
42+
3243
useEffect(
3344
() => {
3445
if (typeof propValue !== 'undefined') {
35-
setValueFinal(propValue)
46+
fnRef.current(propValue)
3647
}
3748
},
38-
[propValue, setValueFinal]
49+
[propValue]
3950
)
4051

4152
return [value, setValueFinal]
@@ -48,6 +59,7 @@ export function useUncontrolledCore<T = any>(
4859
* @param [defaultValue] {T} - Initialize value firstly
4960
* @param [onChange] {(value: T) => void} - Bind `onChange` handler when value updating
5061
* @param [useEffect] {typeof React.useLayoutEffect}
62+
* @param [eq] {(a: T, b: T) => boolean}
5163
* @returns {Array} `[T, ((value: T) => T | T) => void]`
5264
* @example
5365
* function Input({value, onChangeValue, defaultValue}) {
@@ -60,19 +72,22 @@ export default function useUncontrolled<T = any>({
6072
value,
6173
defaultValue,
6274
onChange,
63-
useEffect = React.useLayoutEffect
75+
useEffect = React.useLayoutEffect,
76+
eq
6477
}: {
6578
useEffect?: typeof React.useEffect
6679
value?: T
6780
defaultValue?: T
6881
onChange?: (value: T) => void
82+
eq?: (a: T, b: T) => boolean
6983
}) {
7084
let initialValue = value
7185
if (defaultValue) {
7286
initialValue = typeof value === 'undefined' ? defaultValue : value
7387
}
7488

7589
return useUncontrolledCore<T>(value, {
90+
eq,
7691
initialValue,
7792
useEffect,
7893
onChange

packages/use.uncontrolled/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
"engines": {
3434
"node": ">=6"
3535
},
36-
"version": "1.0.3",
36+
"version": "2.0.0",
3737
"gitHead": "f28ececd4b2fab80551292239b568841c895177f"
3838
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"jsx": "react",
99
"lib": ["dom", "es2015", "es2017"],
1010
"experimentalDecorators": true,
11-
"moduleResolution": "node"
11+
"moduleResolution": "node",
12+
"skipLibCheck": true
1213
},
1314
"include": ["./packages/**/*"],
1415
"exclude": ["node_modules", "test", "__tests__"]

0 commit comments

Comments
 (0)