From 3e3ab902040b9e7e42ee282aad6d5f5a5e1a0977 Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Tue, 10 Oct 2023 18:46:57 -0500 Subject: [PATCH] Refactor. --- src/inputs/internal/ComboBoxBase.tsx | 31 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/inputs/internal/ComboBoxBase.tsx b/src/inputs/internal/ComboBoxBase.tsx index 983713350..0b0b84015 100644 --- a/src/inputs/internal/ComboBoxBase.tsx +++ b/src/inputs/internal/ComboBoxBase.tsx @@ -469,34 +469,33 @@ function getInputValue( : ""; } +/** Transforms/simplifies `optionsOrLoad` into just options, with unsetLabel maybe added. */ export function initializeOptions( optionsOrLoad: OptionsOrLoad, getOptionValue: (opt: O) => V, unsetLabel: string | undefined, ): O[] { + const opts: O[] = []; + if (unsetLabel) { + opts.push(unsetOption as unknown as O); + } if (Array.isArray(optionsOrLoad)) { - const options = optionsOrLoad; - if (!unsetLabel) { - return options; - } else { - return getOptionsWithUnset(unsetLabel, options); - } + opts.push(...optionsOrLoad); } else { const { options, current } = optionsOrLoad; - const opts: O[] = []; - if (unsetLabel) opts.push(unsetOption as unknown as O); - if (options) opts.push(...options); + if (options) { + opts.push(...options); + } // Even if the SelectField has lazy-loaded options, make sure the current value is really in there if (current) { - const found = options && options.find((o) => getOptionValue(o) === getOptionValue(current)); - if (!found) opts.push(current); + const value = getOptionValue(current); + const found = options && options.find((o) => getOptionValue(o) === value); + if (!found) { + opts.push(current); + } } - return opts; } -} - -function getOptionsWithUnset(unsetLabel: string, options: O[] | undefined): O[] { - return [unsetOption as unknown as O, ...asArray(options)]; + return opts; } /** A marker option to automatically add an "Unset" option to the start of options. */