Skip to content

Commit a0fff9d

Browse files
szalonnamatyasf
authored andcommitted
feat(ui-date-input): add support for custom calendar icon
Closes: CLX-260
1 parent 2b2a997 commit a0fff9d

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

packages/ui-date-input/src/DateInput2/__examples__/DateInput2.examples.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
* SOFTWARE.
2323
*/
2424

25+
import React from 'react'
2526
import type { StoryConfig } from '@instructure/ui-test-utils'
27+
import { IconHeartLine } from '@instructure/ui-icons'
2628
import type { DateInput2Props } from '../props'
2729

2830
export default {
@@ -33,6 +35,10 @@ export default {
3335
[{ text: 'error example', type: 'error' }],
3436
[{ text: 'hint example', type: 'hint' }],
3537
[{ text: 'success example', type: 'success' }]
38+
],
39+
renderCalendarIcon: [
40+
null,
41+
<IconHeartLine key="custom-icon" color="error" title="Love" />
3642
]
3743
},
3844
getComponentProps: () => {

packages/ui-date-input/src/DateInput2/__new-tests__/DateInput2.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
2626
import { vi, MockInstance } from 'vitest'
2727
import userEvent from '@testing-library/user-event'
2828
import '@testing-library/jest-dom'
29+
import { IconHeartLine } from '@instructure/ui-icons'
2930

3031
import { DateInput2 } from '../index'
3132

@@ -125,6 +126,27 @@ describe('<DateInput2 />', () => {
125126
expect(calendarLabel).toBeInTheDocument()
126127
})
127128

129+
it('should render a custom calendar icon with screen reader label', async () => {
130+
const iconLabel = 'Calendar icon Label'
131+
const { container } = render(
132+
<DateInput2
133+
renderLabel="Choose a date"
134+
screenReaderLabels={{
135+
calendarIcon: iconLabel,
136+
nextMonthButton: 'Next month',
137+
prevMonthButton: 'Previous month'
138+
}}
139+
value=""
140+
renderCalendarIcon={<IconHeartLine />}
141+
/>
142+
)
143+
const calendarIcon = container.querySelector('svg[name="IconHeart"]')
144+
const calendarLabel = screen.getByText(iconLabel)
145+
146+
expect(calendarIcon).toBeInTheDocument()
147+
expect(calendarLabel).toBeInTheDocument()
148+
})
149+
128150
it('should not show calendar table by default', async () => {
129151
render(<DateInputExample />)
130152
const calendarTable = screen.queryByRole('table')

packages/ui-date-input/src/DateInput2/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
} from '@instructure/ui-icons'
3535
import { Popover } from '@instructure/ui-popover'
3636
import { TextInput } from '@instructure/ui-text-input'
37-
import { passthroughProps } from '@instructure/ui-react-utils'
37+
import { callRenderProp, passthroughProps } from '@instructure/ui-react-utils'
3838

3939
import { ApplyLocaleContext, Locale } from '@instructure/ui-i18n'
4040
import { jsx } from '@instructure/emotion'
@@ -152,6 +152,7 @@ const DateInput2 = ({
152152
placeholder,
153153
dateFormat,
154154
onRequestValidateDate,
155+
renderCalendarIcon,
155156
// margin, TODO enable this prop
156157
...rest
157158
}: DateInput2Props) => {
@@ -298,7 +299,11 @@ const DateInput2 = ({
298299
shape="circle"
299300
interaction={interaction}
300301
>
301-
<IconCalendarMonthLine />
302+
{renderCalendarIcon ? (
303+
callRenderProp(renderCalendarIcon)
304+
) : (
305+
<IconCalendarMonthLine />
306+
)}
302307
</IconButton>
303308
}
304309
isShowingContent={showPopover}

packages/ui-date-input/src/DateInput2/props.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ type DateInput2OwnProps = {
165165
utcDateString: string
166166
) => void
167167
// margin?: Spacing // TODO enable this prop
168+
169+
/**
170+
* Custom icon for the icon button opening the picker.
171+
*/
172+
renderCalendarIcon?: Renderable
168173
}
169174

170175
type PropKeys = keyof DateInput2OwnProps
@@ -195,7 +200,8 @@ const propTypes: PropValidators<PropKeys> = {
195200
timezone: PropTypes.string,
196201
withYearPicker: PropTypes.object,
197202
dateFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
198-
onRequestValidateDate: PropTypes.func
203+
onRequestValidateDate: PropTypes.func,
204+
renderCalendarIcon: PropTypes.oneOfType([PropTypes.node, PropTypes.func])
199205
}
200206

201207
export type { DateInput2Props }

0 commit comments

Comments
 (0)