Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit c9672e0

Browse files
committed
Added ariaLabels prop to insert aria-label to elements
1 parent 5e11039 commit c9672e0

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ fixedHeight | Boolean | false | Since some
138138
renderStaticRangeLabel(`DefinedRange`)| Function | | Callback function to be triggered for the static range configurations that have `hasCustomRendering: true` on them. Instead of rendering `staticRange.label`, return value of this callback will be rendered.
139139
staticRanges(`DefinedRange`, `DateRangePicker`) | Array | [default preDefined ranges](https://github.com/hypeserver/react-date-range/blob/master/src/defaultRanges.js) | -
140140
inputRanges(`DefinedRange`, `DateRangePicker`) | Array | [default input ranges](https://github.com/hypeserver/react-date-range/blob/master/src/defaultRanges.js) | -
141+
ariaLabels | **Object | {} | inserts aria-label to inner elements
141142

142143
*shape of range:
143144
```js
@@ -152,6 +153,22 @@ inputRanges(`DefinedRange`, `DateRangePicker`) | Array | [default i
152153
}
153154
```
154155

156+
**shape of ariaLabels:
157+
```js
158+
{
159+
// The key of dateInput should be same as key in range.
160+
dateInput: objectOf(
161+
shape({
162+
startDate: PropTypes.string,
163+
endDate: PropTypes.string
164+
})
165+
),
166+
monthPicker: string,
167+
yearPicker: string,
168+
prevButton: string,
169+
nextButton: string,
170+
}
171+
```
155172
#### Infinite Scrolled Mode
156173

157174
To enable infinite scroll set `scroll={{enabled: true}}` basically. Infinite scroll feature is affected by `direction`(rendering direction for months) and `months`(for rendered months count) props directly.

src/accessibility/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import PropTypes from 'prop-types';
2+
3+
export const ariaLabelsShape = PropTypes.shape({
4+
dateInput: PropTypes.objectOf(
5+
PropTypes.shape({ startDate: PropTypes.string, endDate: PropTypes.string })
6+
),
7+
monthPicker: PropTypes.string,
8+
yearPicker: PropTypes.string,
9+
prevButton: PropTypes.string,
10+
nextButton: PropTypes.string,
11+
});

src/components/Calendar/index.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from 'date-fns';
2929
import defaultLocale from 'date-fns/locale/en-US';
3030
import coreStyles from '../../styles';
31+
import { ariaLabelsShape } from '../../accessibility';
3132

3233
class Calendar extends PureComponent {
3334
constructor(props, context) {
@@ -176,7 +177,7 @@ class Calendar extends PureComponent {
176177
this.isFirstRender = false;
177178
};
178179
renderMonthAndYear = (focusedDate, changeShownDate, props) => {
179-
const { showMonthArrow, minDate, maxDate, showMonthAndYearPickers } = props;
180+
const { showMonthArrow, minDate, maxDate, showMonthAndYearPickers, ariaLabels } = props;
180181
const upperYearLimit = (maxDate || Calendar.defaultProps.maxDate).getFullYear();
181182
const lowerYearLimit = (minDate || Calendar.defaultProps.minDate).getFullYear();
182183
const styles = this.styles;
@@ -186,7 +187,8 @@ class Calendar extends PureComponent {
186187
<button
187188
type="button"
188189
className={classnames(styles.nextPrevButton, styles.prevButton)}
189-
onClick={() => changeShownDate(-1, 'monthOffset')}>
190+
onClick={() => changeShownDate(-1, 'monthOffset')}
191+
aria-label={ariaLabels.prevButton}>
190192
<i />
191193
</button>
192194
) : null}
@@ -195,7 +197,8 @@ class Calendar extends PureComponent {
195197
<span className={styles.monthPicker}>
196198
<select
197199
value={focusedDate.getMonth()}
198-
onChange={e => changeShownDate(e.target.value, 'setMonth')}>
200+
onChange={e => changeShownDate(e.target.value, 'setMonth')}
201+
aria-label={ariaLabels.monthPicker}>
199202
{this.state.monthNames.map((monthName, i) => (
200203
<option key={i} value={i}>
201204
{monthName}
@@ -207,7 +210,8 @@ class Calendar extends PureComponent {
207210
<span className={styles.yearPicker}>
208211
<select
209212
value={focusedDate.getFullYear()}
210-
onChange={e => changeShownDate(e.target.value, 'setYear')}>
213+
onChange={e => changeShownDate(e.target.value, 'setYear')}
214+
aria-label={ariaLabels.yearPicker}>
211215
{new Array(upperYearLimit - lowerYearLimit + 1)
212216
.fill(upperYearLimit)
213217
.map((val, i) => {
@@ -230,7 +234,8 @@ class Calendar extends PureComponent {
230234
<button
231235
type="button"
232236
className={classnames(styles.nextPrevButton, styles.nextButton)}
233-
onClick={() => changeShownDate(+1, 'monthOffset')}>
237+
onClick={() => changeShownDate(+1, 'monthOffset')}
238+
aria-label={ariaLabels.nextButton}>
234239
<i />
235240
</button>
236241
) : null}
@@ -262,6 +267,7 @@ class Calendar extends PureComponent {
262267
editableDateInputs,
263268
startDatePlaceholder,
264269
endDatePlaceholder,
270+
ariaLabels,
265271
} = this.props;
266272

267273
const defaultColor = rangeColors[focusedRange[0]] || color;
@@ -287,6 +293,11 @@ class Calendar extends PureComponent {
287293
placeholder={startDatePlaceholder}
288294
dateOptions={this.dateOptions}
289295
dateDisplayFormat={dateDisplayFormat}
296+
ariaLabel={
297+
ariaLabels.dateInput &&
298+
ariaLabels.dateInput[range.key] &&
299+
ariaLabels.dateInput[range.key].startDate
300+
}
290301
onChange={this.onDragSelectionEnd}
291302
onFocus={() => this.handleRangeFocusChange(i, 0)}
292303
/>
@@ -300,6 +311,11 @@ class Calendar extends PureComponent {
300311
placeholder={endDatePlaceholder}
301312
dateOptions={this.dateOptions}
302313
dateDisplayFormat={dateDisplayFormat}
314+
ariaLabel={
315+
ariaLabels.dateInput &&
316+
ariaLabels.dateInput[range.key] &&
317+
ariaLabels.dateInput[range.key].endDate
318+
}
303319
onChange={this.onDragSelectionEnd}
304320
onFocus={() => this.handleRangeFocusChange(i, 1)}
305321
/>
@@ -528,6 +544,7 @@ Calendar.defaultProps = {
528544
editableDateInputs: false,
529545
dragSelectionEnabled: true,
530546
fixedHeight: false,
547+
ariaLabels: {},
531548
};
532549

533550
Calendar.propTypes = {
@@ -581,6 +598,7 @@ Calendar.propTypes = {
581598
editableDateInputs: PropTypes.bool,
582599
dragSelectionEnabled: PropTypes.bool,
583600
fixedHeight: PropTypes.bool,
601+
ariaLabels: ariaLabelsShape,
584602
};
585603

586604
export default Calendar;

src/components/DateInput/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class DateInput extends PureComponent {
6464
};
6565

6666
render() {
67-
const { className, readOnly, placeholder, disabled, onFocus } = this.props;
67+
const { className, readOnly, placeholder, ariaLabel, disabled, onFocus } = this.props;
6868
const { value, invalid } = this.state;
6969

7070
return (
@@ -74,6 +74,7 @@ class DateInput extends PureComponent {
7474
disabled={disabled}
7575
value={value}
7676
placeholder={placeholder}
77+
aria-label={ariaLabel}
7778
onKeyDown={this.onKeyDown}
7879
onChange={this.onChange}
7980
onBlur={this.onBlur}
@@ -92,6 +93,7 @@ DateInput.propTypes = {
9293
readOnly: PropTypes.bool,
9394
dateOptions: PropTypes.object,
9495
dateDisplayFormat: PropTypes.string,
96+
ariaLabel: PropTypes.string,
9597
className: PropTypes.string,
9698
onFocus: PropTypes.func.isRequired,
9799
onChange: PropTypes.func.isRequired,

src/components/DateRangePicker/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,42 @@ const [state, setState] = useState({
8484
ranges={[state.selection1, state.selection2, state.selection3]}
8585
/>;
8686
```
87+
88+
#### Example: Insert Aria-label
89+
90+
```jsx inside Markdown
91+
import { addDays } from 'date-fns';
92+
import { useState } from 'react';
93+
94+
const [state, setState] = useState([
95+
{
96+
startDate: addDays(new Date(), -6),
97+
endDate: new Date(),
98+
key: 'selection1'
99+
},
100+
{
101+
startDate: addDays(new Date(), 1),
102+
endDate: addDays(new Date(), 7),
103+
key: 'selection2'
104+
}
105+
]);
106+
107+
<DateRangePicker
108+
onChange={item => setState([item.selection])}
109+
showSelectionPreview={true}
110+
moveRangeOnFirstSelection={false}
111+
months={2}
112+
ranges={state}
113+
direction="horizontal"
114+
ariaLabels={{
115+
dateInput: {
116+
selection1: { startDate: "start date input of selction 1", endDate: "end date input of selction 1" },
117+
selection2: { startDate: "start date input of selction 2", endDate: "end date input of selction 2" }
118+
},
119+
monthPicker: "month picker",
120+
yearPicker: "year picker",
121+
prevButton: "previous month button",
122+
nextButton: "next month button",
123+
}}
124+
/>;
125+
```

0 commit comments

Comments
 (0)