-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): 完善 useMergeValue 文档及测试用例
- Loading branch information
Showing
5 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { useMergeValue } from '@nmsn/utils'; | ||
|
||
const ControlledInput = ({ | ||
defaultValue, | ||
value, | ||
onChange, | ||
}: { | ||
defaultValue?: string; | ||
value?: string; | ||
onChange?: React.Dispatch<string>; | ||
}) => { | ||
const [curValue, setCurValue] = useMergeValue('', { defaultValue, value }); | ||
|
||
return ( | ||
<input | ||
value={curValue} | ||
onChange={e => { | ||
setCurValue(e.target.value); | ||
onChange?.(e.target.value); | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
const DEFAULT_VALUE = '1'; | ||
const VALUE = '2'; | ||
|
||
export default () => { | ||
const [state1, setState1] = useState(''); | ||
const [state2, setState2] = useState(DEFAULT_VALUE); | ||
const [state3, setState3] = useState(VALUE); | ||
return ( | ||
<> | ||
<p>不使用 value/defaultValue 的情况</p> | ||
<ControlledInput onChange={setState1} /> | ||
<p>state: {state1}</p> | ||
<p>---</p> | ||
<p>使用 defaultValue 的情况</p> | ||
<p>defaultValue: {DEFAULT_VALUE}</p> | ||
<ControlledInput onChange={setState2} defaultValue={DEFAULT_VALUE} /> | ||
<p>state: {state2}</p> | ||
<p>---</p> | ||
<p>使用 value 的情况</p> | ||
<p>value: {VALUE}</p> | ||
<ControlledInput onChange={setState3} value={VALUE} /> | ||
<p>state: {state3}</p> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: useMergeValue | ||
toc: false | ||
--- | ||
|
||
# useMergeValue | ||
|
||
帮助受控组件处理 value、defaultValue 的值 | ||
|
||
<code src="./demo.tsx"></code> | ||
|
||
## API | ||
|
||
```typescript | ||
const [mergedValue, setState, stateValue] = useMergeValue<T>( | ||
defaultStateValue: T, | ||
props?: { defaultValue:?: T; value?: T } | ||
) | ||
: [T, React.Dispatch<React.SetStateAction<T>>, T]; | ||
``` | ||
|
||
### Params | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| ----------------- | ------------------ | ---- | ------ | | ||
| defaultStateValue | `state` 默认值 | `T` | - | | ||
| defaultValue | `props` 默认初始值 | `T` | - | | ||
| value | `props` 值 | `T` | - | | ||
|
||
### Result | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| --------- | ---------------------------------------- | ----------------------------------------- | ------ | | ||
| result[0] | 经过是否 `undefined` 判断的 `stateValue` | `T` | - | | ||
| result[1] | 执行函数 | `React.Dispatch<React.SetStateAction<T>>` | - | | ||
| result[2] | `stateValue` | `T` | - | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import useMergeValue from './'; | ||
|
||
const setUp = ({ | ||
defaultStateValue, | ||
defaultValue, | ||
value, | ||
}: { | ||
defaultStateValue?: string; | ||
defaultValue?: string; | ||
value?: string; | ||
}) => | ||
renderHook( | ||
({ defaultValue, value }: { defaultValue?: string; value?: string }) => | ||
useMergeValue(defaultStateValue, { defaultValue, value }), | ||
{ | ||
initialProps: { defaultValue, value } as { defaultValue?: string; value?: string }, | ||
}, | ||
); | ||
|
||
describe('useMergeValue', () => { | ||
it('useMergeValue with defaultStateValue', async () => { | ||
const { result } = setUp({ defaultStateValue: '1' }); | ||
|
||
expect(result.current?.[0]).toBe('1'); | ||
expect(result.current?.[2]).toBe('1'); | ||
|
||
act(() => { | ||
result.current?.[1]('2'); | ||
}); | ||
|
||
expect(result.current?.[0]).toBe('2'); | ||
expect(result.current?.[2]).toBe('2'); | ||
|
||
act(() => { | ||
result.current?.[1](undefined); | ||
}); | ||
|
||
expect(result.current?.[0]).toBe(undefined); | ||
expect(result.current?.[2]).toBe(undefined); | ||
}); | ||
|
||
it('useMergeValue with defaultStateValue/defaultValue', async () => { | ||
const { result } = setUp({ defaultStateValue: '1', defaultValue: '2' }); | ||
|
||
expect(result.current?.[0]).toBe('2'); | ||
expect(result.current?.[2]).toBe('2'); | ||
|
||
act(() => { | ||
result.current?.[1]('3'); | ||
}); | ||
|
||
expect(result.current?.[0]).toBe('3'); | ||
expect(result.current?.[2]).toBe('3'); | ||
}); | ||
|
||
it('useMergeValue with defaultStateValue/value', async () => { | ||
const { result, rerender } = setUp({ defaultStateValue: '1', value: '2' }); | ||
|
||
expect(result.current?.[0]).toBe('2'); | ||
expect(result.current?.[2]).toBe('2'); | ||
|
||
act(() => { | ||
result.current?.[1]('3'); | ||
}); | ||
|
||
expect(result.current?.[0]).toBe('2'); | ||
expect(result.current?.[2]).toBe('3'); | ||
|
||
act(() => { | ||
result.current?.[1](undefined); | ||
}); | ||
|
||
expect(result.current?.[0]).toBe('2'); | ||
expect(result.current?.[2]).toBe(undefined); | ||
|
||
rerender({ value: undefined }); | ||
|
||
expect(result.current?.[0]).toBe(undefined); | ||
expect(result.current?.[2]).toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
610bdf4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
utils – ./
nmsn-utils.vercel.app
utils-git-main-nmsn.vercel.app
utils-nmsn.vercel.app