Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui-date-input): add support for custom calendar icon #1816

Merged
merged 1 commit into from
Dec 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* SOFTWARE.
*/

import React from 'react'
import type { StoryConfig } from '@instructure/ui-test-utils'
import { IconHeartLine } from '@instructure/ui-icons'
import type { DateInput2Props } from '../props'

export default {
Expand All @@ -33,6 +35,10 @@ export default {
[{ text: 'error example', type: 'error' }],
[{ text: 'hint example', type: 'hint' }],
[{ text: 'success example', type: 'success' }]
],
renderCalendarIcon: [
null,
<IconHeartLine key="custom-icon" color="error" title="Love" />
]
},
getComponentProps: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { vi, MockInstance } from 'vitest'
import userEvent from '@testing-library/user-event'
import '@testing-library/jest-dom'
import { IconHeartLine } from '@instructure/ui-icons'

import { DateInput2 } from '../index'

Expand Down Expand Up @@ -125,6 +126,27 @@ describe('<DateInput2 />', () => {
expect(calendarLabel).toBeInTheDocument()
})

it('should render a custom calendar icon with screen reader label', async () => {
const iconLabel = 'Calendar icon Label'
const { container } = render(
<DateInput2
renderLabel="Choose a date"
screenReaderLabels={{
calendarIcon: iconLabel,
nextMonthButton: 'Next month',
prevMonthButton: 'Previous month'
}}
value=""
renderCalendarIcon={<IconHeartLine />}
/>
)
const calendarIcon = container.querySelector('svg[name="IconHeart"]')
const calendarLabel = screen.getByText(iconLabel)

expect(calendarIcon).toBeInTheDocument()
expect(calendarLabel).toBeInTheDocument()
})

it('should not show calendar table by default', async () => {
render(<DateInputExample />)
const calendarTable = screen.queryByRole('table')
Expand Down
9 changes: 7 additions & 2 deletions packages/ui-date-input/src/DateInput2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from '@instructure/ui-icons'
import { Popover } from '@instructure/ui-popover'
import { TextInput } from '@instructure/ui-text-input'
import { passthroughProps } from '@instructure/ui-react-utils'
import { callRenderProp, passthroughProps } from '@instructure/ui-react-utils'

import { ApplyLocaleContext, Locale } from '@instructure/ui-i18n'
import { jsx } from '@instructure/emotion'
Expand Down Expand Up @@ -152,6 +152,7 @@ const DateInput2 = ({
placeholder,
dateFormat,
onRequestValidateDate,
renderCalendarIcon,
// margin, TODO enable this prop
...rest
}: DateInput2Props) => {
Expand Down Expand Up @@ -298,7 +299,11 @@ const DateInput2 = ({
shape="circle"
interaction={interaction}
>
<IconCalendarMonthLine />
{renderCalendarIcon ? (
callRenderProp(renderCalendarIcon)
) : (
<IconCalendarMonthLine />
)}
</IconButton>
}
isShowingContent={showPopover}
Expand Down
8 changes: 7 additions & 1 deletion packages/ui-date-input/src/DateInput2/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ type DateInput2OwnProps = {
utcDateString: string
) => void
// margin?: Spacing // TODO enable this prop

/**
* Custom icon for the icon button opening the picker.
*/
renderCalendarIcon?: Renderable
}

type PropKeys = keyof DateInput2OwnProps
Expand Down Expand Up @@ -195,7 +200,8 @@ const propTypes: PropValidators<PropKeys> = {
timezone: PropTypes.string,
withYearPicker: PropTypes.object,
dateFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
onRequestValidateDate: PropTypes.func
onRequestValidateDate: PropTypes.func,
renderCalendarIcon: PropTypes.oneOfType([PropTypes.node, PropTypes.func])
}

export type { DateInput2Props }
Expand Down
Loading