Created
June 17, 2020 08:08
-
-
Save ztolley/531614e0b915c998a36950923a2a6f78 to your computer and use it in GitHub Desktop.
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
/* eslint-disable react/prefer-stateless-function */ | |
import React from 'react' | |
interface WithErrorProps { | |
className?: string | |
error?: string | |
name?: string | |
} | |
export const withError = <P extends object>( | |
Component: React.ComponentType<P> | |
) => | |
class WithError extends React.Component<P & WithErrorProps> { | |
render() { | |
const { className, error, name, ...props } = this.props | |
const thisClassName = error ? `${className} is-invalid` : className | |
return ( | |
<> | |
<Component | |
aria-invalid={!!error} | |
aria-describedby={error ? `error-${name}` : undefined} | |
className={thisClassName} | |
name={name} | |
{...(props as P)} | |
/> | |
{error && ( | |
<div id={`error-${name}`} className="rn-form__invalid-feedback"> | |
{error} | |
</div> | |
)} | |
</> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment