Skip to content

Commit

Permalink
Merge pull request #105 from BerkieBb/master
Browse files Browse the repository at this point in the history
keyPressed for onClose
  • Loading branch information
LukeWasTakenn authored Sep 25, 2022
2 parents 52728fe + 615a3cc commit 41c7688
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package/client/resource/interface/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ interface MenuProps {
options: MenuOptions[];
position?: MenuPosition;
disableInput?: boolean;
onClose?: () => void;
canClose?: boolean;
onClose?: (keyPressed?: 'Escape' | 'Backspace') => void;
onSelected?: ChangeFunction;
onSideScroll?: ChangeFunction;
}
Expand Down
11 changes: 7 additions & 4 deletions resource/interface/client/menu.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---@type { [string]: MenuProps }
local registeredMenus = {}
---@type MenuProps | nil
local openMenu = nil
local keepInput = IsNuiFocusKeepingInput()

Expand All @@ -19,7 +21,8 @@ local keepInput = IsNuiFocusKeepingInput()
---@field options MenuOptions[]
---@field position? MenuPosition
---@field disableInput? boolean
---@field onClose? fun()
---@field canClose? boolean
---@field onClose? fun(keyPressed?: 'Escape' | 'Backspace')
---@field onSelected? MenuChangeFunction
---@field onSideScroll? MenuChangeFunction

Expand All @@ -45,7 +48,7 @@ function lib.showMenu(id, startIndex)
if not openMenu then
CreateThread(function()
while openMenu do
if not openMenu.disableInput then
if openMenu.disableInput == nil or openMenu.disableInput then
DisablePlayerFiring(cache.playerId, true)
HudWeaponWheelIgnoreSelection()
DisableControlAction(0, 140, true)
Expand Down Expand Up @@ -165,6 +168,6 @@ RegisterNUICallback('closeMenu', function(data, cb)
openMenu = nil

if menu.onClose then
menu.onClose()
menu.onClose(data --[[@as 'Escape' | 'Backspace' | nil]])
end
end)
end)
6 changes: 3 additions & 3 deletions web/src/features/menu/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ const ListMenu: React.FC = () => {
const [indexStates, setIndexStates] = useState<Record<number, number>>({});
const listRefs = useRef<Array<HTMLDivElement | null>>([]);

const closeMenu = (ignoreFetch?: boolean) => {
const closeMenu = (ignoreFetch?: boolean, keyPressed?: string) => {
if (menu.canClose === false) return;
setVisible(false);
if (!ignoreFetch) fetchNui('closeMenu');
if (!ignoreFetch) fetchNui('closeMenu', keyPressed);
};

const moveMenu = (e: React.KeyboardEvent<HTMLDivElement>) => {
Expand Down Expand Up @@ -111,7 +111,7 @@ const ListMenu: React.FC = () => {
if (!visible) return;

const keyHandler = (e: KeyboardEvent) => {
if (['Escape', 'Backspace'].includes(e.code)) closeMenu();
if (['Escape', 'Backspace'].includes(e.code)) closeMenu(false, e.code);
};

window.addEventListener('keydown', keyHandler);
Expand Down

0 comments on commit 41c7688

Please sign in to comment.