Skip to content

Commit 1718305

Browse files
authored
Pipeline Versions (#32)
* Extends dropdown to allow additional itmes appended * Adds disabled flag to local storage dropdown * Adds force open option
1 parent a90abdd commit 1718305

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

components/KernDropdown.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, useEffect, useMemo, useRef, useState } from 'react'
1+
import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from 'react'
22
import { Menu, Transition } from '@headlessui/react'
33
import { KernDropdownProps } from '../types/dropdown';
44
import { combineClassNames } from '../../javascript-functions/general';
@@ -10,6 +10,7 @@ import useOnClickOutside from '../hooks/useHooks/useOnClickOutside';
1010
import { useDefaults } from '../hooks/useDefaults';
1111
import SVGIcon from './SVGIcon';
1212
import { CSSProperties } from 'react';
13+
import useRefFor from '../hooks/useRefFor';
1314

1415
const DEFAULTS = { fontSizeClass: 'text-xs' };
1516

@@ -20,12 +21,16 @@ export default function KernDropdown(props: KernDropdownProps) {
2021
const [disabledOptions, setDisabledOptions] = useState<boolean[]>([]);
2122
const [backgroundColors, setBackgroundColors] = useState<string[]>([]);
2223
const [searchText, setSearchText] = useState('');
23-
const [isOpen, setIsOpen] = useState(false);
24+
const [isOpen, setIsOpen_] = useState(false);
2425
const [selectedCheckboxes, setSelectedCheckboxes] = useState<any[]>([]);
2526
const [position, setPosition] = useState(null);
2627
const [savedIndex, setSavedIndex] = useState(null);
2728

2829
const [defaultProps] = useDefaults<{ fontSizeClass: string }>(props, DEFAULTS);
30+
const forceOpenRef = useRefFor(props.forceOverwriteOpen);
31+
32+
// catches all isOpen changes & sets the value if it is not forced to be open
33+
const setIsOpen = useCallback((value: boolean) => { if ((!value && !forceOpenRef.current) || value) setIsOpen_(value) }, [])
2934

3035
const dropdownRef = useRef(null);
3136
useOnClickOutside(dropdownRef, () => setIsOpen(false));
@@ -238,6 +243,7 @@ export default function KernDropdown(props: KernDropdownProps) {
238243
backgroundColors[index], props.useDifferentTextColor && props.useDifferentTextColor[index] ? 'text-' + props.differentTextColor + '-700' : active && !backgroundColors[index] ? "bg-gray-100 text-gray-900" : "text-gray-700",
239244
props.iconsArray && props.iconsArray[index] ? "px-2" : "px-4",
240245
defaultProps.fontSizeClass,
246+
props.dropdownAdd ? "inline-flex w-full justify-between" : "",
241247
"py-2 flex items-center"
242248
)}
243249
onClick={() => {
@@ -260,6 +266,7 @@ export default function KernDropdown(props: KernDropdownProps) {
260266
<span className='truncate'>{option}</span>
261267
{props.onClickDelete && <div className="ml-auto flex items-center cursor-pointer hover:bg-gray-200" onClick={(e) => { e.stopPropagation(); props.onClickDelete(option) }}><IconTrashXFilled size={20} /></div>}
262268
{props.optionsHaveLink && <a href={props.linkList[index]} target="_blank" className="h-4 w-4 mr-2 ml-auto flex items-center cursor-pointer"><IconExternalLink size={16} /></a>}
269+
{props.dropdownAdd && props.dropdownAdd[index]}
263270
</label>
264271
</Tooltip>
265272
</div>

components/LocalStorageDropdown.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export const LocalStorageDropdown = forwardRef((_props: LocalStorageDropdownProp
113113
setInputText(inputText)
114114
onOptionSelected(inputText)
115115
}}
116+
disabled={props.disabled}
116117

117118
/> :
118119

@@ -121,7 +122,8 @@ export const LocalStorageDropdown = forwardRef((_props: LocalStorageDropdownProp
121122
}}
122123
className="h-9 w-full text-sm border-gray-300 rounded-md placeholder-italic border text-gray-900 pl-4 placeholder:text-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-300 focus:ring-offset-2 focus:ring-offset-gray-100"
123124
placeholder="Enter value..."
124-
onFocus={(event) => event.target.select()} />
125+
onFocus={(event) => event.target.select()}
126+
disabled={props.disabled} />
125127
}
126128

127129

types/dropdown.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
* @fontSizeClass {string} - The font size of the dropdown items
4040
* @ignoreDisabledForSearch {boolean} - If the search bar should ignore the disabled options
4141
* @positionDropdown {string} - The position of the dropdown
42+
* @dropdownAdd {JSX.Element} - array of JSX elements that will be added to the dropdown items
43+
* @forceOverwriteOpen {boolean} - forces the dropdown to stay open until set to false/undefined
4244
*/
4345
export type KernDropdownProps = {
4446
buttonName?: string;
@@ -84,6 +86,8 @@ export type KernDropdownProps = {
8486
ignoreDisabledForSearch?: boolean;
8587
positionDropdown?: "top" | "bottom" | "left" | "right";
8688
scrollAfterNOptions?: number;
89+
dropdownAdd?: JSX.Element[];
90+
forceOverwriteOpen?: boolean;
8791
}
8892

8993
export type AppSelectionDropdownProps = {

types/localStorageDropdown.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type LocalStorageDropdownProps = {
1919
onOptionSelected?: (option: string) => void;
2020
searchDefaultValue?: string;
2121
excludedFromStorage?: { values: string[]; compareOptions?: CompareOptions[] }; // if the value is in this list it will not be added to the storage //setting this to null will assume all values are valid
22+
disabled?: boolean;
2223
}
2324

2425

0 commit comments

Comments
 (0)