Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resetState, reset to powerplug #148

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/components/Value.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ Your value state
**set**
`(value: T | (value: T) => T) => void`
Set or over the value state

**reset**
`() => void`
Reset value to initial
3 changes: 2 additions & 1 deletion src/components/Value.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const Value = ({ initial, onChange, ...props }) => (
initial={{ value: initial }}
onChange={onChangeProp(onChange, 'value')}
>
{({ state, setState }) =>
{({ state, setState, resetState }) =>
renderProps(props, {
value: state.value,
set: value => setState(s => ({ value: set(value, s.value) })),
reset: () => resetState(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not reset: resetState?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By analogy, State has setState with callback, so resetState has too, but all other components has no callback, so here we remove callback too

})
}
</State>
Expand Down
1 change: 1 addition & 0 deletions src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ type ValueChange<T> = (value: T) => void
type ValueRender<T> = ({|
value: T,
set: Updater<T>,
reset: () => void,
|}) => React.Node

type ValueProps<T> =
Expand Down
11 changes: 10 additions & 1 deletion tests/components/Value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { lastCallArg } from './utils'

test('<Value />', () => {
const renderFn = jest.fn().mockReturnValue(null)
TestRenderer.create(<Value initial={{ a: 1 }} render={renderFn} />)
const testRenderer = TestRenderer.create(
<Value initial={{ a: 1 }} render={renderFn} />
)

expect(renderFn).toBeCalledTimes(1)

Expand All @@ -21,6 +23,13 @@ test('<Value />', () => {

lastCallArg(renderFn).set(0)
expect(renderFn).lastCalledWith(expect.objectContaining({ value: 0 }))

// test reset
testRenderer.update(<Value initial={3} render={renderFn} />)
expect(renderFn).lastCalledWith(expect.objectContaining({ value: 0 }))

lastCallArg(renderFn).reset()
expect(renderFn).lastCalledWith(expect.objectContaining({ value: 3 }))
})

test('<Value onChange />', () => {
Expand Down
6 changes: 5 additions & 1 deletion tests/test_flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ const noop = () => null

/* Value with inferred generic */
{
const render = ({ value, set }) => {
const render = ({ value, set, reset }) => {
;(value: number | string | boolean)
// $FlowFixMe
;(value: number)
Expand All @@ -549,6 +549,10 @@ const noop = () => null
// $FlowFixMe
;(value: boolean)
set(true)

reset()
// $FlowFixMe
reset(1)
}
const onChange = value => {
;(value: number | string)
Expand Down