Skip to content

Commit

Permalink
feat: making input ids passable by props for datepicker (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydaf committed Jun 15, 2021
1 parent 0d100ce commit 2667dce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/components/Datepicker/DatepickerRangeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import styled from 'styled-components';
import { compose, margin, MarginProps, width, WidthProps } from 'styled-system';
import { Colors, Elevation, MediaQueries } from '../../essentials';
import { theme } from '../../essentials/theme';
import { useGeneratedId } from '../../utils/hooks/useGeneratedId';
import { ChevronRightIcon } from '../../icons';
import { Input } from '../Input/Input';

Expand Down Expand Up @@ -153,6 +154,14 @@ interface DatepickerRangeInputProps extends MarginProps, WidthProps {
* or fn to be trigger as a callback when error
*/
errorHandler?: (() => void) | string;
/**
* The id to be assigned to the start date input
*/
startInputId?: string;
/**
* The id to be assigned to the end date input
*/
endInputId?: string;
}

interface DateRangeInputText {
Expand Down Expand Up @@ -199,6 +208,8 @@ const DatepickerRangeInput = ({
locale = 'en-US',
value = {},
errorHandler,
startInputId,
endInputId,
...rest
}: DatepickerRangeInputProps) => {
const localeObject = useLocaleObject(locale);
Expand All @@ -211,6 +222,9 @@ const DatepickerRangeInput = ({
const [error, setError] = useState({ startDate: false, endDate: false });
const displayErrorMessage = typeof errorHandler === 'string';

const startId = useGeneratedId(startInputId);
const endId = useGeneratedId(endInputId);

useEffect(() => {
if (!focusedInput && (error.startDate || error.endDate) && typeof errorHandler === 'function') {
errorHandler();
Expand Down Expand Up @@ -312,6 +326,7 @@ const DatepickerRangeInput = ({
<>
<DateRangeWrapper ref={ref} {...rest}>
<Input
id={startId}
ref={startDateRef}
autoComplete="off"
className="startDate"
Expand All @@ -328,6 +343,7 @@ const DatepickerRangeInput = ({
{focusedInput === START_DATE && <StartDateFocusedBlock />}
<DateArrow color={Colors.AUTHENTIC_BLUE_550} />
<Input
id={endId}
ref={endDateRef}
tabIndex={!inputText.startText ? -1 : 0}
autoComplete="off"
Expand Down
9 changes: 9 additions & 0 deletions src/components/Datepicker/DatepickerSingleInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Input } from '../Input/Input';
import { Datepicker } from './Datepicker';
import { isValidDateText } from './utils/isValidDateText';
import { Elevation } from '../../essentials';
import { useGeneratedId } from '../../utils/hooks/useGeneratedId';
import { HelperText } from '../HelperText/HelperText';
import { dateToDisplayText } from './utils/dateToDisplayText';
import { useLocaleObject } from './utils/useLocaleObject';
Expand Down Expand Up @@ -66,6 +67,10 @@ interface DatepickerSingleInputProps extends MarginProps, WidthProps {
* or fn to be trigger as a callback when error
*/
errorHandler?: (() => void) | string;
/**
* The id to be assigned to the input field
*/
inputId?: string;
}

const DatepickerSingleInput = ({
Expand All @@ -81,6 +86,7 @@ const DatepickerSingleInput = ({
locale = 'en-US',
value,
errorHandler,
inputId,
...rest
}: DatepickerSingleInputProps) => {
const localeObject = useLocaleObject(locale);
Expand All @@ -90,6 +96,8 @@ const DatepickerSingleInput = ({
const [error, setError] = useState(false);
const displayErrorMessage = typeof errorHandler === 'string';

const id = useGeneratedId(inputId);

useEffect(() => {
if (error && typeof errorHandler === 'function') {
errorHandler();
Expand Down Expand Up @@ -155,6 +163,7 @@ const DatepickerSingleInput = ({
inputRef.current = element;
ref.current = element;
}}
id={id}
autoComplete="off"
className="startDate"
data-testid="start-date-input"
Expand Down

0 comments on commit 2667dce

Please sign in to comment.