Skip to content

Commit

Permalink
feat(DatePicker): add onFocus event (#3188)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Jan 8, 2024
1 parent eb6417f commit 2062213
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DatePickerDateFnsRangeIsWeekend } from 'Docs/uilib/components/date-pick
| `on_show` | _(optional)_ will be called once date-picker is visible. |
| `on_hide` | _(optional)_ will be called once date-picker is hidden. |
| `on_days_render` | _(optional)_ will be called right before every new calendar view gets rendered. See the example above. |
| `onFocus` | _(optional)_ will be called once the input gets focus. |
| `onBlur` | _(optional)_ will be called once the input lose focus. |

## Returned Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ export interface DatePickerProps
* Will be called once a user presses the reset button.
*/
on_reset?: (...args: any[]) => any;
/**
* Will be called once the input gets focus.
*/
onFocus?: (event: React.FocusEventHandler<HTMLInputElement>) => void;
/**
* Will be called once the input loses focus.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@ export default class DatePicker extends React.PureComponent {
locale={locale}
{...attributes}
submitAttributes={submitParams}
onFocus={this.showPicker}
onSubmit={this.togglePicker}
{...status_props}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ export default class DatePickerInput extends React.PureComponent {
focusState: 'focus',
_listenForPropChanges: false,
})

if (this.props.onFocus) {
this.props.onFocus({
...event,
...this.context.getReturnObject({ event }),
})
}
}

onBlurHandler = (event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,41 @@ describe('DatePicker component', () => {
expect(thirdDateButton.children[2]).toHaveTextContent('24')
})

it('should fire fire event when input gets focus', async () => {
const onFocus = jest.fn()
render(<DatePicker show_input onFocus={onFocus} date="2024-01-05" />)

const [firstInput, secondInput]: Array<HTMLInputElement> = Array.from(
document.querySelectorAll('.dnb-input__input')
)

await userEvent.click(firstInput)

expect(onFocus).toHaveBeenCalledTimes(1)
expect(document.activeElement).toBe(firstInput)

await userEvent.click(document.body)

expect(document.activeElement).not.toBe(firstInput)
expect(onFocus).toHaveBeenCalledTimes(1)
expect(onFocus).toHaveBeenCalledWith(
expect.objectContaining({ target: firstInput, date: '2024-01-05' })
)

await userEvent.click(secondInput)

expect(onFocus).toHaveBeenCalledTimes(2)
expect(document.activeElement).toBe(secondInput)

await userEvent.click(document.body)

expect(document.activeElement).not.toBe(secondInput)
expect(onFocus).toHaveBeenCalledTimes(2)
expect(onFocus).toHaveBeenCalledWith(
expect.objectContaining({ target: secondInput, date: '2024-01-05' })
)
})

it('should fire blur event when input loses focus', async () => {
const onBlur = jest.fn()
render(<DatePicker show_input onBlur={onBlur} date="2024-01-05" />)
Expand Down

0 comments on commit 2062213

Please sign in to comment.