Improve input props handling

This commit is contained in:
Bartłomiej Głownia 2022-03-01 13:56:26 +01:00 committed by Matthew Russell
parent 6a65299918
commit cc084cb247

View File

@ -1,14 +1,11 @@
import { InputHTMLAttributes, forwardRef } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
/* eslint-disable-next-line */ interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
export interface InputProps {
onChange?: React.FormEventHandler<HTMLInputElement>;
hasError?: boolean; hasError?: boolean;
disabled?: boolean; disabled?: boolean;
className?: string; className?: string;
value?: string | number;
} }
export const inputClassNames = ({ export const inputClassNames = ({
hasError, hasError,
disabled, disabled,
@ -42,34 +39,28 @@ export const inputClassNames = ({
className className
); );
export const inputStyle = ({ disabled }: { disabled?: boolean }) => { export const inputStyle = ({
const style: React.CSSProperties = {}; style,
if (disabled) {
style.backgroundImage =
'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAAXNSR0IArs4c6QAAACNJREFUGFdjtLS0/M8ABcePH2eEsRlJl4BpBdHIuuFmEi0BABqjEQVjx/LTAAAAAElFTkSuQmCC)';
}
return style;
};
export function Input({
hasError,
onChange,
disabled, disabled,
className, }: {
value, style?: React.CSSProperties;
}: InputProps) { disabled?: boolean;
return ( }) =>
<input disabled
onChange={onChange} ? {
className={classNames( ...style,
inputClassNames({ hasError, disabled, className }), backgroundImage:
'h-28' 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAAXNSR0IArs4c6QAAACNJREFUGFdjtLS0/M8ABcePH2eEsRlJl4BpBdHIuuFmEi0BABqjEQVjx/LTAAAAAElFTkSuQmCC)',
)}
value={value}
disabled={disabled}
style={inputStyle({ disabled })}
/>
);
} }
: style;
export const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => (
<input
{...props}
ref={ref}
className={classNames(inputClassNames(props), 'h-28')}
style={inputStyle(props)}
/>
));
export default Input; export default Input;