Skip to content

Commit 2a293d5

Browse files
committed
Fixes #7188: Re-add missing support for null_option on API select
1 parent 4a13ee6 commit 2a293d5

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

docs/release-notes/version-3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [#7164](https://github.com/netbox-community/netbox/issues/7164) - Fix styling of "decommissioned" label for circuits
99
* [#7169](https://github.com/netbox-community/netbox/issues/7169) - Fix CSV import file upload
1010
* [#7176](https://github.com/netbox-community/netbox/issues/7176) - Fix issue where query parameters were duplicated across different forms of the same type
11+
* [#7188](https://github.com/netbox-community/netbox/issues/7188) - Fix issue where select fields with `null_option` did not render or send the null option
1112
* [#7193](https://github.com/netbox-community/netbox/issues/7193) - Fix prefix (flat) template issue when viewing child prefixes with prefixes available
1213

1314
---

netbox/project-static/dist/netbox.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/dist/netbox.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/src/select/api/apiSelect.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export class APISelect {
5858
*/
5959
public readonly emptyOption: Option;
6060

61+
/**
62+
* Null option. When `data-null-option` attribute is a string, the value is used to created an
63+
* option of type `{text: '<value from data-null-option>': 'null'}`.
64+
*/
65+
public readonly nullOption: Nullable<Option> = null;
66+
6167
/**
6268
* Event that will initiate the API call to NetBox to load option data. By default, the trigger
6369
* is `'load'`, so data will be fetched when the element renders on the page.
@@ -197,6 +203,14 @@ export class APISelect {
197203
this.emptyOption = EMPTY_PLACEHOLDER;
198204
}
199205

206+
const nullOption = base.getAttribute('data-null-option');
207+
if (isTruthy(nullOption)) {
208+
this.nullOption = {
209+
text: nullOption,
210+
value: 'null',
211+
};
212+
}
213+
200214
this.slim = new SlimSelect({
201215
select: this.base,
202216
allowDeselect: true,
@@ -291,8 +305,15 @@ export class APISelect {
291305
*/
292306
private set options(optionsIn: Option[]) {
293307
let newOptions = optionsIn;
308+
// Ensure null option is present, if it exists.
309+
if (this.nullOption !== null) {
310+
newOptions = [this.nullOption, ...newOptions];
311+
}
312+
// Sort options unless this element is pre-sorted.
294313
if (!this.preSorted) {
295-
newOptions = optionsIn.sort((a, b) => (a.text.toLowerCase() > b.text.toLowerCase() ? 1 : -1));
314+
newOptions = newOptions.sort((a, b) =>
315+
a.text.toLowerCase() > b.text.toLowerCase() ? 1 : -1,
316+
);
296317
}
297318
// Deduplicate options each time they're set.
298319
const deduplicated = uniqueByProperty(newOptions, 'value');

0 commit comments

Comments
 (0)