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

[RNMobile] Allow decimals in range cell #24379

Merged
merged 1 commit into from
Aug 7, 2020
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
31 changes: 21 additions & 10 deletions packages/components/src/mobile/bottom-sheet/range-cell.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class BottomSheetRangeCell extends Component {
AppState.removeEventListener( 'change', this.handleChangePixelRatio );
}

toFixed( num ) {
const { toFixed = 0 } = this.props;
const fixed = Math.pow( 10, toFixed );
return Math.floor( num * fixed ) / fixed;
}

getFontScale() {
return PixelRatio.getFontScale() < 1 ? 1 : PixelRatio.getFontScale();
}
Expand All @@ -70,8 +76,15 @@ class BottomSheetRangeCell extends Component {
}

handleChange( text ) {
text =
typeof text === 'number'
? this.toFixed( text )
: text.replace( ',', '.' );

if ( ! isNaN( Number( text ) ) ) {
this.setState( { sliderValue: text } );
this.setState( {
sliderValue: text,
} );
this.announceCurrentValue( text );
}
}
Expand All @@ -81,7 +94,7 @@ class BottomSheetRangeCell extends Component {

if ( validateInput ) {
const sliderValue = this.validateInput( this.state.sliderValue );
this.handleValueSave( sliderValue );
this.handleValueSave( this.toFixed( sliderValue ) );
}

this.setState( newState );
Expand All @@ -96,19 +109,17 @@ class BottomSheetRangeCell extends Component {
return Math.min( Math.max( text, minimumValue ), maximumValue );
}
return Math.min(
Math.max(
text.replace( /[^0-9]/g, '' ).replace( /^0+(?=\d)/, '' ),
minimumValue
),
Math.max( text.replace( /[^0-9.]/g, '' ), minimumValue ),
maximumValue
);
}

handleValueSave( text ) {
if ( ! isNaN( Number( text ) ) ) {
this.onChangeValue( text );
this.setState( { sliderValue: text } );
this.announceCurrentValue( text );
const value = this.toFixed( text );
this.onChangeValue( value );
this.setState( { sliderValue: value } );
this.announceCurrentValue( value );
}
}

Expand Down Expand Up @@ -225,7 +236,7 @@ class BottomSheetRangeCell extends Component {
onChangeText={ this.handleChange }
onFocus={ this.handleToggleFocus }
onBlur={ this.handleToggleFocus }
keyboardType="number-pad"
keyboardType="numeric"
returnKeyType="done"
value={ `${ sliderValue }` }
/>
Expand Down