Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/js/components/DateInput/DateInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const DateInput = forwardRef(
onChange,
onFocus,
plain,
reverse: reverseProp = false,
value: valueArg,
messages,
...rest
Expand Down Expand Up @@ -111,6 +112,10 @@ Use the icon prop instead.`,
);
}

const reverse = reverseProp || restOfInputProps.reverse;

const calendarDropdownAlign = { top: 'bottom', left: 'left' };

// We need to distinguish between the caller changing a Form value
// and the user typing a date that he isn't finished with yet.
// To handle this, we see if we have a value and the text value
Expand Down Expand Up @@ -213,14 +218,23 @@ Use the icon prop instead.`,
<DropButton
ref={ref}
id={id}
dropProps={{ align: { top: 'bottom', left: 'left' }, ...dropProps }}
dropProps={{ align: calendarDropdownAlign, ...dropProps }}
dropContent={calendar}
icon={icon || MaskedInputIcon || <CalendarIcon size={iconSize} />}
{...buttonProps}
/>
);
}

const calendarButton = (
<Button
onClick={open ? closeCalendar : openCalendar}
plain
icon={icon || MaskedInputIcon || <CalendarIcon size={iconSize} />}
margin={reverse ? { left: 'small' } : { right: 'small' }}
/>
);

const input = (
<FormContext.Provider
key="input"
Expand All @@ -241,6 +255,7 @@ Use the icon prop instead.`,
direction="row"
fill
>
{reverse && calendarButton}
<MaskedInput
ref={ref}
id={id}
Expand Down Expand Up @@ -297,12 +312,7 @@ Use the icon prop instead.`,
if (onFocus) onFocus(event);
}}
/>
<Button
onClick={open ? closeCalendar : openCalendar}
plain
icon={icon || MaskedInputIcon || <CalendarIcon size={iconSize} />}
margin={{ right: 'small' }}
/>
{!reverse && calendarButton}
</Box>
</Keyboard>
</FormContext.Provider>
Expand All @@ -324,8 +334,8 @@ Use the icon prop instead.`,
<Drop
overflow="visible"
id={id ? `${id}__drop` : undefined}
target={ref.current}
align={{ top: 'bottom', left: 'left', ...dropProps }}
target={containerRef.current}
align={{ ...calendarDropdownAlign, ...dropProps }}
onEsc={closeCalendar}
onClickOutside={({ target }) => {
if (
Expand Down
79 changes: 78 additions & 1 deletion src/js/components/DateInput/__tests__/DateInput-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { axe } from 'jest-axe';
import 'jest-axe/extend-expect';
import 'regenerator-runtime/runtime';
import '@testing-library/jest-dom';
import { Calendar as CalendarIcon } from 'grommet-icons';
import { Calendar as CalendarIcon, Clock as ClockIcon } from 'grommet-icons';

import { createPortal, expectPortal } from '../../../utils/portal';
import { Grommet } from '../../Grommet';
Expand Down Expand Up @@ -106,6 +106,83 @@ describe('DateInput', () => {
expect(container.firstChild).toMatchSnapshot();
});

test('reverse calendar icon', () => {
const { container } = render(
<Grommet>
<DateInput
id="item"
name="item"
format="dd/mm/yyyy"
reverse
value={DATES}
/>
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});

test('input props reverse as false', () => {
const { container } = render(
<Grommet>
<DateInput
id="item"
name="item"
format="dd/mm/yyyy"
inputProps={{ reverse: false }}
value={DATES}
/>
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});

test('input props reverse as true', () => {
const { container } = render(
<Grommet>
<DateInput
id="item"
name="item"
format="dd/mm/yyyy"
inputProps={{ reverse: true }}
value={DATES}
/>
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});

test('input props reverse as false with icon', () => {
const { container } = render(
<Grommet>
<DateInput
id="item"
name="item"
format="dd/mm/yyyy"
icon={<ClockIcon />}
inputProps={{ reverse: false }}
value={DATES}
/>
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});

test('input props reverse as true with icon', () => {
const { container } = render(
<Grommet>
<DateInput
id="item"
name="item"
format="dd/mm/yyyy"
icon={<ClockIcon />}
inputProps={{ reverse: true }}
value={DATES}
/>
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});

test('inline', () => {
const { container } = render(
<Grommet>
Expand Down
Loading