summaryrefslogtreecommitdiffhomepage
path: root/app/components/AccountInput.js
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2018-06-01 16:13:10 +0200
committerAndrej Mihajlov <and@mullvad.net>2018-06-05 12:11:55 +0200
commitca2f6fbfcad7b73d4ea63ef46cb1cab943ec9087 (patch)
treeb1f7754eb50896ab3681e35fa4e08be642b940c9 /app/components/AccountInput.js
parent5852c980980de53e00d76a0bdb4b41bf5c0f5b39 (diff)
downloadmullvadvpn-ca2f6fbfcad7b73d4ea63ef46cb1cab943ec9087.tar.xz
mullvadvpn-ca2f6fbfcad7b73d4ea63ef46cb1cab943ec9087.zip
Add formatted source code
Diffstat (limited to 'app/components/AccountInput.js')
-rw-r--r--app/components/AccountInput.js108
1 files changed, 57 insertions, 51 deletions
diff --git a/app/components/AccountInput.js b/app/components/AccountInput.js
index e113b1679c..5a8fa47336 100644
--- a/app/components/AccountInput.js
+++ b/app/components/AccountInput.js
@@ -18,14 +18,14 @@ declare class ClipboardEvent extends Event {
}
export type AccountInputProps = {
- value: string;
- onEnter: ?(() => void);
- onChange: ?((newValue: string) => void);
+ value: string,
+ onEnter: ?() => void,
+ onChange: ?(newValue: string) => void,
};
type AccountInputState = {
- value: string;
- selectionRange: SelectionRange;
+ value: string,
+ selectionRange: SelectionRange,
};
type SelectionRange = [number, number];
@@ -34,12 +34,12 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
static defaultProps = {
value: '',
onEnter: null,
- onChange: null
+ onChange: null,
};
state = {
value: '',
- selectionRange: [0, 0]
+ selectionRange: [0, 0],
};
_ref: ?TextInput;
@@ -53,25 +53,27 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
this.state = {
value: val,
- selectionRange: [val.length, val.length]
+ selectionRange: [val.length, val.length],
};
}
componentWillReceiveProps(nextProps: AccountInputProps) {
const nextVal = this.sanitize(nextProps.value);
- if(nextVal !== this.state.value) {
+ if (nextVal !== this.state.value) {
const len = nextVal.length;
this.setState({ value: nextVal, selectionRange: [len, len] });
}
}
shouldComponentUpdate(nextProps: AccountInputProps, nextState: AccountInputState) {
- return (this.props.value !== nextProps.value ||
- this.props.onEnter !== nextProps.onEnter ||
- this.props.onChange !== nextProps.onChange ||
- this.state.value !== nextState.value ||
- this.state.selectionRange[0] !== nextState.selectionRange[0] ||
- this.state.selectionRange[1] !== nextState.selectionRange[1]);
+ return (
+ this.props.value !== nextProps.value ||
+ this.props.onEnter !== nextProps.onEnter ||
+ this.props.onChange !== nextProps.onChange ||
+ this.state.value !== nextState.value ||
+ this.state.selectionRange[0] !== nextState.selectionRange[0] ||
+ this.state.selectionRange[1] !== nextState.selectionRange[1]
+ );
}
render() {
@@ -79,19 +81,20 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
// eslint-disable-next-line no-unused-vars
const { value, onChange, onEnter, ...otherProps } = this.props;
return (
- <TextInput { ...otherProps }
- value={ displayString }
- onSelectionChange={ this.onSelect }
- onPaste={ this.onPaste }
- onCut={ this.onCut }
- ref={ (ref) => this.onRef(ref) }
- autoCorrect={ false }
- onChangeText={ () => {} }
- onKeyPress={ this.onKeyPress }
+ <TextInput
+ {...otherProps}
+ value={displayString}
+ onSelectionChange={this.onSelect}
+ onPaste={this.onPaste}
+ onCut={this.onCut}
+ ref={(ref) => this.onRef(ref)}
+ autoCorrect={false}
+ onChangeText={() => {}}
+ onKeyPress={this.onKeyPress}
returnKeyType="done"
keyboardType="numeric"
placeholderTextColor={colors.blue20}
- testName='AccountInput'
+ testName="AccountInput"
/>
);
}
@@ -123,7 +126,6 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
return { value: newVal, selectionRange: [selectionOffset, selectionOffset] };
}
-
/**
* Modify string by removing single character or range of characters based on selection range.
*
@@ -137,7 +139,7 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
remove(val: string, selRange: SelectionRange): AccountInputState {
let newVal, selectionOffset;
- if(selRange[0] === selRange[1]) {
+ if (selRange[0] === selRange[1]) {
const oneOff = Math.max(0, selRange[0] - 1);
const head = val.slice(0, oneOff);
const tail = val.slice(selRange[0], val.length);
@@ -153,7 +155,6 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
return { value: newVal, selectionRange: [selectionOffset, selectionOffset] };
}
-
/**
* Convert DOM selection range to internal selection range
*
@@ -176,12 +177,11 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
const within = countSpaces(fmt.slice(start, end));
start -= before;
- end -= (before + within);
+ end -= before + within;
- return [ start, end ];
+ return [start, end];
}
-
/**
* Convert internal selection range to DOM selection range
*
@@ -194,7 +194,9 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
*/
toDomSelection(val: string, selRange: SelectionRange): SelectionRange {
const countSpaces = (val, untilIndex) => {
- if(val.length > 12) { return 0; }
+ if (val.length > 12) {
+ return 0;
+ }
return Math.floor(untilIndex / 4); // groups of 4 digits
};
@@ -206,7 +208,7 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
start += startSpaces;
end += startSpaces + (endSpaces - startSpaces);
- return [ start, end ];
+ return [start, end];
}
// Events
@@ -214,40 +216,42 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
onKeyPress = (e: KeyboardEvent) => {
const { value, selectionRange } = this.state;
- if(e.which === 8) { // backspace
+ if (e.which === 8) {
+ // backspace
this._ignoreSelect = true;
const result = this.remove(value, selectionRange);
e.preventDefault();
this.setState(result, () => {
this._ignoreSelect = false;
- if(this.props.onChange) {
+ if (this.props.onChange) {
this.props.onChange(result.value);
}
});
- } else if(/^[0-9]$/.test(e.key)) { // digits or cmd+v
+ } else if (/^[0-9]$/.test(e.key)) {
+ // digits or cmd+v
this._ignoreSelect = true;
const result = this.insert(value, e.key, selectionRange);
e.preventDefault();
this.setState(result, () => {
this._ignoreSelect = false;
- if(this.props.onChange) {
+ if (this.props.onChange) {
this.props.onChange(result.value);
}
});
- } else if(e.which === 13 && this.props.onEnter) {
+ } else if (e.which === 13 && this.props.onEnter) {
this.props.onEnter();
}
- }
+ };
onSelect = (start: number, end: number) => {
- if (this._ignoreSelect){
+ if (this._ignoreSelect) {
return;
}
const selRange = this.toInternalSelectionRange(this.sanitize(this.state.value), [start, end]);
this.setState({ selectionRange: selRange });
- }
+ };
onPaste = (e: ClipboardEvent) => {
const { value, selectionRange } = this.state;
@@ -256,15 +260,15 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
const result = this.insert(value, filteredData, selectionRange);
e.preventDefault();
this.setState(result, () => {
- if(this.props.onChange) {
+ if (this.props.onChange) {
this.props.onChange(result.value);
}
});
- }
+ };
onCut = (e: ClipboardEvent) => {
const target = e.target;
- if(!(target instanceof HTMLInputElement)) {
+ if (!(target instanceof HTMLInputElement)) {
throw new Error('ref must be an instance of HTMLInputElement');
}
@@ -273,7 +277,7 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
e.preventDefault();
// range is not empty?
- if(selectionRange[0] !== selectionRange[1]) {
+ if (selectionRange[0] !== selectionRange[1]) {
const result = this.remove(value, selectionRange);
const domSelectionRange = this.toDomSelection(value, selectionRange);
const slice = target.value.slice(domSelectionRange[0], domSelectionRange[1]);
@@ -281,26 +285,28 @@ export default class AccountInput extends React.Component<AccountInputProps, Acc
e.clipboardData.setData('text', slice);
this.setState(result, () => {
- if(this.props.onChange) {
+ if (this.props.onChange) {
this.props.onChange(result.value);
}
});
}
- }
+ };
onRef = (ref: ?TextInput) => {
this._ref = ref;
- if(!ref) { return; }
+ if (!ref) {
+ return;
+ }
const { value, selectionRange } = this.state;
const domRange = this.toDomSelection(value, selectionRange);
ref.selectRange(domRange[0], domRange[1]);
- }
+ };
focus() {
- if(this._ref) {
+ if (this._ref) {
this._ref.focus();
}
}
-} \ No newline at end of file
+}