Skip to content

Commit

Permalink
Merge pull request #9 from materialr/feature/autofocus
Browse files Browse the repository at this point in the history
feat: add focus method on input field
  • Loading branch information
hvolschenk authored Aug 24, 2018
2 parents 2828db2 + a9c461c commit 27ff10f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class TextField extends React.Component {
super(props);
this.state = { helperTextControlsId: uuidv1(), id: uuidv1() };
this.elementIcon = undefined;
this.elementField = undefined;
this.elementRoot = undefined;
this.textField = undefined;
this.focus = this.focus.bind(this);
this.getClassNames = this.getClassNames.bind(this);
this.getClassNamesFloatingLabel = this.getClassNamesFloatingLabel.bind(this);
this.getClassNamesHelperText = this.getClassNamesHelperText.bind(this);
Expand Down Expand Up @@ -86,6 +88,9 @@ class TextField extends React.Component {
getId() {
return this.props.id || this.state.id;
}
focus() {
this.elementField.focus();
}
hasFloatingLabel() {
const { fullWidth } = this.props;
return !fullWidth || this.isTextArea();
Expand Down Expand Up @@ -191,6 +196,7 @@ class TextField extends React.Component {
onFocus={onFocus}
onKeyUp={onKeyUp}
placeholder={fullWidth ? label : undefined}
ref={(elementField) => { this.elementField = elementField; }}
required={required}
type={type}
value={value}
Expand All @@ -212,6 +218,7 @@ class TextField extends React.Component {
onDrop={onDrop}
onFocus={onFocus}
onKeyUp={onKeyUp}
ref={(elementField) => { this.elementField = elementField; }}
required={required}
rows={8}
columns={40}
Expand Down
25 changes: 25 additions & 0 deletions src/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,31 @@ test('Reners the correct elements', () => {
expect(actual).toBe(expected);
});

test('Focuses the input field', () => {
const focus = jest.fn();
const wrapper = mount(<TextField label={LABEL} />);
const instance = wrapper.instance();
const { elementField } = instance;
const expected = 1;
elementField.focus = focus;

instance.focus();
const actual = focus.mock.calls.length;

expect(actual).toBe(expected);
});

test('Reners the correct elements', () => {
const wrapper = mount(
<TextField box helperText="helperText" icon="icon" label={LABEL} type="textarea" />,
);
const expected = true;

const actual = wrapper.exists();

expect(actual).toBe(expected);
});

test('Adds extra properties that are passed in', () => {
const DATA_QA = 'DATA_QA';
const wrapper = shallow(
Expand Down

0 comments on commit 27ff10f

Please sign in to comment.