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