Skip to content

Commit

Permalink
fix(listbox): prop to disable confirming selections on blur events (#866
Browse files Browse the repository at this point in the history
)
  • Loading branch information
haxxmaxx authored Jun 27, 2022
1 parent 88fa194 commit d680826
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion apis/nucleus/src/components/listbox/ListBoxInline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function ListBoxInline({ app, fieldIdentifier, stateName = '$', o
sortByState = 1,
scrollState = undefined,
setCount = undefined,
shouldConfirmOnBlur = undefined,
} = options;
let { frequencyMode, histogram = false } = options;

Expand Down Expand Up @@ -210,7 +211,7 @@ export default function ListBoxInline({ app, fieldIdentifier, stateName = '$', o
}, [selections]);

const listBoxRef = useRef(null);
useConfirmUnfocus(listBoxRef, selections);
useConfirmUnfocus(listBoxRef, selections, shouldConfirmOnBlur);

useEffect(() => {
if (!searchContainer || !searchContainer.current) {
Expand Down
6 changes: 4 additions & 2 deletions apis/nucleus/src/components/listbox/useConfirmUnfocus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useRef, useEffect, useState } from 'react';
* user interact outside of passed element ref.
*/

export default function useConfirmUnfocus(ref, selections) {
export default function useConfirmUnfocus(ref, selections, shouldConfirmOnBlur) {
const [_isConfirmed, _setIsConfirmed] = useState(false);
// Wrap state in ref to use inside eventListener callback
const isConfirmedRef = useRef(_isConfirmed);
Expand All @@ -15,6 +15,8 @@ export default function useConfirmUnfocus(ref, selections) {
};

useEffect(() => {
if (!shouldConfirmOnBlur) return undefined;

const handleEvent = (event) => {
const interactInside = ref.current && ref.current.contains(event.target);
const isConfirmed = isConfirmedRef.current;
Expand All @@ -32,5 +34,5 @@ export default function useConfirmUnfocus(ref, selections) {
document.removeEventListener('mousedown', handleEvent);
document.removeEventListener('keydown', handleEvent);
};
}, [ref, selections]);
}, [ref, selections, shouldConfirmOnBlur]);
}
1 change: 1 addition & 0 deletions apis/nucleus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const mergeConfigs = (base, c) => ({
* @property {{setScrollPos:function(number):void, initScrollPos:number}} [options.scrollState=] Object including a setScrollPos function that sets current scroll position index. A initial scroll position index.
* @property {number=} [options.sortByState=1] Sort by state, detault 1 = sort descending, 0 = no sorting, -1 sort ascending.
* @property {function(number):void} [options.setCount=] A function that gets called with the length of the data in the Listbox.
* @property {boolean} [options.shouldConfirmOnBlur=] A boolean that determines if the listbox should actively confirm selections on blur events.
*/

/**
Expand Down

0 comments on commit d680826

Please sign in to comment.