@codecademy/gamut

72.2.272.2.3-alpha.f0a032.0
dist/Form/SelectDropdown/core/utils.js
+dist/Form/SelectDropdown/core/utils.jsNew file
+61
Index: package/dist/Form/SelectDropdown/core/utils.js
===================================================================
--- package/dist/Form/SelectDropdown/core/utils.js
+++ package/dist/Form/SelectDropdown/core/utils.js
@@ -0,0 +1,61 @@
+export const isMultipleSelectProps = props => !!props.multiple;
+export const isSingleSelectProps = props => !props.multiple;
+/**
+ * Resolves the value for a newly created option from react-select action metadata
+ * or the onChange option payload. Returns undefined when no reliable value exists.
+ */
+export const getCreatedOptionValue = (optionEvent, actionMeta, multiple) => {
+  const metaValue = actionMeta.option?.value;
+  if (metaValue) return metaValue;
+  if (!multiple) {
+    const {
+      value
+    } = optionEvent;
+    return value || undefined;
+  }
+  const newOption = optionEvent.find(option => option.__isNew__);
+  return newOption?.value || undefined;
+};
+export const isOptionGroup = obj => obj != null && typeof obj === 'object' && 'options' in obj && obj.options !== undefined;
+export const isOptionsGrouped = options => Array.isArray(options) && options.some(option => isOptionGroup(option));
+
+/**
+ * Filters options based on the selected value(s).
+ * Handles both single values and arrays of values, and works with both
+ * flat option arrays and grouped options.
+ *
+ * @param options - The options to filter from
+ * @param value - The value or values to filter by
+ * @param optionsAreGrouped - Whether the options are grouped
+ * @returns Array of matching options
+ */
+export const filterValueFromOptions = (options, value, optionsAreGrouped) => {
+  if (optionsAreGrouped) {
+    return options.map(optionGroup => optionGroup.options.filter(option => option.value === value || value?.includes(option.value))).flat();
+  }
+  return options.filter(option => option.value === value || value?.includes(option.value));
+};
+
+/**
+ * Removes a value from the selected options array.
+ * Handles both single values and arrays of values to remove.
+ *
+ * @param selectedOptions - The currently selected options
+ * @param value - The value or values to remove
+ * @returns New array with the specified values removed
+ */
+export const resolveNoOptionsMessage = validationMessage => {
+  if (validationMessage === undefined) return undefined;
+  if (typeof validationMessage === 'function') {
+    return validationMessage;
+  }
+  return () => validationMessage;
+};
+export const removeValueFromSelectedOptions = (selectedOptions, value) => {
+  return selectedOptions.filter(option => {
+    if (Array.isArray(value)) {
+      return !value.includes(option.value);
+    }
+    return option.value !== value;
+  });
+};
\ No newline at end of file