-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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] Stepper component #18864
Merged
Merged
[RNMobile] Stepper component #18864
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1fa0c72
Stepper Control
2fc03f0
Stepper component
76704e5
Update docs
5541f3f
RNMobile - Switch RangeControl with StepperControl in Spacer Block
70c35c2
RNMobile - StepperControl accessibility changes and README file
b978650
[RNMobile] - Restore RangeControl in the Spacer block
8159ab4
Merge branch 'master' of github.com:WordPress/gutenberg into rnmobile…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
packages/components/src/mobile/bottom-sheet/stepper-cell/index.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { AccessibilityInfo, View, Platform } from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { Component } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Cell from '../cell'; | ||
import Stepper from './stepper'; | ||
import styles from './style.scss'; | ||
|
||
const STEP_SPEED = 200; | ||
const DEFAULT_STEP = 1; | ||
|
||
class BottomSheetStepperCell extends Component { | ||
constructor( props ) { | ||
super( props ); | ||
|
||
this.announceValue = this.announceValue.bind( this ); | ||
this.onDecrementValue = this.onDecrementValue.bind( this ); | ||
this.onDecrementValuePressIn = this.onDecrementValuePressIn.bind( this ); | ||
this.onIncrementValue = this.onIncrementValue.bind( this ); | ||
this.onIncrementValuePressIn = this.onIncrementValuePressIn.bind( this ); | ||
this.onPressOut = this.onPressOut.bind( this ); | ||
this.startPressInterval = this.startPressInterval.bind( this ); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearTimeout( this.timeout ); | ||
clearInterval( this.interval ); | ||
clearTimeout( this.timeoutAnnounceValue ); | ||
} | ||
|
||
onIncrementValue() { | ||
const { step, maxValue, onChangeValue, value } = this.props; | ||
const newValue = value + step; | ||
|
||
if ( newValue <= maxValue ) { | ||
onChangeValue( newValue ); | ||
this.announceValue( newValue ); | ||
} | ||
} | ||
|
||
onDecrementValue() { | ||
const { step, minValue, onChangeValue, value } = this.props; | ||
const newValue = value - step; | ||
|
||
if ( newValue >= minValue ) { | ||
onChangeValue( newValue ); | ||
this.announceValue( newValue ); | ||
} | ||
} | ||
|
||
onIncrementValuePressIn() { | ||
this.onIncrementValue(); | ||
this.timeout = setTimeout( () => { | ||
this.startPressInterval( this.onIncrementValue ); | ||
}, 500 ); | ||
} | ||
|
||
onDecrementValuePressIn() { | ||
this.onDecrementValue(); | ||
this.timeout = setTimeout( () => { | ||
this.startPressInterval( this.onDecrementValue ); | ||
}, 500 ); | ||
} | ||
|
||
onPressOut() { | ||
clearTimeout( this.timeout ); | ||
clearInterval( this.interval ); | ||
} | ||
|
||
startPressInterval( callback ) { | ||
let counter = 0; | ||
this.interval = setInterval( () => { | ||
callback(); | ||
counter += 1; | ||
|
||
if ( counter === 10 ) { | ||
clearInterval( this.interval ); | ||
this.startPressInterval( callback, STEP_SPEED / 2 ); | ||
} | ||
}, STEP_SPEED ); | ||
} | ||
|
||
announceValue( value ) { | ||
const { label } = this.props; | ||
|
||
if ( Platform.OS === 'ios' ) { // On Android it triggers the accessibilityLabel with the value change | ||
clearTimeout( this.timeoutAnnounceValue ); | ||
this.timeoutAnnounceValue = setTimeout( () => { | ||
AccessibilityInfo.announceForAccessibility( `${ value } ${ label }` ); | ||
}, 300 ); | ||
} | ||
} | ||
|
||
render() { | ||
const { label, icon, minValue, maxValue, value, separatorType } = this.props; | ||
const isMinValue = value === minValue; | ||
const isMaxValue = value === maxValue; | ||
|
||
const accessibilityLabel = sprintf( | ||
/* translators: accessibility text. Inform about current value. %1$s: Control label %2$s: Current value. */ | ||
__( '%1$s. Current value is %2$s' ), | ||
label, value | ||
); | ||
|
||
return ( | ||
<View | ||
accessible={ true } | ||
accessibilityRole="adjustable" | ||
accessibilityLabel={ accessibilityLabel } | ||
accessibilityActions={ [ | ||
{ name: 'increment' }, | ||
{ name: 'decrement' }, | ||
] } | ||
onAccessibilityAction={ ( event ) => { | ||
switch ( event.nativeEvent.actionName ) { | ||
case 'increment': | ||
this.onIncrementValue(); | ||
break; | ||
case 'decrement': | ||
this.onDecrementValue(); | ||
break; | ||
} | ||
} }> | ||
<Cell | ||
accessibilityRole="none" | ||
accessible={ false } | ||
cellContainerStyle={ styles.cellContainerStyles } | ||
cellRowContainerStyle={ styles.cellRowStyles } | ||
disabled={ true } | ||
editable={ false } | ||
icon={ icon } | ||
label={ label } | ||
labelStyle={ styles.cellLabel } | ||
separatorType={ separatorType } | ||
> | ||
<Stepper | ||
isMaxValue={ isMaxValue } | ||
isMinValue={ isMinValue } | ||
onPressInDecrement={ this.onDecrementValuePressIn } | ||
onPressInIncrement={ this.onIncrementValuePressIn } | ||
onPressOut={ this.onPressOut } | ||
value={ value } | ||
/> | ||
</Cell> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
BottomSheetStepperCell.defaultProps = { | ||
step: DEFAULT_STEP, | ||
}; | ||
|
||
export default BottomSheetStepperCell; |
52 changes: 52 additions & 0 deletions
52
packages/components/src/mobile/bottom-sheet/stepper-cell/stepper.android.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Text, TouchableOpacity, View } from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Dashicon } from '@wordpress/components'; | ||
import { withPreferredColorScheme } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import styles from './style.scss'; | ||
|
||
function Stepper( { | ||
getStylesFromColorScheme, | ||
isMaxValue, | ||
isMinValue, | ||
onPressInDecrement, | ||
onPressInIncrement, | ||
onPressOut, | ||
value, | ||
} ) { | ||
const valueStyle = getStylesFromColorScheme( styles.value, styles.valueTextDark ); | ||
const buttonIconStyle = getStylesFromColorScheme( styles.buttonNoBg, styles.buttonNoBgTextDark ); | ||
|
||
return ( | ||
<View style={ styles.container } accesibility={ false } importantForAccessibility="no-hide-descendants"> | ||
<TouchableOpacity | ||
disabled={ isMinValue } | ||
onPressIn={ onPressInDecrement } | ||
onPressOut={ onPressOut } | ||
style={ [ styles.buttonNoBg, isMinValue ? { opacity: 0.4 } : null ] } | ||
> | ||
<Dashicon icon="arrow-down-alt2" size={ 18 } color={ buttonIconStyle.color } /> | ||
</TouchableOpacity> | ||
<Text style={ valueStyle }>{ value }</Text> | ||
<TouchableOpacity | ||
disabled={ isMaxValue } | ||
onPressIn={ onPressInIncrement } | ||
onPressOut={ onPressOut } | ||
style={ [ styles.buttonNoBg, isMaxValue ? { opacity: 0.4 } : null ] } | ||
> | ||
<Dashicon icon="arrow-up-alt2" size={ 18 } color={ buttonIconStyle.color } /> | ||
</TouchableOpacity> | ||
</View> | ||
); | ||
} | ||
|
||
export default withPreferredColorScheme( Stepper ); |
52 changes: 52 additions & 0 deletions
52
packages/components/src/mobile/bottom-sheet/stepper-cell/stepper.ios.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Text, TouchableOpacity, View } from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Dashicon } from '@wordpress/components'; | ||
import { withPreferredColorScheme } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import styles from './style.scss'; | ||
|
||
function Stepper( { | ||
getStylesFromColorScheme, | ||
isMaxValue, | ||
isMinValue, | ||
onPressInDecrement, | ||
onPressInIncrement, | ||
onPressOut, | ||
value, | ||
} ) { | ||
const valueStyle = getStylesFromColorScheme( styles.value, styles.valueTextDark ); | ||
const buttonStyle = getStylesFromColorScheme( styles.button, styles.buttonDark ); | ||
|
||
return ( | ||
<View style={ styles.container }> | ||
<Text style={ valueStyle }>{ value }</Text> | ||
<TouchableOpacity | ||
disabled={ isMinValue } | ||
onPressIn={ onPressInDecrement } | ||
onPressOut={ onPressOut } | ||
style={ [ buttonStyle, isMinValue ? { opacity: 0.4 } : null ] } | ||
> | ||
<Dashicon icon="minus" size={ 24 } color={ buttonStyle.color } /> | ||
</TouchableOpacity> | ||
<TouchableOpacity | ||
disabled={ isMaxValue } | ||
onPressIn={ onPressInIncrement } | ||
onPressOut={ onPressOut } | ||
style={ [ buttonStyle, isMaxValue ? { opacity: 0.4 } : null ] } | ||
> | ||
<Dashicon icon="plus" size={ 24 } color={ buttonStyle.color } style={ styles.plus } /> | ||
</TouchableOpacity> | ||
</View> | ||
); | ||
} | ||
|
||
export default withPreferredColorScheme( Stepper ); |
66 changes: 66 additions & 0 deletions
66
packages/components/src/mobile/bottom-sheet/stepper-cell/style.native.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
.container { | ||
flex-grow: 1; | ||
flex-direction: row; | ||
align-items: center; | ||
min-height: 48px; | ||
justify-content: flex-end; | ||
} | ||
|
||
.cellContainerStyles { | ||
flex-direction: row; | ||
align-items: flex-start; | ||
} | ||
|
||
.cellRowStyles { | ||
min-height: 48px; | ||
align-items: center; | ||
justify-content: space-between; | ||
} | ||
|
||
.cellLabel { | ||
flex: 1; | ||
} | ||
|
||
.button { | ||
width: 32px; | ||
height: 32px; | ||
background-color: $gray-light; | ||
color: #0087be; | ||
border-radius: 16px; | ||
margin-left: 12px; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.buttonNoBg { | ||
width: 32px; | ||
height: 32px; | ||
color: #87a6bc; | ||
margin-left: 12px; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.buttonNoBgTextDark { | ||
color: $light-opacity-700; | ||
} | ||
|
||
.buttonDark { | ||
background-color: $light-opacity-light-100; | ||
color: #5198d9; | ||
} | ||
|
||
.value { | ||
color: #2e4453; | ||
font-size: 16px; | ||
margin-left: 12px; | ||
} | ||
|
||
.valueTextDark { | ||
color: $light-opacity-200; | ||
} | ||
|
||
.plus { | ||
margin-top: 4px; | ||
margin-right: 1px; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great solution for accessibility, works like a charm ✨