Skip to content

Commit b447b88

Browse files
committed
feat(textfield): make TextField and TextArea one component
Displays textarea when passed 'multiline' prop. Handles 'rows' prop. Added tests. BREAKING CHANGE: TextArea component removed from library (use <TextField multiline /> instead)
1 parent 859dce0 commit b447b88

File tree

6 files changed

+242
-298
lines changed

6 files changed

+242
-298
lines changed

src/components/TextArea/TextArea.js

-99
This file was deleted.

src/components/TextArea/TextArea.stories.js

-100
This file was deleted.

src/components/TextField/TextField.js

+61-35
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,113 @@
11
import React from 'react';
22
import propTypes from 'prop-types';
33

4-
import styled from 'styled-components';
4+
import styled, { css } from 'styled-components';
55
import { createDisabledTextStyles, createFlatBoxStyles } from '../common';
66
import { blockSizes, fontFamily } from '../common/system';
77
import Cutout from '../Cutout/Cutout';
88

9-
const StyledWrapper = styled(Cutout)`
10-
height: ${blockSizes.md};
9+
const sharedWrapperStyles = css`
10+
display: flex;
11+
align-items: center;
12+
width: ${({ fullWidth }) => (fullWidth ? '100%' : 'auto')};
13+
min-height: ${blockSizes.md};
14+
`;
15+
16+
const Wrapper = styled(Cutout)`
17+
${sharedWrapperStyles}
1118
background: ${({ theme, isDisabled }) =>
1219
isDisabled ? theme.material : theme.canvas};
1320
`;
14-
const StyledFlatWrapper = styled.div`
15-
position: relative;
16-
height: ${blockSizes.md};
21+
22+
const FlatWrapper = styled.div`
1723
${createFlatBoxStyles()}
24+
${sharedWrapperStyles}
25+
position: relative;
1826
`;
19-
export const StyledTextInput = styled.input`
27+
28+
const sharedInputStyles = css`
29+
display: block;
2030
box-sizing: border-box;
2131
width: 100%;
2232
height: 100%;
23-
padding: 0 8px;
2433
outline: none;
2534
border: none;
2635
background: none;
2736
font-size: 1rem;
37+
min-height: 27px;
2838
font-family: ${fontFamily};
2939
color: ${({ theme }) => theme.inputText};
3040
${({ disabled, variant }) =>
3141
variant !== 'flat' && disabled && createDisabledTextStyles()}
3242
`;
43+
44+
export const StyledTextInput = styled.input`
45+
${sharedInputStyles}
46+
padding: 0 8px;
47+
`;
48+
49+
const StyledTextArea = styled.textarea`
50+
${sharedInputStyles}
51+
padding: 8px;
52+
resize: none;
53+
`;
54+
3355
const TextField = React.forwardRef(function TextField(props, ref) {
3456
const {
35-
onChange,
57+
className,
3658
disabled,
37-
variant,
38-
type,
39-
style,
59+
fullWidth,
60+
multiline,
61+
onChange,
4062
shadow,
41-
className,
42-
width,
63+
style,
64+
type,
65+
variant,
4366
...otherProps
4467
} = props;
45-
const Wrapper = variant === 'flat' ? StyledFlatWrapper : StyledWrapper;
68+
const WrapperComponent = variant === 'flat' ? FlatWrapper : Wrapper;
69+
const Input = multiline ? StyledTextArea : StyledTextInput;
4670
return (
47-
<Wrapper
48-
width={width}
49-
shadow={shadow}
50-
isDisabled={disabled}
51-
style={{ ...style, width: width || 'auto' }}
71+
<WrapperComponent
5272
className={className}
73+
fullWidth={fullWidth}
74+
isDisabled={disabled}
75+
shadow={shadow}
76+
style={style}
5377
>
54-
<StyledTextInput
78+
<Input
79+
disabled={disabled}
5580
onChange={disabled ? undefined : onChange}
5681
readOnly={disabled}
57-
disabled={disabled}
58-
variant={variant}
59-
type={type}
6082
ref={ref}
83+
type={type}
84+
variant={variant}
6185
{...otherProps}
6286
/>
63-
</Wrapper>
87+
</WrapperComponent>
6488
);
6589
});
6690
TextField.defaultProps = {
91+
className: '',
6792
disabled: false,
68-
type: 'text',
93+
fullWidth: null,
94+
multiline: false,
95+
onChange: () => {},
6996
shadow: true,
70-
variant: 'default',
7197
style: {},
72-
width: null,
73-
onChange: () => {},
74-
className: ''
98+
type: 'text',
99+
variant: 'default'
75100
};
76101

77102
TextField.propTypes = {
78-
width: propTypes.oneOfType([propTypes.string, propTypes.number]),
79-
onChange: propTypes.func,
103+
className: propTypes.string,
80104
disabled: propTypes.bool,
81-
variant: propTypes.oneOf(['default', 'flat']),
105+
fullWidth: propTypes.bool,
106+
multiline: propTypes.bool,
107+
onChange: propTypes.func,
82108
shadow: propTypes.bool,
109+
style: propTypes.shape([propTypes.string, propTypes.number]),
83110
type: propTypes.string,
84-
className: propTypes.string,
85-
style: propTypes.shape([propTypes.string, propTypes.number])
111+
variant: propTypes.oneOf(['default', 'flat'])
86112
};
87113
export default TextField;

0 commit comments

Comments
 (0)