Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit e677b7b

Browse files
committed
feat: add confitional HOC
1 parent e4a5b89 commit e677b7b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/conditional.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { FormSpy } from 'react-final-form'
2+
import React, { Fragment } from 'react'
3+
import propTypes from 'prop-types'
4+
5+
export const conditional = Input => {
6+
const Conditional = ({ condition, ...props }) => (
7+
<Fragment>
8+
{condition && <Input {...props} />}
9+
10+
<FormSpy
11+
onChange={({ form }) => {
12+
if (!condition && values[props.name]) {
13+
form.mutators.clear(props.name)
14+
}
15+
}}
16+
/>
17+
</Fragment>
18+
)
19+
20+
Conditional.propTypes = {
21+
...Input.propTypes,
22+
condition: propTypes.bool,
23+
}
24+
25+
Conditional.defaultProps = {
26+
condition: true,
27+
}
28+
29+
Conditional.displayName = `conditional(${Input.displayName})`
30+
31+
return Conditional
32+
}

0 commit comments

Comments
 (0)