Skip to content

Commit f6395ed

Browse files
authored
fix(ui): add input api methods
1 parent 5234f85 commit f6395ed

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

src/framework/ui/input/input.component.tsx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import React from 'react';
88
import {
9-
Image,
109
ImageProps,
1110
StyleProp,
1211
StyleSheet,
@@ -31,7 +30,6 @@ import {
3130
} from '../support/services';
3231
import {
3332
FlexStyleProps,
34-
InputEndEditEvent,
3533
InputFocusEvent,
3634
} from '../support/typings';
3735

@@ -59,6 +57,15 @@ export type InputProps = StyledComponentProps & TextInputProps & ComponentProps;
5957
*
6058
* @extends React.Component
6159
*
60+
* @method {() => void} focus - Requests focus for the given input or view. The exact behavior triggered
61+
* will depend on the platform and type of view.
62+
*
63+
* @method {() => void} blur - Removes focus from an input or view. This is the opposite of `focus()`.
64+
*
65+
* @method {() => boolean} isFocused - Returns if the input is currently focused.
66+
*
67+
* @method {() => void} clear - Removes all text from the input.
68+
*
6269
* @property {boolean} disabled - Determines whether component is disabled.
6370
* Default is `false`.
6471
*
@@ -140,7 +147,23 @@ export class InputComponent extends React.Component<InputProps> {
140147

141148
static styledComponentName: string = 'Input';
142149

143-
static Icon: React.ComponentClass<ImageProps> = Image;
150+
private textInputRef: React.RefObject<TextInput> = React.createRef();
151+
152+
public focus = () => {
153+
this.textInputRef.current.focus();
154+
};
155+
156+
public blur = () => {
157+
this.textInputRef.current.blur();
158+
};
159+
160+
public isFocused = (): boolean => {
161+
return this.textInputRef.current.isFocused();
162+
};
163+
164+
public clear = () => {
165+
this.textInputRef.current.clear();
166+
};
144167

145168
private onFocus = (event: InputFocusEvent) => {
146169
this.props.dispatch([Interaction.FOCUSED]);
@@ -150,11 +173,11 @@ export class InputComponent extends React.Component<InputProps> {
150173
}
151174
};
152175

153-
private onEndEditing = (event: InputEndEditEvent) => {
176+
private onBlur = (event: InputFocusEvent) => {
154177
this.props.dispatch([]);
155178

156-
if (this.props.onEndEditing) {
157-
this.props.onEndEditing(event);
179+
if (this.props.onBlur) {
180+
this.props.onBlur(event);
158181
}
159182
};
160183

@@ -322,12 +345,13 @@ export class InputComponent extends React.Component<InputProps> {
322345
{labelElement}
323346
<View style={componentStyle.inputContainer}>
324347
<TextInput
348+
ref={this.textInputRef}
325349
{...restProps}
326350
style={componentStyle.text}
327351
placeholderTextColor={componentStyle.placeholder.color}
328352
editable={!disabled}
329353
onFocus={this.onFocus}
330-
onEndEditing={this.onEndEditing}
354+
onBlur={this.onBlur}
331355
/>
332356
{iconElement}
333357
</View>

src/framework/ui/input/input.spec.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,55 @@ const renderComponent = (props?: InputProps): RenderAPI => {
4242
);
4343
};
4444

45+
describe('@input: native methods', () => {
46+
47+
const RefMock = React.forwardRef((props: InputProps, ref: React.Ref<TextInput>) => {
48+
return (
49+
<ApplicationProvider
50+
mapping={mapping}
51+
theme={theme}>
52+
<Input ref={ref} {...props}/>
53+
</ApplicationProvider>
54+
);
55+
});
56+
57+
it('* focus', () => {
58+
const componentRef: React.RefObject<TextInput> = React.createRef();
59+
render(
60+
<RefMock ref={componentRef}/>,
61+
);
62+
63+
expect(componentRef.current.focus).toBeTruthy();
64+
});
65+
66+
it('* blur', () => {
67+
const componentRef: React.RefObject<TextInput> = React.createRef();
68+
render(
69+
<RefMock ref={componentRef}/>,
70+
);
71+
72+
expect(componentRef.current.blur).toBeTruthy();
73+
});
74+
75+
it('* isFocused', () => {
76+
const componentRef: React.RefObject<TextInput> = React.createRef();
77+
render(
78+
<RefMock ref={componentRef}/>,
79+
);
80+
81+
expect(componentRef.current.isFocused).toBeTruthy();
82+
});
83+
84+
it('* clear', () => {
85+
const componentRef: React.RefObject<TextInput> = React.createRef();
86+
render(
87+
<RefMock ref={componentRef}/>,
88+
);
89+
90+
expect(componentRef.current.clear).toBeTruthy();
91+
});
92+
});
93+
4594
describe('@input: matches snapshot', () => {
4695

4796
describe('* interaction', () => {

0 commit comments

Comments
 (0)