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

keyPressed for onClose #105

Merged
merged 3 commits into from
Sep 25, 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 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