fix: types and nil-checks

This commit is contained in:
Linden
2022-11-21 16:45:46 +11:00
parent c88c847b18
commit d2799075ad
2 changed files with 15 additions and 7 deletions

View File

@@ -33,7 +33,7 @@ local function chatSuggestion(name, parameters, help)
} }
end end
---@param group string | string[] ---@param group string | string[] | false
---@param name string | string[] ---@param name string | string[]
---@param callback function ---@param callback function
---@param parameters table ---@param parameters table

View File

@@ -1,7 +1,7 @@
---@type { [string]: MenuProps } ---@type { [string]: MenuProps }
local registeredMenus = {} local registeredMenus = {}
---@type MenuProps | nil ---@type MenuProps | nil
local openMenu = nil local openMenu
local keepInput = IsNuiFocusKeepingInput() local keepInput = IsNuiFocusKeepingInput()
---@alias MenuPosition 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' ---@alias MenuPosition 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
@@ -11,7 +11,7 @@ local keepInput = IsNuiFocusKeepingInput()
---@field label string ---@field label string
---@field icon? string ---@field icon? string
---@field checked? boolean ---@field checked? boolean
---@field values? Array<string | { label: string, description: string }> ---@field values? table<string | { label: string, description: string }>
---@field description? string ---@field description? string
---@field defaultIndex? number ---@field defaultIndex? number
---@field args? {[any]: any} ---@field args? {[any]: any}
@@ -91,6 +91,9 @@ end
---@param onExit boolean? ---@param onExit boolean?
function lib.hideMenu(onExit) function lib.hideMenu(onExit)
local menu = openMenu local menu = openMenu
if not menu then return end
openMenu = nil openMenu = nil
if onExit and menu.onClose then if onExit and menu.onClose then
@@ -116,7 +119,7 @@ function lib.setMenuOptions(id, options, index)
end end
---@return string? ---@return string?
function lib.getOpenMenu() return openMenu?.id end function lib.getOpenMenu() return openMenu and openMenu.id end
RegisterNUICallback('confirmSelected', function(data, cb) RegisterNUICallback('confirmSelected', function(data, cb)
cb(1) cb(1)
@@ -128,6 +131,8 @@ RegisterNUICallback('confirmSelected', function(data, cb)
local menu = openMenu local menu = openMenu
if not menu then return end
if menu.options[data[1]].close ~= false then if menu.options[data[1]].close ~= false then
resetFocus() resetFocus()
openMenu = nil openMenu = nil
@@ -140,7 +145,7 @@ end)
RegisterNUICallback('changeIndex', function(data, cb) RegisterNUICallback('changeIndex', function(data, cb)
cb(1) cb(1)
if not openMenu?.onSideScroll then return end if not openMenu or not openMenu.onSideScroll then return end
data[1] += 1 -- selected data[1] += 1 -- selected
@@ -153,7 +158,7 @@ end)
RegisterNUICallback('changeSelected', function(data, cb) RegisterNUICallback('changeSelected', function(data, cb)
cb(1) cb(1)
if not openMenu?.onSelected then return end if not openMenu or not openMenu.onSelected then return end
data[1] += 1 -- selected data[1] += 1 -- selected
@@ -176,7 +181,7 @@ end)
RegisterNUICallback('changeChecked', function(data, cb) RegisterNUICallback('changeChecked', function(data, cb)
cb(1) cb(1)
if not openMenu?.onCheck then return end if not openMenu or not openMenu.onCheck then return end
data[1] += 1 -- selected data[1] += 1 -- selected
@@ -188,6 +193,9 @@ RegisterNUICallback('closeMenu', function(data, cb)
resetFocus() resetFocus()
local menu = openMenu local menu = openMenu
if not menu then return end
openMenu = nil openMenu = nil
if menu.onClose then if menu.onClose then