Skip to content

Commit

Permalink
feat: add global_press_key to checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom324 committed Dec 23, 2024
1 parent caecfe2 commit 9913345
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/pages/docs/components/checkbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local signal = n.create_signal({
n.checkbox({
label = "Display preview",
value = signal.is_preview_visible,
global_press_key = "<C-d>"
on_change = function(is_checked)
signal.is_preview_visible = is_checked
end,
Expand Down Expand Up @@ -65,6 +66,12 @@ n.checkbox({
types={['string']}
/>

#### global_press_key

<Property
types={['string[]', 'string']}
/>

#### prepare_lines

<Property
Expand Down
16 changes: 14 additions & 2 deletions lua/nui-components/checkbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function Checkbox:prop_types()
value = "boolean",
checked_sign = "string",
default_sign = "string",
global_press_key = { "table", "string", "nil" },
})
end

Expand All @@ -43,9 +44,20 @@ function Checkbox:mappings()
props.on_change(value, self)
end

return {
{ mode = { "n" }, key = props.press_key, handler = on_change },
local mappings = {
{ mode = { "n" }, key = props.press_key, handler = on_change },
}

if props.global_press_key then
table.insert(mappings, {
global = true,
mode = { "n", "i", "v" },
key = props.global_press_key,
handler = on_change,
})
end

return mappings
end

function Checkbox:initial_value()
Expand Down

0 comments on commit 9913345

Please sign in to comment.