@codecademy/gamut
72.2.272.2.3-alpha.f0a032.0
dist/Form/SelectDropdown/hooks/useSelectOptions.js+
dist/Form/SelectDropdown/hooks/useSelectOptions.jsNew file+39
Index: package/dist/Form/SelectDropdown/hooks/useSelectOptions.js
===================================================================
--- package/dist/Form/SelectDropdown/hooks/useSelectOptions.js
+++ package/dist/Form/SelectDropdown/hooks/useSelectOptions.js
@@ -0,0 +1,39 @@
+import { useMemo } from 'react';
+import { parseOptions } from '../../utils';
+import { isOptionsGrouped } from '../core/utils';
+export const useSelectOptions = ({
+ options,
+ id,
+ size,
+ value
+}) => {
+ const selectOptions = useMemo(() => {
+ if (!options || Array.isArray(options) && !options.length || typeof options === 'object' && !Array.isArray(options) && Object.keys(options).length === 0) {
+ return [];
+ }
+ if (isOptionsGrouped(options)) {
+ return options;
+ }
+ return parseOptions({
+ options,
+ id,
+ size
+ });
+ }, [options, id, size]);
+ const parsedValue = useMemo(() => {
+ if (isOptionsGrouped(selectOptions)) {
+ for (const group of selectOptions) {
+ if (group.options) {
+ const foundOption = group.options.find(option => option.value === value);
+ if (foundOption) return foundOption;
+ }
+ }
+ return undefined;
+ }
+ return selectOptions.find(option => option.value === value);
+ }, [selectOptions, value]);
+ return {
+ selectOptions,
+ parsedValue
+ };
+};
\ No newline at end of file