Skip to content

Instantly share code, notes, and snippets.

@jmlavoier
Last active December 2, 2022 22:20
Show Gist options
  • Save jmlavoier/fa9b36a840a3b2025320e78db0ebd3b2 to your computer and use it in GitHub Desktop.
Save jmlavoier/fa9b36a840a3b2025320e78db0ebd3b2 to your computer and use it in GitHub Desktop.
High Order Component
// Field.js
import React from 'react';
const onHandleChange = (validate, onChange) => event => {
const { value } = event.target;
const isValid = validate(value);
onChange(value, isValid);
}
const getStyle = isValid => ({
border: isValid ? '' : '1px solid red',
});
export const Field = ({
component: Component, // <-- receice the component to render
validate,
isValid,
onChange,
...props, // <-- receive the name, value, type, options and etc...
}) => (
<div>
<Component
style={getStyle(isValid)}
onChange={onHandleChange(validate, onChange)}
{...props}
/>
</div>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment