Skip to content

Commit 34264cb

Browse files
author
Marco Cesarato
committed
feat: add new events on text input
1 parent 8441af7 commit 34264cb

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class InputSpinner extends Component {
3131
max: this.parseNum(this.props.max),
3232
value: this.parseNum(this.props.value),
3333
step: spinnerStep,
34+
focused: false,
3435
buttonPress: null,
3536
};
3637
}
@@ -239,6 +240,40 @@ class InputSpinner extends Component {
239240
}
240241
}
241242

243+
/**
244+
* On Focus
245+
* @returns {*}
246+
* @param e
247+
*/
248+
onFocus(e){
249+
if (this.props.onFocus) {
250+
this.props.onFocus(e);
251+
}
252+
this.setState({focused: true});
253+
}
254+
255+
/**
256+
* On Blur
257+
* @returns {*}
258+
* @param e
259+
*/
260+
onBlur(e){
261+
if (this.props.onBlur) {
262+
this.props.onBlur(e);
263+
}
264+
this.setState({focused: false});
265+
}
266+
267+
/**
268+
* On Key Press
269+
* @returns {*}
270+
* @param e
271+
*/
272+
onKeyPress(e){
273+
if (this.props.onKeyPress) {
274+
this.props.onKeyPress(e);
275+
}
276+
}
242277

243278
/**
244279
* Max is reached
@@ -264,6 +299,27 @@ class InputSpinner extends Component {
264299
return num <= this.state.min;
265300
}
266301

302+
/**
303+
* Blur
304+
*/
305+
blur() {
306+
this.textInput.blur();
307+
}
308+
309+
/**
310+
* Focus
311+
*/
312+
focus() {
313+
this.textInput.focus();
314+
}
315+
316+
/**
317+
* Clear
318+
*/
319+
clear() {
320+
this.textInput.clear();
321+
}
322+
267323
/**
268324
* Is object empty
269325
* @param obj
@@ -290,6 +346,14 @@ class InputSpinner extends Component {
290346
return !this.props.disabled && this.props.editable;
291347
}
292348

349+
/**
350+
* Is text input focused
351+
* @returns {boolean|Boolean}
352+
*/
353+
isFocused() {
354+
return this.state.focus;
355+
}
356+
293357
/**
294358
* Is left button disabled
295359
* @returns {Boolean}
@@ -672,9 +736,15 @@ class InputSpinner extends Component {
672736
{this.props.prepend}
673737

674738
<TextInput
739+
ref={input => this.textInput = input}
675740
style={this._getInputTextStyle()}
676741
value={this.getValue()}
742+
autofocus={this.props.autofocus}
677743
editable={this.isEditable()}
744+
maxLength={this.props.maxLength}
745+
onKeyPress={this.onKeyPress.bind(this)}
746+
onFocus={this.onFocus.bind(this)}
747+
onBlur={this.onBlur.bind(this)}
678748
keyboardType={this._getKeyboardType()}
679749
onChangeText={this.onChange.bind(this)}
680750
onSubmitEditing={this.onSubmit.bind(this)}
@@ -713,11 +783,16 @@ InputSpinner.propTypes = {
713783
buttonFontSize: PropTypes.number,
714784
buttonFontFamily: PropTypes.string,
715785
buttonTextColor: PropTypes.string,
786+
maxLength: PropTypes.number,
716787
disabled: PropTypes.bool,
717788
editable: PropTypes.bool,
789+
autofocus: PropTypes.bool,
718790
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
719791
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
720792
onChange: PropTypes.func,
793+
onFocus: PropTypes.func,
794+
onBlur: PropTypes.func,
795+
onKeyPress: PropTypes.func,
721796
onMin: PropTypes.func,
722797
onMax: PropTypes.func,
723798
onIncrease: PropTypes.func,
@@ -763,8 +838,10 @@ InputSpinner.defaultProps = {
763838
buttonFontFamily: null,
764839
buttonTextColor: "#FFFFFF",
765840
buttonPressTextColor: "#FFFFFF",
841+
maxLength: null,
766842
disabled: false,
767843
editable: true,
844+
autofocus: false,
768845
width: 150,
769846
height: 50,
770847
buttonLeftDisabled: false,

0 commit comments

Comments
 (0)