Skip to content

Commit

Permalink
feat: onDateSelect callback functionality (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamesnelson117 authored Sep 2, 2022
1 parent a5e51a6 commit c77309c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ datePicker(selector, options, {
}
});
```

### onDateSelect

A callback that accepts a Date object when a date is selected via the date picker.

Using it in your code:
```javascript
datePicker(selector, options, {
onDateSelect: function(selectedDate) {
// do something with selected date
}
});
```

## Running locally

Clone this repository to your local machine and install dependencies.
Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/datepicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,5 +1059,30 @@ describe('Date picker', () => {

expect(highlighted === document.activeElement).toBeTruthy();
});

it('should call the onDateSelect callback with the selected date', () => {
let expectedDate;

datePicker(
document.querySelector('.date-picker'),
{},
{
onDateSelect: (date) => {
expectedDate = date;
},
},
);

const revealButton = document.querySelector('.date-picker__reveal');

$(revealButton).trigger('click');

const todayButton = document.querySelector(`[data-test-id="${today.toLocaleDateString()}"]`);
$(todayButton).trigger('click');

expect(expectedDate.getDate()).toEqual(today.getDate());
expect(expectedDate.getMonth()).toEqual(today.getMonth());
expect(expectedDate.getFullYear()).toEqual(today.getFullYear());
});
});
});
4 changes: 4 additions & 0 deletions src/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ function datePicker(datePickerElement, options = {}, callbacks = {}) {
elements.inputs.day.value = formatDateInput(date.getDate());
elements.inputs.month.value = formatDateInput(date.getMonth() + 1);
elements.inputs.year.value = date.getFullYear();

if (callbacks.onDateSelect) {
callbacks.onDateSelect(date);
}
}

function setIsPreviousEnabled() {
Expand Down

0 comments on commit c77309c

Please sign in to comment.