Skip to content

Instantly share code, notes, and snippets.

@ztolley
Created June 17, 2020 08:08
Show Gist options
  • Save ztolley/531614e0b915c998a36950923a2a6f78 to your computer and use it in GitHub Desktop.
Save ztolley/531614e0b915c998a36950923a2a6f78 to your computer and use it in GitHub Desktop.
/* 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