Last active
December 2, 2022 22:20
-
-
Save jmlavoier/fa9b36a840a3b2025320e78db0ebd3b2 to your computer and use it in GitHub Desktop.
High Order Component
This file contains hidden or 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
// 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