Skip to content

Commit 021a547

Browse files
Brian Stonestonebk
authored andcommitted
feat: ariaValuetext now supports a function for dynamic value text
1 parent de75419 commit 021a547

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

src/components/ReactSlider/ReactSlider.jsx

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,16 @@ class ReactSlider extends React.Component {
210210
ariaLabel: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
211211

212212
/**
213-
* aria-valuetext for screen-readers
213+
* aria-valuetext for screen-readers.
214+
* Can be a static string, or a function that returns a string.
215+
* The function will be passed a single argument,
216+
* an object with the following properties:
217+
*
218+
* - `index` {`number`} the index of the thumb
219+
* - 'value' {`number` | `array`} the current value state
214220
*/
215221
// eslint-disable-next-line zillow/react/require-default-props
216-
ariaValuetext: PropTypes.string,
222+
ariaValuetext: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
217223

218224
/**
219225
* Provide a custom render function for the track node.
@@ -841,31 +847,38 @@ class ReactSlider extends React.Component {
841847
this.state.index === i ? this.props.thumbActiveClassName : ''
842848
}`;
843849

844-
return React.createElement(
845-
'div',
846-
{
847-
ref: r => {
848-
this[`thumb${i}`] = r;
849-
},
850-
key: `thumb${i}`,
851-
className,
852-
style,
853-
onMouseDown: this.createOnMouseDown(i),
854-
onTouchStart: this.createOnTouchStart(i),
855-
onFocus: this.createOnKeyDown(i),
856-
tabIndex: 0,
857-
role: 'slider',
858-
'aria-orientation': this.props.orientation,
859-
'aria-valuenow': this.state.value[i],
860-
'aria-valuemin': this.props.min,
861-
'aria-valuemax': this.props.max,
862-
'aria-label': Array.isArray(this.props.ariaLabel)
863-
? this.props.ariaLabel[i]
864-
: this.props.ariaLabel,
865-
'aria-valuetext': this.props.ariaValuetext,
850+
const props = {
851+
ref: r => {
852+
this[`thumb${i}`] = r;
866853
},
867-
child
868-
);
854+
key: `thumb${i}`,
855+
className,
856+
style,
857+
onMouseDown: this.createOnMouseDown(i),
858+
onTouchStart: this.createOnTouchStart(i),
859+
onFocus: this.createOnKeyDown(i),
860+
tabIndex: 0,
861+
role: 'slider',
862+
'aria-orientation': this.props.orientation,
863+
'aria-valuenow': this.state.value[i],
864+
'aria-valuemin': this.props.min,
865+
'aria-valuemax': this.props.max,
866+
'aria-label': Array.isArray(this.props.ariaLabel)
867+
? this.props.ariaLabel[i]
868+
: this.props.ariaLabel,
869+
};
870+
871+
if (this.props.ariaValuetext) {
872+
props['aria-valuetext'] =
873+
typeof this.props.ariaValuetext === 'string'
874+
? this.props.ariaValuetext
875+
: this.props.ariaValuetext({
876+
value: this.state.value,
877+
index: i,
878+
});
879+
}
880+
881+
return React.createElement('div', props, child);
869882
};
870883

871884
renderThumbs(offset) {

src/components/ReactSlider/ReactSlider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Double slider
1818
trackClassName="example-track"
1919
defaultValue={[0, 100]}
2020
ariaLabel={['Lower thumb', 'Upper thumb']}
21+
ariaValuetext={({ index, value }) => `Thumb value ${value[index]}`}
2122
renderThumb={({ index, value }) => value[index]}
2223
pearling
2324
minDistance={10}

0 commit comments

Comments
 (0)