Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform BigNumberPad.js from class component to functional one #20231

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 48 additions & 54 deletions src/components/BigNumberPad.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useState} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -32,74 +32,68 @@ const padNumbers = [
['.', '0', '<'],
];

class BigNumberPad extends React.PureComponent {
constructor(props) {
super(props);

this.state = {
timer: null,
};
}
function BigNumberPad(props) {
const [timer, setTimer] = useState(null);

/**
* Handle long press key on number pad.
* Only handles the '<' key and starts the continuous input timer.
*
* @param {String} key
*/
handleLongPress(key) {
// Only handles deleting.
const handleLongPress = (key) => {
if (key !== '<') {
return;
}
this.props.longPressHandlerStateChanged(true);
const timer = setInterval(() => {
this.props.numberPressed(key);

props.longPressHandlerStateChanged(true);

const newTimer = setInterval(() => {
props.numberPressed(key);
}, 100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When changed from class to functional component, the native device's long press stopped working. It is just deleting the last text only; numberPressed was passing the old value, and due to that, the selection value doesn't have the the correct value, and the previous text is just deleted. More context here: #39079 (comment)

this.setState({timer});
}
setTimer(newTimer);
};

render() {
return (
<View
style={[styles.flexColumn, styles.w100]}
nativeID={this.props.nativeID}
>
{_.map(padNumbers, (row, rowIndex) => (
<View
key={`NumberPadRow-${rowIndex}`}
style={[styles.flexRow, styles.mt3]}
>
{_.map(row, (column, columnIndex) => {
// Adding margin between buttons except first column to
// avoid unccessary space before the first column.
const marginLeft = columnIndex > 0 ? styles.ml3 : {};
return (
<Button
key={column}
shouldEnableHapticFeedback
style={[styles.flex1, marginLeft]}
text={column === '<' ? column : this.props.toLocaleDigit(column)}
onLongPress={() => this.handleLongPress(column)}
onPress={() => this.props.numberPressed(column)}
onPressIn={ControlSelection.block}
onPressOut={() => {
clearInterval(this.state.timer);
ControlSelection.unblock();
this.props.longPressHandlerStateChanged(false);
}}
onMouseDown={(e) => e.preventDefault()}
/>
);
})}
</View>
))}
</View>
);
}
return (
<View
style={[styles.flexColumn, styles.w100]}
nativeID={props.nativeID}
>
{_.map(padNumbers, (row, rowIndex) => (
<View
key={`NumberPadRow-${rowIndex}`}
style={[styles.flexRow, styles.mt3]}
>
{_.map(row, (column, columnIndex) => {
// Adding margin between buttons except first column to
// avoid unccessary space before the first column.
const marginLeft = columnIndex > 0 ? styles.ml3 : {};
return (
<Button
key={column}
shouldEnableHapticFeedback
style={[styles.flex1, marginLeft]}
text={column === '<' ? column : props.toLocaleDigit(column)}
onLongPress={() => handleLongPress(column)}
onPress={() => props.numberPressed(column)}
onPressIn={ControlSelection.block}
onPressOut={() => {
clearInterval(timer);
ControlSelection.unblock();
props.longPressHandlerStateChanged(false);
}}
onMouseDown={(e) => e.preventDefault()}
/>
);
})}
</View>
))}
</View>
);
}

BigNumberPad.propTypes = propTypes;
BigNumberPad.defaultProps = defaultProps;
BigNumberPad.displayName = 'BigNumberPad';

export default withLocalize(BigNumberPad);
Loading