goobs-frontend 0.7.65 → 0.7.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
- import { useState, useEffect, useCallback } from 'react'
1
+ 'use client'
2
+
3
+ import { useState, useCallback } from 'react'
2
4
  import { StyledComponentProps } from '..'
3
5
  import { styled, Menu, MenuItem } from '@mui/material'
4
6
  import React from 'react'
@@ -29,41 +31,26 @@ export const useDropdown = (
29
31
  inputBoxRef: React.RefObject<HTMLDivElement>,
30
32
  onOptionSelect?: (option: string) => void
31
33
  ) => {
34
+ const { componentvariant, options, value, defaultOption } = props
35
+
32
36
  /** State to control if the dropdown is open */
33
37
  const [isDropdownOpen, setIsDropdownOpen] = useState(false)
34
38
  /** State to store the anchor element for the dropdown */
35
39
  const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null)
36
40
  /** State to store the filtered options */
37
- const [filteredOptions, setFilteredOptions] = useState<string[]>([])
41
+ const [filteredOptions, setFilteredOptions] = useState<string[]>(
42
+ componentvariant === 'dropdown' && options ? [...options] : []
43
+ )
38
44
  /** State to store the currently selected option */
39
- const [selectedOption, setSelectedOption] = useState('')
45
+ const [selectedOption, setSelectedOption] = useState(() => {
46
+ if (value !== undefined) return value
47
+ if (defaultOption !== undefined) return defaultOption
48
+ if (options && options.length > 0) return options[0]
49
+ return ''
50
+ })
40
51
  /** State to track if the dropdown is focused */
41
52
  const [isDropdownFocused, setIsDropdownFocused] = useState(false)
42
53
 
43
- const { componentvariant, options, value, defaultOption } = props
44
-
45
- /**
46
- * Effect to set filtered options when component variant is dropdown and options are provided
47
- */
48
- useEffect(() => {
49
- if (componentvariant === 'dropdown' && options) {
50
- setFilteredOptions([...options])
51
- }
52
- }, [componentvariant, options])
53
-
54
- /**
55
- * Effect to set the selected option based on value, defaultOption, or first option
56
- */
57
- useEffect(() => {
58
- if (value !== undefined) {
59
- setSelectedOption(value)
60
- } else if (defaultOption !== undefined) {
61
- setSelectedOption(defaultOption)
62
- } else if (options && options.length > 0) {
63
- setSelectedOption(options[0])
64
- }
65
- }, [value, defaultOption, options])
66
-
67
54
  /**
68
55
  * Handle click on the dropdown to open/close it
69
56
  */
@@ -102,6 +89,22 @@ export const useDropdown = (
102
89
  [componentvariant]
103
90
  )
104
91
 
92
+ /**
93
+ * Update filtered options and selected option when props change
94
+ */
95
+ const updateDropdownState = useCallback(() => {
96
+ if (componentvariant === 'dropdown' && options) {
97
+ setFilteredOptions([...options])
98
+ }
99
+ if (value !== undefined) {
100
+ setSelectedOption(value)
101
+ } else if (defaultOption !== undefined) {
102
+ setSelectedOption(defaultOption)
103
+ } else if (options && options.length > 0) {
104
+ setSelectedOption(options[0])
105
+ }
106
+ }, [componentvariant, options, value, defaultOption])
107
+
105
108
  /**
106
109
  * Render the dropdown menu
107
110
  */
@@ -142,5 +145,6 @@ export const useDropdown = (
142
145
  handleOptionSelect,
143
146
  isDropdownFocused,
144
147
  handleInputFocus,
148
+ updateDropdownState,
145
149
  }
146
150
  }