feat(input): add renderRight prop

This commit is contained in:
George Lima 2019-01-21 15:13:48 -03:00
parent 7bebe7e491
commit 18722813a4
1 changed files with 31 additions and 3 deletions

View File

@ -1,5 +1,5 @@
// @flow
import React from 'react';
import React, { type Element } from 'react';
import styled from 'styled-components';
@ -9,6 +9,7 @@ const getDefaultStyles = t => styled[t]`
background-color: ${props => props.theme.colors.inputBackground};
color: ${props => props.theme.colors.text};
padding: 15px;
padding-right: ${props => (props.withRightElement ? '85px' : '15px')};
width: 100%;
outline: none;
font-family: ${props => props.theme.fontFamily};
@ -18,6 +19,16 @@ const getDefaultStyles = t => styled[t]`
}
`;
const Wrapper = styled.div`
position: relative;
`;
const RightElement = styled.div`
position: absolute;
top: 15px;
right: 15px;
`;
const Input = getDefaultStyles('input');
const Textarea = getDefaultStyles('textarea');
@ -30,16 +41,24 @@ type Props = {
disabled?: boolean,
type?: string,
step?: number,
renderRight?: () => Element<*> | null,
};
export const InputComponent = ({
inputType,
onChange = () => {},
renderRight = () => null,
...props
}: Props) => {
const rightElement = renderRight();
const inputTypes = {
input: () => (
<Input onChange={evt => onChange(evt.target.value)} {...props} />
<Input
onChange={evt => onChange(evt.target.value)}
{...props}
withRightElement={Boolean(rightElement)}
/>
),
textarea: () => (
<Textarea onChange={evt => onChange(evt.target.value)} {...props} />
@ -50,7 +69,12 @@ export const InputComponent = ({
throw new Error(`Invalid input type: ${String(inputType)}`);
}
return inputTypes[inputType || 'input']();
return (
<Wrapper>
{inputTypes[inputType || 'input']()}
{rightElement && <RightElement>{rightElement}</RightElement>}
</Wrapper>
);
};
InputComponent.defaultProps = {
@ -58,4 +82,8 @@ InputComponent.defaultProps = {
rows: 4,
disabled: false,
type: 'text',
renderRight: () => null,
onChange: () => {},
onFocus: () => {},
step: 1,
};