Last active
August 18, 2019 21:01
-
-
Save xamedow/1d5ab8a52fd71579e019a980acb97551 to your computer and use it in GitHub Desktop.
Final form animated condition component
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
import React from "react"; | |
import { Field } from "react-final-form"; | |
import { useSpring, animated } from "react-spring"; | |
interface Props extends React.HTMLProps<HTMLElement> { | |
when: string; | |
is: any; | |
} | |
interface InnerProps { | |
input: { | |
value: any; | |
}; | |
} | |
const Inner = ({ | |
value, | |
is, | |
children | |
}: { | |
value: any; | |
is: any; | |
children: React.ReactNode; | |
}) => { | |
const contentStatus = value === is; | |
const contentProps = useSpring({ | |
opacity: contentStatus ? 1 : 0, | |
transformOrigin: 'center top', | |
transform: contentStatus ? "scale(1, 1)" : "scale(0.9, 0)" | |
}); | |
if (!contentStatus) return null; | |
return <animated.div style={contentProps}>{children}</animated.div>; | |
}; | |
const Condition = ({ when, is, children }: Props) => ( | |
<Field name={when} subscription={{ value: true }}> | |
{({ input: { value } }: InnerProps) => ( | |
<Inner value={value} is={is}> | |
{children} | |
</Inner> | |
)} | |
</Field> | |
); | |
export default Condition; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment