Skip to content

Commit

Permalink
fix(DatePicker): add return object for onBlur (#3178)
Browse files Browse the repository at this point in the history
  • Loading branch information
joakbjerk authored Jan 8, 2024
1 parent 2ca216a commit 18b3889
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export const DatePickerRange = () => (
on_cancel={({ start_date, end_date }) => {
console.log('on_cancel', start_date, end_date)
}}
onBlur={({ start_date, end_date }) => {
console.log('onBlur', start_date, end_date)
}}
shortcuts={[
{
title: 'Set date period',
Expand Down Expand Up @@ -96,6 +99,9 @@ export const DatePickerWithInput = () => (
on_cancel={({ date }) => {
console.log('on_cancel', date)
}}
onBlur={({ date }) => {
console.log('onBlur', date)
}}
/>
</ComponentBox>
)
Expand All @@ -113,6 +119,9 @@ export const DatePickerTrigger = () => (
on_show={({ date }) => {
console.log('on_show', date)
}}
onBlur={({ start_date, end_date }) => {
console.log('onBlur', start_date, end_date)
}}
/>
</ComponentBox>
</Wrapper>
Expand All @@ -135,6 +144,9 @@ export const DatePickerHiddenNav = () => (
on_hide={({ date }) => {
console.log('on_hide', date)
}}
onBlur={({ date }) => {
console.log('onBlur', date)
}}
/>
</ComponentBox>
)
Expand Down
12 changes: 11 additions & 1 deletion packages/dnb-eufemia/src/components/date-picker/DatePicker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ type DatePickerSuffix =
| React.ReactNode;
type DatePickerDirection = 'auto' | 'top' | 'bottom';
type DatePickerAlignPicker = 'auto' | 'left' | 'right';

// Make it possible to join React.Event interfaces with DatePickerEvent type.
type DatePickerEvent<T extends T> = T & {
date?: string;
start_date?: string;
end_date?: string;
};

export interface DatePickerProps
extends Omit<React.HTMLProps<HTMLElement>, 'ref', 'onBlur'>,
SpacingProps {
Expand Down Expand Up @@ -247,7 +255,9 @@ export interface DatePickerProps
/**
* Will be called once the input loses focus.
*/
onBlur?: (event: React.FocusEventHandler<HTMLInputElement>) => void;
onBlur?: (
event: DatePickerEvent<React.FocusEvent<HTMLInputElement>>
) => void;
}
export default class DatePicker extends React.Component<
DatePickerProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,18 @@ export default class DatePickerInput extends React.PureComponent {
})
}

onBlurHandler = (e) => {
onBlurHandler = (event) => {
this.focusMode = null
this.setState({
focusState: 'blur',
_listenForPropChanges: false,
})

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ describe('DatePicker component', () => {

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

const [firstInput, secondInput]: Array<HTMLInputElement> = Array.from(
document.querySelectorAll('.dnb-input__input')
Expand All @@ -1512,7 +1512,7 @@ describe('DatePicker component', () => {
expect(document.activeElement).not.toBe(firstInput)
expect(onBlur).toHaveBeenCalledTimes(1)
expect(onBlur).toHaveBeenCalledWith(
expect.objectContaining({ target: firstInput })
expect.objectContaining({ target: firstInput, date: '2024-01-05' })
)

await userEvent.click(secondInput)
Expand All @@ -1525,7 +1525,7 @@ describe('DatePicker component', () => {
expect(document.activeElement).not.toBe(secondInput)
expect(onBlur).toHaveBeenCalledTimes(2)
expect(onBlur).toHaveBeenCalledWith(
expect.objectContaining({ target: secondInput })
expect.objectContaining({ target: secondInput, date: '2024-01-05' })
)
})

Expand Down

0 comments on commit 18b3889

Please sign in to comment.