import * as React from 'react';
import { ResponsiveValue } from 'styled-system';
import { keyframes } from '@emotion/core';
import Text from '../text';
import View from '../view';
import TextInput from './text';
import TextArea from './textarea';
export const fadeIn: any = keyframes({
'0%': {
opacity: 0,
},
'100%': {
opacity: 1,
},
});
const XCircle = (props: any) => (
);
const CheckCircle = (props: any) => (
);
type InputVariant = 'onWhite' | 'onBlack' | 'onGray';
export type InputProps = React.AllHTMLAttributes & {
variant?: ResponsiveValue;
adorned?: 'before' | 'after';
} & (
| { valid?: true; invalid?: false | undefined }
| { invalid?: true; valid?: false | undefined }
) &
(
| { type: 'textarea'; pattern: undefined }
| { type?: string; pattern?: string }
);
const Input = (
{
type = 'text',
placeholder = ' ',
label,
variant = 'onWhite',
error,
success,
...props
}: InputProps & {
label?: React.ReactNode;
error?: React.ReactNode;
success?: React.ReactNode;
},
ref: any,
) => {
const [value, setValue] = React.useState('');
const inputEl = React.useRef(ref);
const validity =
inputEl.current &&
// @ts-ignore
inputEl.current.validity &&
// @ts-ignore
inputEl.current.validity.valid;
const valueMissing =
inputEl.current &&
// @ts-ignore
inputEl.current.validity &&
// @ts-ignore
inputEl.current.validity.valueMissing;
const inputValid = React.useMemo(() => {
if (props.valid) {
return 'valid';
}
if (props.invalid) {
return 'invalid';
}
if (inputEl && inputEl.current) {
if (validity && value !== '') {
return 'valid';
}
if (!validity && value !== '') {
return 'invalid';
}
}
return 'pending';
}, [
inputEl.current,
value,
validity,
valueMissing,
props.invalid,
props.valid,
]);
const validationMessage = React.useMemo(() => {
if (inputValid == 'valid') {
return (
{success}
);
}
if (inputValid === 'invalid') {
return (
{error}
);
}
return null;
}, [inputValid, success, error]);
const icon = React.useMemo(() => {
if (type !== 'textarea') {
if (inputValid == 'valid') {
return ;
}
if (inputValid === 'invalid') {
return ;
}
return null;
}
return null;
}, [inputValid]);
const handleChange = React.useCallback(
evt => {
setValue(evt.target.value);
if (typeof props.onChange === 'function') {
props.onChange(evt);
}
},
[props.onChange, setValue],
);
const input = React.useMemo(() => {
switch (type) {
case 'textarea':
return () => (
);
case 'text':
return () => (
);
default:
return () => (
);
}
}, [variant, props]);
return type === 'hidden' ? (
input()
) : (
{label}
{input()}
{Boolean(icon) && (
{icon}
)}
{(error || success) && (
{validationMessage}
)}
);
};
Input.displayName = 'Monterey(Input)';
export default React.forwardRef(Input);