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

datepicker: add yearRangeReverse property sort year in reverse order #6521

Open
wants to merge 2 commits into
base: v1-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion jade/page-contents/pickers_content.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ <h5>Options</h5>
<td>10</td>
<td>Number of years either side, or array of upper/lower range.</td>
</tr>
<tr>
<td>yearRangeReverse</td>
<td>Boolean</td>
<td>false</td>
<td>Sort year range in reverse order</td>
</tr>
<tr>
<td>isRTL</td>
<td>Boolean</td>
Expand Down Expand Up @@ -760,4 +766,4 @@ <h5 class="method-header">
</div>

</div>
</div>
</div>
10 changes: 6 additions & 4 deletions js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,7 @@
}
return (
`<td data-day="${opts.day}" class="${arr.join(' ')}" aria-selected="${ariaSelected}">` +
`<button class="datepicker-day-button" type="button" data-year="${opts.year}" data-month="${
opts.month
}" data-day="${opts.day}">${opts.day}</button>` +
`<button class="datepicker-day-button" type="button" data-year="${opts.year}" data-month="${opts.month}" data-day="${opts.day}">${opts.day}</button>` +
'</td>'
);
}
Expand Down Expand Up @@ -604,11 +602,15 @@
j = 1 + year + opts.yearRange;
}

let years = [];
for (arr = []; i < j && i <= opts.maxYear; i++) {
if (i >= opts.minYear) {
arr.push(`<option value="${i}" ${i === year ? 'selected="selected"' : ''}>${i}</option>`);
years.push(
`<option value="${i}" ${i === year ? 'selected="selected"' : ''}>${i}</option>`
);
}
}
arr = arr.concat(opts.yearRangeReverse ? years.reverse() : years);
Copy link
Contributor

@DanielRuf DanielRuf May 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why arr.concat is used here. You can probably leave the old code untouched and use one line with this:

if(opts.yearRangeReverse) {
  arr.reverse();
}

Which will reverse the array elements in place.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might also want to add it as default at the top.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielRuf I fixed the overhead.
Pls. what do you mean by add it as default at the top ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/Dogfalo/materialize/blob/v1-dev/js/datepicker.js#L4, the defaults are readable and visible when you output a component instance on the console.


yearHtml = `<select class="datepicker-select orig-select-year" tabindex="-1">${arr.join(
''
Expand Down