|
| 1 | +import _ from 'lodash'; |
1 | 2 | import React from 'react';
|
2 |
| -import {Text, View} from 'react-native'; |
| 3 | +import {AccessibilityInfo, Text, View} from 'react-native'; |
3 | 4 | import PropTypes from 'prop-types';
|
4 |
| -import _ from 'lodash'; |
5 |
| -import createStyles from './style'; |
6 |
| -import {BaseComponent} from '../../commons'; |
7 | 5 | import StepperButton from './StepperButton';
|
| 6 | +import createStyles from './style'; |
| 7 | +import {PureBaseComponent} from '../../commons'; |
8 | 8 |
|
9 | 9 | /**
|
10 | 10 | * @description: Stepper component with increase and decrease buttons
|
11 | 11 | * @gif: https://media.giphy.com/media/3oFzm47bk0v4WV15O8/giphy.gif
|
12 | 12 | * @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/FormScreen.js
|
13 | 13 | */
|
14 |
| -export default class Stepper extends BaseComponent { |
| 14 | +export default class Stepper extends PureBaseComponent { |
15 | 15 | static displayName = 'Stepper';
|
16 | 16 | static propTypes = {
|
17 | 17 | /**
|
@@ -48,6 +48,47 @@ export default class Stepper extends BaseComponent {
|
48 | 48 | };
|
49 | 49 | }
|
50 | 50 |
|
| 51 | + getAccessibilityProps() { |
| 52 | + const {value} = this.state; |
| 53 | + const {accessibilityLabel} = this.props; |
| 54 | + const labelSuffix = `value = ${value}`; |
| 55 | + return { |
| 56 | + accessibilityLabel: accessibilityLabel ? `${accessibilityLabel}, ${labelSuffix}` : `Stepper, ${labelSuffix}`, |
| 57 | + accessible: true, |
| 58 | + accessibilityRole: 'adjustable', |
| 59 | + accessibilityActions: ['decrement', 'increment'], |
| 60 | + onAccessibilityAction: this.onAccessibilityAction |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + onAccessibilityAction = event => { |
| 65 | + const {value} = this.state; |
| 66 | + const {min, max} = this.props; |
| 67 | + const eventMsgContext = event.nativeEvent.action === 'decrement' ? 'Minimum' : 'Maximum'; |
| 68 | + const stepperLimitMsg = `${eventMsgContext} stepper value, ${value}, reached`; |
| 69 | + |
| 70 | + switch (event.nativeEvent.action) { |
| 71 | + case 'decrement': |
| 72 | + if (value <= min) { |
| 73 | + AccessibilityInfo.announceForAccessibility(`${stepperLimitMsg}`); |
| 74 | + } else { |
| 75 | + this.updateValue(value - 1); |
| 76 | + AccessibilityInfo.announceForAccessibility(value - 1); |
| 77 | + } |
| 78 | + break; |
| 79 | + case 'increment': |
| 80 | + if (value >= max) { |
| 81 | + AccessibilityInfo.announceForAccessibility(`${stepperLimitMsg}`); |
| 82 | + } else { |
| 83 | + this.updateValue(value + 1); |
| 84 | + AccessibilityInfo.announceForAccessibility(value + 1); |
| 85 | + } |
| 86 | + break; |
| 87 | + default: |
| 88 | + break; |
| 89 | + } |
| 90 | + }; |
| 91 | + |
51 | 92 | generateStyles() {
|
52 | 93 | this.styles = createStyles(this.props.size);
|
53 | 94 | }
|
@@ -80,7 +121,7 @@ export default class Stepper extends BaseComponent {
|
80 | 121 | render() {
|
81 | 122 | const {minusDisabled, plusDisabled, testID} = this.getDisabledState();
|
82 | 123 | return (
|
83 |
| - <View style={[this.styles.container, this.props.containerStyle]}> |
| 124 | + <View {...this.getAccessibilityProps()} style={[this.styles.container, this.props.containerStyle]}> |
84 | 125 | <View style={this.styles.title}>
|
85 | 126 | <Text testID={`${testID}.label`} style={this.styles.titleText}>
|
86 | 127 | {this.getLabel()}
|
|
0 commit comments