Skip to content

Enable setting the first option in the list as active. #1796

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<!-- Feel free to put either your handle and/or full name, according to
your privacy needs -->

## v0.13.1 · 21 04 2022

* Added option to enable setting the first option in the list as active.

*@joshuan92*

## v0.13.0 · 03 11 2020

* Support for Bootstrap v4.x.
Expand Down
37 changes: 20 additions & 17 deletions dist/js/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -1705,23 +1705,25 @@
}
}

// activate
self.hasOptions = results.items.length > 0 || ( has_create_option && self.settings.showAddOptionOnCreate );
if (self.hasOptions) {
if (results.items.length > 0) {
$active_before = active_before && self.getOption(active_before);
if (results.query !== "" && $active_before && $active_before.length) {
$active = $active_before;
} else if (self.settings.mode === 'single' && self.items.length) {
$active = self.getOption(self.items[0]);
}
if (!$active || !$active.length) {
if ($create && !self.settings.addPrecedence) {
$active = self.getAdjacentOption($create, 1);
} else {
$active = $dropdown_content.find('[data-selectable]:first');
}
}
// activate
self.hasOptions = results.items.length > 0 || ( has_create_option && self.settings.showAddOptionOnCreate ) || self.settings.setFirstOptionActive;
if (self.hasOptions) {
if (results.items.length > 0) {
$active_before = active_before && self.getOption(active_before);
if (results.query !== "" && self.settings.setFirstOptionActive) {
$active = $dropdown_content.find('[data-selectable]:first')
} else if (results.query !== "" && $active_before && $active_before.length) {
$active = $active_before;
} else if (self.settings.mode === 'single' && self.items.length) {
$active = self.getOption(self.items[0]);
}
if (!$active || !$active.length) {
if ($create && !self.settings.addPrecedence) {
$active = self.getAdjacentOption($create, 1);
} else {
$active = $dropdown_content.find('[data-selectable]:first');
}
}
} else {
$active = $create;
}
Expand Down Expand Up @@ -2839,6 +2841,7 @@
showEmptyOptionInDropdown: false,
emptyOptionLabel: '--',
closeAfterSelect: false,
setFirstOptionActive: false,

scrollDuration: 60,
deselectBehavior: 'previous', //top, previous
Expand Down
2 changes: 1 addition & 1 deletion dist/js/selectize.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/standalone/selectize.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/standalone/selectize.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ $(function() {
<td valign="top"><code>boolean</code></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>setFirstOptionActive</code></td>
<td valign="top">Enable setting the first option in the list as active.</td>
<td valign="top"><code>boolean</code></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<th valign="top" colspan="4" align="left"><a href="#data_searching" name="data_searching">Data / Searching</a></th>
</tr>
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ declare namespace Selectize {
*/
copyClassesToDropdown?: boolean;

/**
* Sets always the first option in the list as active.
*
* Default: false
*/
setFirstOptionActive?: boolean;

// Callbacks
// ------------------------------------------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Selectize.defaults = {
showEmptyOptionInDropdown: false,
emptyOptionLabel: '--',
closeAfterSelect: false,
setFirstOptionActive: false,

scrollDuration: 60,
deselectBehavior: 'previous', //top, previous
Expand Down
36 changes: 19 additions & 17 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -1200,23 +1200,25 @@ $.extend(Selectize.prototype, {
}
}

// activate
self.hasOptions = results.items.length > 0 || ( has_create_option && self.settings.showAddOptionOnCreate );
if (self.hasOptions) {
if (results.items.length > 0) {
$active_before = active_before && self.getOption(active_before);
if (results.query !== "" && $active_before && $active_before.length) {
$active = $active_before;
} else if (self.settings.mode === 'single' && self.items.length) {
$active = self.getOption(self.items[0]);
}
if (!$active || !$active.length) {
if ($create && !self.settings.addPrecedence) {
$active = self.getAdjacentOption($create, 1);
} else {
$active = $dropdown_content.find('[data-selectable]:first');
}
}
// activate
self.hasOptions = results.items.length > 0 || ( has_create_option && self.settings.showAddOptionOnCreate ) || self.settings.setFirstOptionActive;
Copy link
Contributor

Choose a reason for hiding this comment

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

TODO: We should look at a cleanup of these if checks into something that is both easier to understand as a dev, and avoids edge cases

CHORE (testing, documentation): Do you have any updates to unit tests or an example that can show the expected behavior?

if (self.hasOptions) {
if (results.items.length > 0) {
$active_before = active_before && self.getOption(active_before);
if (results.query !== "" && self.settings.setFirstOptionActive) {
$active = $dropdown_content.find('[data-selectable]:first')
} else if (results.query !== "" && $active_before && $active_before.length) {
$active = $active_before;
} else if (self.settings.mode === 'single' && self.items.length) {
$active = self.getOption(self.items[0]);
}
if (!$active || !$active.length) {
if ($create && !self.settings.addPrecedence) {
$active = self.getAdjacentOption($create, 1);
} else {
$active = $dropdown_content.find('[data-selectable]:first');
}
}
} else {
$active = $create;
}
Expand Down