mirror of
https://github.com/Insality/druid
synced 2025-06-27 10:27:48 +02:00
Merge pull request #26 from AGulev/feature/D20-radio-component
#20 radio_group component (based on checkbox)
This commit is contained in:
commit
69a18f2f06
52
druid/base/radio_group.lua
Normal file
52
druid/base/radio_group.lua
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
--- Radio group module
|
||||||
|
-- @module base.checkbox_group
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
|
||||||
|
local function on_checkbox_click(self, index)
|
||||||
|
for i = 1, #self.checkboxes do
|
||||||
|
self.checkboxes[i]:set_state(i == index, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
if self.callback then
|
||||||
|
self.callback(self.parent.parent, index)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M.set_state(self, index)
|
||||||
|
on_checkbox_click(self, index)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M.get_state(self)
|
||||||
|
local result = -1
|
||||||
|
|
||||||
|
for i = 1, #self.checkboxes do
|
||||||
|
if self.checkboxes[i]:get_state() then
|
||||||
|
result = i
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M.init(self, nodes, callback, click_nodes)
|
||||||
|
self.checkboxes = {}
|
||||||
|
self.callback = callback
|
||||||
|
|
||||||
|
for i = 1, #nodes do
|
||||||
|
local click_node = click_nodes and click_nodes[i] or nil
|
||||||
|
local checkbox = self.parent:new_checkbox(nodes[i], function()
|
||||||
|
on_checkbox_click(self, i)
|
||||||
|
end, click_node)
|
||||||
|
|
||||||
|
table.insert(self.checkboxes, checkbox)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
return M
|
@ -24,6 +24,7 @@ M.comps = {
|
|||||||
grid = require("druid.base.grid"),
|
grid = require("druid.base.grid"),
|
||||||
scroll = require("druid.base.scroll"),
|
scroll = require("druid.base.scroll"),
|
||||||
checkbox = require("druid.base.checkbox"),
|
checkbox = require("druid.base.checkbox"),
|
||||||
|
radio_group = require("druid.base.radio_group"),
|
||||||
checkbox_group = require("druid.base.checkbox_group"),
|
checkbox_group = require("druid.base.checkbox_group"),
|
||||||
slider = require("druid.base.slider"),
|
slider = require("druid.base.slider"),
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user