Skip to content

Instantly share code, notes, and snippets.

@xamedow
Last active August 18, 2019 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xamedow/1d5ab8a52fd71579e019a980acb97551 to your computer and use it in GitHub Desktop.
Save xamedow/1d5ab8a52fd71579e019a980acb97551 to your computer and use it in GitHub Desktop.
Final form animated condition component
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