Skip to content

Commit cc2bd13

Browse files
gergely-nagyTheSharpieOne
authored andcommitted
fix(Input): fix size prop (#662)
Deprecated size prop on input. Use bsSize going forward. Closes #660
1 parent 839419e commit cc2bd13

5 files changed

Lines changed: 32 additions & 9 deletions

File tree

docs/lib/Components/FormPage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default class FormPage extends React.Component {
5353
// type can be things like text, password, (typical input types) as well as select and textarea, providing children as you normally would to those.
5454
type: PropTypes.string,
5555
size: PropTypes.string,
56+
bsSize: PropTypes.string,
5657
state: deprecated(PropTypes.string, 'Please use the prop "valid"'),
5758
valid: PropTypes.bool,
5859
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),

docs/lib/examples/InputGridSizing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class Example extends React.Component {
88
<FormGroup row>
99
<Label for="exampleEmail" sm={2} size="lg">Email</Label>
1010
<Col sm={10}>
11-
<Input type="email" name="email" id="exampleEmail" placeholder="lg" size="lg" />
11+
<Input type="email" name="email" id="exampleEmail" placeholder="lg" bsSize="lg" />
1212
</Col>
1313
</FormGroup>
1414
<FormGroup row>

docs/lib/examples/InputSizing.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ export default class Example extends React.Component {
55
render() {
66
return (
77
<Form>
8-
<Input placeholder="lg" size="lg" />
8+
<Input placeholder="lg" bsSize="lg" />
99
<Input placeholder="default" />
10-
<Input placeholder="sm" size="sm" />
11-
<Input type="select" size="lg">
10+
<Input placeholder="sm" bsSize="sm" />
11+
<Input type="select" bsSize="lg">
1212
<option>Large Select</option>
1313
</Input>
1414
<Input type="select">
1515
<option>Default Select</option>
1616
</Input>
17-
<Input type="select" size="sm">
17+
<Input type="select" bsSize="sm">
1818
<option>Small Select</option>
1919
</Input>
2020
</Form>

src/Input.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import React from 'react';
44
import PropTypes from 'prop-types';
55
import classNames from 'classnames';
6-
import { mapToCssModules, deprecated } from './utils';
6+
import { mapToCssModules, deprecated, warnOnce } from './utils';
77

88
const propTypes = {
99
children: PropTypes.node,
1010
type: PropTypes.string,
1111
size: PropTypes.string,
12+
bsSize: PropTypes.string,
1213
state: deprecated(PropTypes.string, 'Please use the prop "valid"'),
1314
valid: PropTypes.bool,
1415
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
@@ -31,7 +32,7 @@ class Input extends React.Component {
3132
className,
3233
cssModule,
3334
type,
34-
size,
35+
bsSize,
3536
state,
3637
valid,
3738
tag,
@@ -43,6 +44,7 @@ class Input extends React.Component {
4344
} = this.props;
4445

4546
const checkInput = ['radio', 'checkbox'].indexOf(type) > -1;
47+
const isNotaNumber = new RegExp('\\D', 'g');
4648

4749
const fileInput = type === 'file';
4850
const textareaInput = type === 'textarea';
@@ -72,11 +74,17 @@ class Input extends React.Component {
7274
}
7375
}
7476

77+
if (attributes.size && isNotaNumber.test(attributes.size)) {
78+
warnOnce('Please use the prop "bsSize" instead of the "size" to bootstrap\'s input sizing.');
79+
bsSize = attributes.size;
80+
delete attributes.size;
81+
}
82+
7583
const classes = mapToCssModules(classNames(
7684
className,
7785
valid === false && 'is-invalid',
7886
valid && 'is-valid',
79-
size ? `form-control-${size}` : false,
87+
bsSize ? `form-control-${bsSize}` : false,
8088
formControlClass
8189
), cssModule);
8290

src/__tests__/Input.spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,26 @@ describe('Input', () => {
9494
expect(wrapper.hasClass('is-valid')).toBe(true);
9595
});
9696

97-
it('should render with "form-control-${size}" class when size is provided', () => {
97+
it('should render with "form-control-${size}" class when size is "lg" or "sm"', () => {
9898
const wrapper = shallow(<Input size="lg" />);
9999

100100
expect(wrapper.hasClass('form-control-lg')).toBe(true);
101101
});
102102

103+
it('should render with "form-control" class when size is nor "lg" nor "sm"', () => {
104+
const wrapper = shallow(<Input size="5" />);
105+
106+
expect(wrapper.hasClass('form-control-sm')).toBe(false);
107+
expect(wrapper.hasClass('form-control-lg')).toBe(false);
108+
expect(wrapper.hasClass('form-control')).toBe(true);
109+
});
110+
111+
it('should render with "form-control-${bsSize}" class when bsSize is provided', () => {
112+
const wrapper = shallow(<Input bsSize="sm" />);
113+
114+
expect(wrapper.hasClass('form-control-sm')).toBe(true);
115+
});
116+
103117
it('should render with "form-control" class by default', () => {
104118
const wrapper = shallow(<Input />);
105119

0 commit comments

Comments
 (0)