mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +01:00
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
This commit is contained in:
@@ -10,7 +10,7 @@ function lib.registerMenu(data, cb)
|
||||
registeredMenus[data.id] = data
|
||||
end
|
||||
|
||||
function lib.showMenu(id)
|
||||
function lib.showMenu(id, startIndex)
|
||||
local menu = registeredMenus[id]
|
||||
|
||||
if not menu then
|
||||
@@ -45,7 +45,8 @@ function lib.showMenu(id)
|
||||
position = menu.position,
|
||||
canClose = menu.canClose,
|
||||
title = menu.title,
|
||||
items = menu.options
|
||||
items = menu.options,
|
||||
startItemIndex = startIndex and startIndex - 1 or 0
|
||||
}
|
||||
})
|
||||
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)
|
||||
|
||||
@@ -23,6 +23,7 @@ export interface MenuSettings {
|
||||
title: string;
|
||||
canClose?: boolean;
|
||||
items: Array<MenuItem>;
|
||||
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 (
|
||||
|
||||
Reference in New Issue
Block a user