Skip to content

Commit

Permalink
Add support for autoFocus prop (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj committed Nov 27, 2019
1 parent b12d488 commit bd56e88
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Displays an input field complete with custom inputs, native input, and a calenda

|Prop name|Description|Default value|Example values|
|----|----|----|----|
|autoFocus|Automatically focuses the input on mount.|n/a|`true`|
|calendarAriaLabel|`aria-label` for the calendar button.|n/a|`"Toggle calendar"`|
|calendarClassName|Class name(s) that will be added along with `"react-calendar"` to the main React-Calendar `<div>` element.|n/a|<ul><li>String: `"class1 class2"`</li><li>Array of strings: `["class1", "class2 class3"]`</li></ul>|
|calendarIcon|Content of the calendar button. Setting the value explicitly to `null` will hide the icon.|(default icon)|<ul><li>String: `"Calendar"`</li><li>React element: `<CalendarIcon />`</li></ul>|
Expand Down
23 changes: 17 additions & 6 deletions src/DateInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function renderCustomInputs(placeholder, elementFunctions, allowMultipleInstance
if (!allowMultipleInstances && usedFunctions.includes(renderFunction)) {
res.push(currentMatch);
} else {
res.push(renderFunction(currentMatch));
res.push(renderFunction(currentMatch, index));
usedFunctions.push(renderFunction);
}
}
Expand Down Expand Up @@ -439,8 +439,13 @@ export default class DateInput extends PureComponent {
}
}

renderDay = (currentMatch) => {
const { dayAriaLabel, dayPlaceholder, showLeadingZeros } = this.props;
renderDay = (currentMatch, index) => {
const {
autoFocus,
dayAriaLabel,
dayPlaceholder,
showLeadingZeros,
} = this.props;
const { day, month, year } = this.state;

if (currentMatch && currentMatch.length > 2) {
Expand All @@ -454,6 +459,7 @@ export default class DateInput extends PureComponent {
key="day"
{...this.commonInputProps}
ariaLabel={dayAriaLabel}
autoFocus={index === 0 && autoFocus}
month={month}
placeholder={dayPlaceholder}
showLeadingZeros={showLeadingZerosFromFormat || showLeadingZeros}
Expand All @@ -463,8 +469,9 @@ export default class DateInput extends PureComponent {
);
}

renderMonth = (currentMatch) => {
renderMonth = (currentMatch, index) => {
const {
autoFocus,
locale,
monthAriaLabel,
monthPlaceholder,
Expand All @@ -482,6 +489,7 @@ export default class DateInput extends PureComponent {
key="month"
{...this.commonInputProps}
ariaLabel={monthAriaLabel}
autoFocus={index === 0 && autoFocus}
locale={locale}
placeholder={monthPlaceholder}
short={currentMatch.length === 3}
Expand All @@ -498,6 +506,7 @@ export default class DateInput extends PureComponent {
key="month"
{...this.commonInputProps}
ariaLabel={monthAriaLabel}
autoFocus={index === 0 && autoFocus}
placeholder={monthPlaceholder}
showLeadingZeros={showLeadingZerosFromFormat || showLeadingZeros}
value={month}
Expand All @@ -506,15 +515,16 @@ export default class DateInput extends PureComponent {
);
}

renderYear = () => {
const { yearAriaLabel, yearPlaceholder } = this.props;
renderYear = (currentMatch, index) => {
const { autoFocus, yearAriaLabel, yearPlaceholder } = this.props;
const { year } = this.state;

return (
<YearInput
key="year"
{...this.commonInputProps}
ariaLabel={yearAriaLabel}
autoFocus={index === 0 && autoFocus}
placeholder={yearPlaceholder}
value={year}
valueType={this.valueType}
Expand Down Expand Up @@ -592,6 +602,7 @@ const isValue = PropTypes.oneOfType([
]);

DateInput.propTypes = {
autoFocus: PropTypes.bool,
className: PropTypes.string.isRequired,
dayAriaLabel: PropTypes.string,
dayPlaceholder: PropTypes.string,
Expand Down
4 changes: 4 additions & 0 deletions src/DateInput/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import PropTypes from 'prop-types';
import mergeClassNames from 'merge-class-names';
import updateInputWidth, { getFontShorthand } from 'update-input-width';

/* eslint-disable jsx-a11y/no-autofocus */

function select(element) {
if (!element) {
return;
Expand Down Expand Up @@ -37,6 +39,7 @@ function updateInputWidthOnFontLoad(element) {

export default function Input({
ariaLabel,
autoFocus,
className,
disabled,
itemRef,
Expand All @@ -61,6 +64,7 @@ export default function Input({
key="input"
aria-label={ariaLabel}
autoComplete="off"
autoFocus={autoFocus}
className={mergeClassNames(
`${className}__input`,
`${className}__${nameForClass || name}`,
Expand Down
3 changes: 3 additions & 0 deletions src/DatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export default class DatePicker extends PureComponent {

renderInputs() {
const {
autoFocus,
calendarAriaLabel,
calendarIcon,
clearAriaLabel,
Expand Down Expand Up @@ -162,6 +163,7 @@ export default class DatePicker extends PureComponent {
<DateInput
{...ariaLabelProps}
{...placeholderProps}
autoFocus={autoFocus}
className={`${baseClassName}__inputGroup`}
disabled={disabled}
format={format}
Expand Down Expand Up @@ -310,6 +312,7 @@ const isValue = PropTypes.oneOfType([
]);

DatePicker.propTypes = {
autoFocus: PropTypes.bool,
calendarAriaLabel: PropTypes.string,
calendarClassName: PropTypes.oneOfType([
PropTypes.string,
Expand Down

0 comments on commit bd56e88

Please sign in to comment.