From 47163748d543677ca9ec24c4d120dcdf0ffcc6ea Mon Sep 17 00:00:00 2001 From: Jag Date: Sat, 17 Sep 2022 11:03:10 -0700 Subject: [PATCH] feat(menu): Open Menu with specified item index selected * Allows a user to pass in a start index to have pre-selected when a menu is opening * Cleaned up whitespace inconsistencies in menu.lua --- resource/interface/client/menu.lua | 50 ++++++++++++++-------------- web/src/features/menu/list/index.tsx | 7 ++-- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/resource/interface/client/menu.lua b/resource/interface/client/menu.lua index cc40e5c..5339114 100644 --- a/resource/interface/client/menu.lua +++ b/resource/interface/client/menu.lua @@ -3,14 +3,14 @@ local openMenu = nil local keepInput = IsNuiFocusKeepingInput() function lib.registerMenu(data, cb) - if not data.id then return error('No menu id was provided.') end - if not data.title then return error('No menu title was provided.') end - if not data.options then return error('No menu options were provided.') end - data.cb = cb - registeredMenus[data.id] = data + if not data.id then return error('No menu id was provided.') end + if not data.title then return error('No menu title was provided.') end + if not data.options then return error('No menu options were provided.') end + data.cb = cb + registeredMenus[data.id] = data end -function lib.showMenu(id) +function lib.showMenu(id, startIndex) local menu = registeredMenus[id] if not menu then @@ -38,16 +38,17 @@ function lib.showMenu(id) SetNuiFocusKeepInput(true) end - SetNuiFocus(true, false) - SendNUIMessage({ - action = 'setMenu', - data = { - position = menu.position, - canClose = menu.canClose, - title = menu.title, - items = menu.options - } - }) + SetNuiFocus(true, false) + SendNUIMessage({ + action = 'setMenu', + data = { + position = menu.position, + canClose = menu.canClose, + title = menu.title, + items = menu.options, + startItemIndex = startIndex and startIndex - 1 or 0 + } + }) end local function resetFocus() @@ -81,7 +82,7 @@ end function lib.getOpenMenu() return openMenu end RegisterNUICallback('confirmSelected', function(data, cb) - cb(1) + cb(1) data[1] += 1 -- selected if data[2] then @@ -89,8 +90,8 @@ RegisterNUICallback('confirmSelected', function(data, cb) end local menu = openMenu - - if menu.options[data[1]].close ~= false then + + if menu.options[data[1]].close ~= false then resetFocus() openMenu = nil end @@ -98,7 +99,6 @@ RegisterNUICallback('confirmSelected', function(data, cb) if menu.cb then menu.cb(data[1], data[2], menu.options[data[1]].args) end - end) RegisterNUICallback('changeIndex', function(data, cb) @@ -115,8 +115,8 @@ RegisterNUICallback('changeIndex', function(data, cb) end) RegisterNUICallback('changeSelected', function(data, cb) - cb(1) - if not openMenu.onSelected then return end + cb(1) + if not openMenu.onSelected then return end data[1] += 1 -- selected @@ -128,13 +128,13 @@ RegisterNUICallback('changeSelected', function(data, cb) end) RegisterNUICallback('closeMenu', function(data, cb) - cb(1) - resetFocus() + cb(1) + resetFocus() local menu = openMenu openMenu = nil - if menu.onClose then + if menu.onClose then menu.onClose() end end) diff --git a/web/src/features/menu/list/index.tsx b/web/src/features/menu/list/index.tsx index 1114391..5e4622a 100644 --- a/web/src/features/menu/list/index.tsx +++ b/web/src/features/menu/list/index.tsx @@ -23,6 +23,7 @@ export interface MenuSettings { title: string; canClose?: boolean; items: Array; + startItemIndex?: number; } const ListMenu: React.FC = () => { @@ -121,7 +122,9 @@ const ListMenu: React.FC = () => { useNuiEvent('closeMenu', () => closeMenu(true)); useNuiEvent('setMenu', (data: MenuSettings) => { - setSelected(0); + if (!data.startItemIndex || data.startItemIndex < 0) data.startItemIndex = 0; + else if (data.startItemIndex >= data.items.length) data.startItemIndex = data.items.length - 1; + setSelected(data.startItemIndex); if (!data.position) data.position = 'top-left'; listRefs.current = []; setMenu(data); @@ -131,7 +134,7 @@ const ListMenu: React.FC = () => { if (Array.isArray(data.items[i].values)) arrayIndexes[i] = (data.items[i].defaultIndex || 1) - 1; } setIndexStates(arrayIndexes); - listRefs.current[0]?.focus(); + listRefs.current[data.startItemIndex]?.focus(); }); return (