add radio_group component

This commit is contained in:
Insality 2019-09-26 08:46:12 +03:00
parent ae9c580c7a
commit 4a2458ca3b
3 changed files with 56 additions and 3 deletions

View 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

View File

@ -25,6 +25,7 @@ M.comps = {
slider = require("druid.base.slider"), slider = require("druid.base.slider"),
checkbox = require("druid.base.checkbox"), checkbox = require("druid.base.checkbox"),
checkbox_group = require("druid.base.checkbox_group"), checkbox_group = require("druid.base.checkbox_group"),
radio_group = require("druid.base.radio_group"),
progress_rich = require("druid.rich.progress_rich"), progress_rich = require("druid.rich.progress_rich"),
} }

View File

@ -72,9 +72,9 @@ end
local function setup_checkbox(self) local function setup_checkbox(self)
local group1 = self.druid:new_checkbox_group( local radio_group = self.druid:new_radio_group(
{"radio1/check", "radio2/check", "radio3/check"}, {"radio1/check", "radio2/check", "radio3/check"},
nil, true, nil,
{"radio1/back", "radio2/back", "radio3/back"}) {"radio1/back", "radio2/back", "radio3/back"})
local checkbox_group = self.druid:new_checkbox_group( local checkbox_group = self.druid:new_checkbox_group(
@ -82,7 +82,7 @@ local function setup_checkbox(self)
nil, nil,
{"checkbox1/back", "checkbox2/back", "checkbox3/back"}) {"checkbox1/back", "checkbox2/back", "checkbox3/back"})
group1:set_state({false, false, true}) radio_group:set_state(2)
checkbox_group:set_state({true, false, true}) checkbox_group:set_state({true, false, true})
end end