Skip to content

Commit bb7c269

Browse files
authored
fix(components): Correctly apply TextArea styles (#445)
* feat(components): pass styles to Input as object not classname Deprecates the use of classname props to pass styles to the Input component. Will be removed in the next major version. * refactor(components): deprecate the element prop on the Input * fix(components): correctly pass custom textarea styles
1 parent 55a4d80 commit bb7c269

File tree

5 files changed

+184
-8
lines changed

5 files changed

+184
-8
lines changed

src/components/Input/Input.js

+55-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import { size } from 'polished';
2424
import HtmlElement from '../HtmlElement';
2525
import { textMega, disableVisually } from '../../styles/style-helpers';
2626
import { directions } from '../../styles/constants';
27-
import { childrenPropType } from '../../util/shared-prop-types';
27+
import {
28+
childrenPropType,
29+
deprecatedPropType
30+
} from '../../util/shared-prop-types';
2831

2932
import Tooltip from '../Tooltip';
3033

@@ -268,8 +271,12 @@ const StyledInput = ({
268271
inline,
269272
disabled,
270273
wrapperClassName,
274+
wrapperStyles,
271275
inputClassName,
276+
inputStyles,
272277
deepRef,
278+
element,
279+
as,
273280
...props
274281
}) => {
275282
const prefix = RenderPrefix && <RenderPrefix css={prefixStyles} />;
@@ -284,7 +291,13 @@ const StyledInput = ({
284291

285292
return (
286293
<InputContainer
287-
{...{ noMargin, inline, disabled, className: wrapperClassName }}
294+
{...{
295+
noMargin,
296+
inline,
297+
disabled,
298+
className: wrapperClassName,
299+
css: wrapperStyles
300+
}}
288301
>
289302
{prefix}
290303
<InputElement
@@ -294,9 +307,11 @@ const StyledInput = ({
294307
disabled,
295308
hasWarning,
296309
deepRef,
310+
element: element || as,
297311
hasPrefix: !!prefix,
298312
hasSuffix: !!suffix,
299-
className: inputClassName
313+
className: inputClassName,
314+
css: inputStyles
300315
}}
301316
aria-invalid={invalid}
302317
blacklist={{
@@ -327,9 +342,20 @@ Input.RIGHT = directions.RIGHT;
327342
Input.propTypes = {
328343
children: childrenPropType,
329344
/**
345+
* @deprecated
330346
* The HTML input element to render.
331347
*/
332-
element: PropTypes.oneOf(['input', 'textarea']),
348+
element: deprecatedPropType(
349+
PropTypes.oneOf(['input', 'textarea']),
350+
[
351+
'Emotion 10 introduced the ability to change the HTML element.',
352+
'Use the "as" prop instead.'
353+
].join(' ')
354+
),
355+
/**
356+
* The HTML input element to render.
357+
*/
358+
as: PropTypes.oneOf(['input', 'textarea']),
333359
/**
334360
* Render prop that should render a left-aligned overlay icon or element.
335361
* Receives a className prop.
@@ -382,13 +408,35 @@ Input.propTypes = {
382408
*/
383409
textAlign: PropTypes.oneOf([Input.LEFT, Input.RIGHT]),
384410
/**
411+
* @deprecated
385412
* Class name to overwrite the <input> element styles.
386413
*/
387-
inputClassName: PropTypes.string,
414+
inputClassName: deprecatedPropType(
415+
PropTypes.string,
416+
[
417+
'Emotion 10 uses style objects instead of classnames.',
418+
'Use the "inputStyles" prop instead.'
419+
].join(' ')
420+
),
388421
/**
422+
* Emotion style object to overwrite the <input> element styles.
423+
*/
424+
inputStyles: PropTypes.object,
425+
/**
426+
* @deprecated
389427
* Class name to overwrite the input wrapper element styles.
390428
*/
391-
wrapperClassName: PropTypes.string,
429+
wrapperClassName: deprecatedPropType(
430+
PropTypes.string,
431+
[
432+
'Emotion 10 uses style objects instead of classnames.',
433+
'Use the "wrapperStyles" prop instead.'
434+
].join(' ')
435+
),
436+
/**
437+
* Emotion style object to overwrite the input wrapper element styles.
438+
*/
439+
wrapperStyles: PropTypes.object,
392440
/**
393441
* DOM node to be forwarded to the actual input being rendered by
394442
* styled.
@@ -400,7 +448,7 @@ StyledInput.propTypes = Input.propTypes;
400448

401449
Input.defaultProps = {
402450
children: null,
403-
element: 'input',
451+
as: 'input',
404452
renderPrefix: null,
405453
renderSuffix: null,
406454
validationHint: null,

src/components/Input/Input.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
import React from 'react';
17+
import { css } from '@emotion/core';
1718

1819
import Input from '.';
1920
import Label from '../Label';
@@ -107,6 +108,20 @@ describe('Input', () => {
107108
expect(actual).toMatchSnapshot();
108109
});
109110

111+
it('should render with custom styles', () => {
112+
const actual = create(
113+
<Input
114+
wrapperStyles={css`
115+
border: 1px solid red;
116+
`}
117+
inputStyles={css`
118+
color: red;
119+
`}
120+
/>
121+
);
122+
expect(actual).toMatchSnapshot();
123+
});
124+
110125
/**
111126
* Accessibility tests.
112127
*/

src/components/Input/__snapshots__/Input.spec.js.snap

+85
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,91 @@ exports[`Input should render with a suffix when passed the suffix prop 1`] = `
605605
</div>
606606
`;
607607

608+
exports[`Input should render with custom styles 1`] = `
609+
.circuit-2 {
610+
opacity: 0;
611+
-webkit-transition: opacity 200ms ease-in-out;
612+
transition: opacity 200ms ease-in-out;
613+
position: absolute;
614+
top: 1px;
615+
right: 1px;
616+
height: 16px;
617+
width: 16px;
618+
margin: 12px;
619+
pointer-events: none;
620+
}
621+
622+
.circuit-4 {
623+
color: #212933;
624+
display: block;
625+
position: relative;
626+
margin-bottom: 16px;
627+
border: 1px solid red;
628+
}
629+
630+
.circuit-0 {
631+
background-color: #FFFFFF;
632+
border-width: 1px;
633+
border-style: solid;
634+
border-color: #D8DDE1;
635+
border-radius: 4px;
636+
box-shadow: inset 0 1px 2px 0 rgba(102,113,123,0.12);
637+
padding: 8px 12px;
638+
-webkit-transition: border-color 200ms ease-in-out;
639+
transition: border-color 200ms ease-in-out;
640+
width: 100%;
641+
font-size: 16px;
642+
line-height: 24px;
643+
padding-right: calc( 12px + 16px + 12px );
644+
color: red;
645+
}
646+
647+
.circuit-0:focus,
648+
.circuit-0:active {
649+
border: 1px solid #3388FF;
650+
outline: none;
651+
}
652+
653+
.circuit-0::-webkit-input-placeholder {
654+
color: #9DA7B1;
655+
-webkit-transition: color 200ms ease-in-out;
656+
transition: color 200ms ease-in-out;
657+
}
658+
659+
.circuit-0::-moz-placeholder {
660+
color: #9DA7B1;
661+
-webkit-transition: color 200ms ease-in-out;
662+
transition: color 200ms ease-in-out;
663+
}
664+
665+
.circuit-0:-ms-input-placeholder {
666+
color: #9DA7B1;
667+
-webkit-transition: color 200ms ease-in-out;
668+
transition: color 200ms ease-in-out;
669+
}
670+
671+
.circuit-0::placeholder {
672+
color: #9DA7B1;
673+
-webkit-transition: color 200ms ease-in-out;
674+
transition: color 200ms ease-in-out;
675+
}
676+
677+
<div
678+
className=" circuit-4 circuit-5"
679+
disabled={false}
680+
>
681+
<input
682+
aria-invalid={false}
683+
className=" circuit-0 circuit-1"
684+
disabled={false}
685+
required={false}
686+
/>
687+
<div
688+
className="circuit-2 circuit-3"
689+
/>
690+
</div>
691+
`;
692+
608693
exports[`Input should render with default styles 1`] = `
609694
.circuit-4 {
610695
color: #212933;

src/components/TextArea/TextArea.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const textAreaStyles = css`
2929
* TextArea component for forms.
3030
*/
3131
const TextArea = props => (
32-
<Input {...props} css={textAreaStyles} element="textarea" />
32+
<Input {...props} inputStyles={textAreaStyles} as="textarea" />
3333
);
3434

3535
TextArea.LEFT = Input.LEFT;

src/components/TextArea/__snapshots__/TextArea.spec.js.snap

+28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ exports[`TextArea should prioritize disabled over error styles 1`] = `
1515
font-size: 16px;
1616
line-height: 24px;
1717
padding-right: calc( 12px + 16px + 12px );
18+
overflow: auto;
19+
resize: vertical;
1820
}
1921
2022
.circuit-0:focus,
@@ -106,6 +108,8 @@ exports[`TextArea should prioritize disabled over warning styles 1`] = `
106108
font-size: 16px;
107109
line-height: 24px;
108110
padding-right: calc( 12px + 16px + 12px );
111+
overflow: auto;
112+
resize: vertical;
109113
}
110114
111115
.circuit-0:focus,
@@ -237,6 +241,8 @@ exports[`TextArea should prioritize error over optional styles 1`] = `
237241
border-style: dashed;
238242
box-shadow: none;
239243
padding-right: calc( 12px + 16px + 12px );
244+
overflow: auto;
245+
resize: vertical;
240246
}
241247
242248
.circuit-0:focus,
@@ -331,6 +337,8 @@ exports[`TextArea should render with a Tooltip when passed the validationHint pr
331337
font-size: 16px;
332338
line-height: 24px;
333339
padding-right: calc( 12px + 16px + 12px );
340+
overflow: auto;
341+
resize: vertical;
334342
}
335343
336344
.circuit-0:focus,
@@ -460,6 +468,8 @@ exports[`TextArea should render with a prefix when passed the prefix prop 1`] =
460468
font-size: 16px;
461469
line-height: 24px;
462470
padding-right: calc( 12px + 16px + 12px );
471+
overflow: auto;
472+
resize: vertical;
463473
}
464474
465475
.circuit-0:focus,
@@ -544,6 +554,8 @@ exports[`TextArea should render with a suffix when passed the suffix prop 1`] =
544554
font-size: 16px;
545555
line-height: 24px;
546556
padding-right: calc( 12px + 16px + 12px );
557+
overflow: auto;
558+
resize: vertical;
547559
}
548560
549561
.circuit-0:focus,
@@ -628,6 +640,8 @@ exports[`TextArea should render with default styles 1`] = `
628640
font-size: 16px;
629641
line-height: 24px;
630642
padding-right: calc( 12px + 16px + 12px );
643+
overflow: auto;
644+
resize: vertical;
631645
}
632646
633647
.circuit-0:focus,
@@ -704,6 +718,8 @@ exports[`TextArea should render with disabled styled when passed the disabled pr
704718
font-size: 16px;
705719
line-height: 24px;
706720
padding-right: calc( 12px + 16px + 12px );
721+
overflow: auto;
722+
resize: vertical;
707723
}
708724
709725
.circuit-0:focus,
@@ -774,6 +790,8 @@ exports[`TextArea should render with inline styles when passed the inline prop 1
774790
font-size: 16px;
775791
line-height: 24px;
776792
padding-right: calc( 12px + 16px + 12px );
793+
overflow: auto;
794+
resize: vertical;
777795
}
778796
779797
.circuit-0:focus,
@@ -880,6 +898,8 @@ exports[`TextArea should render with invalid styles when passed the invalid prop
880898
font-size: 16px;
881899
line-height: 24px;
882900
padding-right: calc( 12px + 16px + 12px );
901+
overflow: auto;
902+
resize: vertical;
883903
}
884904
885905
.circuit-0:focus,
@@ -967,6 +987,8 @@ exports[`TextArea should render with no margin styles when passed the noMargin p
967987
font-size: 16px;
968988
line-height: 24px;
969989
padding-right: calc( 12px + 16px + 12px );
990+
overflow: auto;
991+
resize: vertical;
970992
}
971993
972994
.circuit-0:focus,
@@ -1074,6 +1096,8 @@ exports[`TextArea should render with optional styles when passed the optional pr
10741096
border-style: dashed;
10751097
box-shadow: none;
10761098
padding-right: calc( 12px + 16px + 12px );
1099+
overflow: auto;
1100+
resize: vertical;
10771101
}
10781102
10791103
.circuit-0:focus,
@@ -1144,6 +1168,8 @@ exports[`TextArea should render with valid styles when passed the showValid prop
11441168
font-size: 16px;
11451169
line-height: 24px;
11461170
padding-right: calc( 12px + 16px + 12px );
1171+
overflow: auto;
1172+
resize: vertical;
11471173
}
11481174
11491175
.circuit-0:focus,
@@ -1231,6 +1257,8 @@ exports[`TextArea should render with warning styles when passed the hasWarning p
12311257
font-size: 16px;
12321258
line-height: 24px;
12331259
padding-right: calc( 12px + 16px + 12px );
1260+
overflow: auto;
1261+
resize: vertical;
12341262
}
12351263
12361264
.circuit-0:focus,

0 commit comments

Comments
 (0)