-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: APP-2730 - Implement InputDate component (#55)
- Loading branch information
Showing
13 changed files
with
145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './inputContainer'; | ||
export * from './inputDate'; | ||
export * from './inputSearch'; | ||
export * from './inputText'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { InputDate, type IInputDateProps } from './inputDate'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { InputDate } from './inputDate'; | ||
|
||
const meta: Meta<typeof InputDate> = { | ||
title: 'components/Input/InputDate', | ||
component: InputDate, | ||
tags: ['autodocs'], | ||
parameters: { | ||
design: { | ||
type: 'figma', | ||
url: 'https://www.figma.com/file/jfKRr1V9evJUp1uBeyP3Zz/v1.0.0?type=design&node-id=10080-1466&mode=design&t=2bLCEeKZ7ueBboTs-4', | ||
}, | ||
}, | ||
}; | ||
|
||
type Story = StoryObj<typeof InputDate>; | ||
|
||
/** | ||
* Default usage example of the InputDate component. | ||
*/ | ||
export const Default: Story = { | ||
args: {}, | ||
}; | ||
|
||
export default meta; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { fireEvent, render, screen, within } from '@testing-library/react'; | ||
import React from 'react'; | ||
import * as MergeRefs from 'react-merge-refs'; | ||
import { IconType } from '../../icon'; | ||
import { InputDate, type IInputDateProps } from './inputDate'; | ||
|
||
describe('<InputDate /> component', () => { | ||
const useRefMock = jest.spyOn(React, 'useRef'); | ||
const mergeRefMock = jest.spyOn(MergeRefs, 'mergeRefs'); | ||
|
||
afterEach(() => { | ||
useRefMock.mockReset(); | ||
mergeRefMock.mockReset(); | ||
}); | ||
|
||
const createTestComponent = (props?: Partial<IInputDateProps>) => { | ||
const completeProps = { ...props }; | ||
|
||
return <InputDate {...completeProps} />; | ||
}; | ||
|
||
it('renders a date input', () => { | ||
const label = 'Date label'; | ||
render(createTestComponent({ label })); | ||
const dateInput = screen.getByLabelText<HTMLInputElement>(label); | ||
expect(dateInput).toBeInTheDocument(); | ||
expect(dateInput.type).toEqual('date'); | ||
}); | ||
|
||
it('renders the input as disabled when the isDisabled property is set to true', () => { | ||
const isDisabled = true; | ||
const label = 'test'; | ||
render(createTestComponent({ label, isDisabled })); | ||
expect(screen.getByLabelText(label)).toBeDisabled(); | ||
expect(screen.getByRole('button')).toBeDisabled(); | ||
}); | ||
|
||
it('renders a button which opens the date picker on click', () => { | ||
const showPicker = jest.fn(); | ||
useRefMock.mockReturnValue({ current: { showPicker } }); | ||
mergeRefMock.mockReturnValue(() => null); | ||
render(createTestComponent()); | ||
|
||
const calendarButton = screen.getByRole('button'); | ||
expect(calendarButton).toBeInTheDocument(); | ||
expect(within(calendarButton).getByTestId(IconType.CALENDAR)).toBeInTheDocument(); | ||
|
||
fireEvent.click(calendarButton); | ||
expect(showPicker).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import classNames from 'classnames'; | ||
import { forwardRef, useRef } from 'react'; | ||
import { mergeRefs } from 'react-merge-refs'; | ||
import { Button } from '../../button'; | ||
import { IconType } from '../../icon'; | ||
import { InputContainer, type IInputComponentProps } from '../inputContainer'; | ||
import { useInputProps } from '../useInputProps'; | ||
|
||
export interface IInputDateProps extends Omit<IInputComponentProps, 'maxLength'> {} | ||
|
||
export const InputDate: React.FC<IInputDateProps> = forwardRef((props, ref) => { | ||
const { containerProps, inputProps } = useInputProps(props); | ||
|
||
const { className: containerClassName, ...otherContainerProps } = containerProps; | ||
const { className: inputClassName, disabled, ...otherInputProps } = inputProps; | ||
|
||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const handleCalendarClick = () => { | ||
inputRef.current?.showPicker(); | ||
}; | ||
|
||
return ( | ||
// Using absolute and relative positions to hide native date-picker icon on Firefox as it cannot be customised | ||
// (see Firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1812397) | ||
<InputContainer className={classNames('relative', containerClassName)} {...otherContainerProps}> | ||
<input | ||
type="date" | ||
className={classNames('absolute calendar-icon:hidden calendar-icon:appearance-none', inputClassName)} | ||
ref={mergeRefs([inputRef, ref])} | ||
disabled={disabled} | ||
{...otherInputProps} | ||
/> | ||
<Button | ||
variant="tertiary" | ||
size="sm" | ||
iconLeft={IconType.CALENDAR} | ||
className="absolute right-2" | ||
onClick={handleCalendarClick} | ||
state={disabled ? 'disabled' : undefined} | ||
/> | ||
</InputContainer> | ||
); | ||
}); | ||
|
||
InputDate.displayName = 'InputDate'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters