Skip to content

Commit

Permalink
InputWithClearButton [nfc]: Remove unnecessary canBeCleared state.
Browse files Browse the repository at this point in the history
Greg points out [1] that `canBeCleared` is always the same as
`text.length > 0`. So, just drop this piece of state and compute the
value we need when we need it.

[1] zulip#4914 (comment)
  • Loading branch information
chrisbobbe committed Jul 28, 2021
1 parent 2dc1204 commit 4fdcb32
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/common/InputWithClearButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const componentStyles = createStyleSheet({
type Props = $ReadOnly<$Diff<InputProps, {| textInputRef: mixed, value: mixed, _: mixed |}>>;

type State = {|
canBeCleared: boolean,
text: string,
|};

Expand All @@ -29,7 +28,6 @@ type State = {|
*/
export default class InputWithClearButton extends PureComponent<Props, State> {
state = {
canBeCleared: false,
text: '',
};
// We should replace the fixme with
Expand All @@ -40,7 +38,6 @@ export default class InputWithClearButton extends PureComponent<Props, State> {

handleChangeText = (text: string) => {
this.setState({
canBeCleared: text.length > 0,
text,
});
if (this.props.onChangeText) {
Expand All @@ -57,7 +54,7 @@ export default class InputWithClearButton extends PureComponent<Props, State> {
};

render() {
const { canBeCleared, text } = this.state;
const { text } = this.state;

return (
<View style={styles.row}>
Expand All @@ -67,7 +64,7 @@ export default class InputWithClearButton extends PureComponent<Props, State> {
onChangeText={this.handleChangeText}
value={text}
/>
{canBeCleared && (
{text.length > 0 && (
<Icon
name="x"
size={24}
Expand Down

0 comments on commit 4fdcb32

Please sign in to comment.