Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(listbox): prop to disable confirming selections on blur events #866

Merged
merged 5 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's just my personal preference, but I think it feels nicer calling it just confirmOnBlur.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure I can change

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or actually I just merged the other PR so easier to stick with this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fine, just a detail

} = 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