2018-12-06 07:42:30 -08:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import styled from 'styled-components';
|
|
|
|
|
2018-12-19 13:41:16 -08:00
|
|
|
const getDefaultStyles = t => styled[t]`
|
2019-01-12 09:55:53 -08:00
|
|
|
border-radius: ${props => props.theme.boxBorderRadius};
|
2018-12-19 13:41:16 -08:00
|
|
|
border: none;
|
2019-01-12 09:55:53 -08:00
|
|
|
background-color: ${props => props.theme.colors.inputBackground};
|
|
|
|
color: ${props => props.theme.colors.text};
|
2018-12-19 13:41:16 -08:00
|
|
|
padding: 15px;
|
2018-12-06 07:42:30 -08:00
|
|
|
width: 100%;
|
2018-12-06 12:50:01 -08:00
|
|
|
outline: none;
|
2019-01-12 09:55:53 -08:00
|
|
|
font-family: ${props => props.theme.fontFamily};
|
2018-12-06 07:42:30 -08:00
|
|
|
|
2018-12-19 13:41:16 -08:00
|
|
|
::placeholder {
|
|
|
|
opacity: 0.5;
|
|
|
|
}
|
2018-12-06 07:42:30 -08:00
|
|
|
`;
|
|
|
|
|
2018-12-19 13:41:16 -08:00
|
|
|
const Input = getDefaultStyles('input');
|
|
|
|
const Textarea = getDefaultStyles('textarea');
|
2018-12-06 07:42:30 -08:00
|
|
|
|
|
|
|
type Props = {
|
2018-12-19 13:41:16 -08:00
|
|
|
inputType?: 'input' | 'textarea',
|
2018-12-06 07:42:30 -08:00
|
|
|
value: string,
|
2019-01-12 09:55:53 -08:00
|
|
|
onChange?: string => void,
|
2019-01-07 10:06:57 -08:00
|
|
|
onFocus?: (SyntheticFocusEvent<HTMLInputElement>) => void,
|
2018-12-06 07:42:30 -08:00
|
|
|
rows?: number,
|
|
|
|
disabled?: boolean,
|
|
|
|
type?: string,
|
2019-01-08 06:34:36 -08:00
|
|
|
step?: number,
|
2019-01-23 09:04:57 -08:00
|
|
|
name?: string,
|
2018-12-06 07:42:30 -08:00
|
|
|
};
|
|
|
|
|
2019-01-12 09:55:53 -08:00
|
|
|
export const InputComponent = ({
|
|
|
|
inputType,
|
|
|
|
onChange = () => {},
|
|
|
|
...props
|
|
|
|
}: Props) => {
|
2018-12-06 12:50:01 -08:00
|
|
|
const inputTypes = {
|
2018-12-15 07:10:39 -08:00
|
|
|
input: () => (
|
|
|
|
<Input onChange={evt => onChange(evt.target.value)} {...props} />
|
|
|
|
),
|
|
|
|
textarea: () => (
|
|
|
|
<Textarea onChange={evt => onChange(evt.target.value)} {...props} />
|
|
|
|
),
|
2018-12-06 12:50:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(inputTypes).find(key => key === inputType)) {
|
2018-12-07 05:45:20 -08:00
|
|
|
throw new Error(`Invalid input type: ${String(inputType)}`);
|
2018-12-06 07:42:30 -08:00
|
|
|
}
|
|
|
|
|
2018-12-07 05:45:20 -08:00
|
|
|
return inputTypes[inputType || 'input']();
|
2018-12-06 07:42:30 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
InputComponent.defaultProps = {
|
|
|
|
inputType: 'input',
|
|
|
|
rows: 4,
|
|
|
|
disabled: false,
|
|
|
|
type: 'text',
|
2019-01-23 09:04:57 -08:00
|
|
|
name: '',
|
2018-12-06 07:42:30 -08:00
|
|
|
};
|