mirror of
https://github.com/airstruck/luigi.git
synced 2025-12-19 02:16:43 +00:00
add check and radio controls
This commit is contained in:
18
luigi/widget/check.lua
Normal file
18
luigi/widget/check.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
--[[--
|
||||
A check box.
|
||||
|
||||
@widget check
|
||||
--]]--
|
||||
|
||||
return function (self)
|
||||
self:onPress(function ()
|
||||
self.value = not self.value
|
||||
end)
|
||||
|
||||
self:onChange(function ()
|
||||
local subtype = self.value and 'check.checked' or 'check.unchecked'
|
||||
self.type = { 'check', subtype }
|
||||
end)
|
||||
|
||||
self.value = not not self.value
|
||||
end
|
||||
33
luigi/widget/radio.lua
Normal file
33
luigi/widget/radio.lua
Normal file
@@ -0,0 +1,33 @@
|
||||
--[[--
|
||||
A radio button.
|
||||
|
||||
@widget radio
|
||||
--]]--
|
||||
|
||||
-- TODO: make `group` a first-class attribute
|
||||
local groups = {}
|
||||
|
||||
return function (self)
|
||||
local groupName = self.group or 'default'
|
||||
|
||||
if not groups[groupName] then
|
||||
groups[groupName] = {}
|
||||
end
|
||||
|
||||
local group = groups[groupName]
|
||||
|
||||
group[#group + 1] = self
|
||||
|
||||
self:onPress(function ()
|
||||
for _, widget in ipairs(group) do
|
||||
widget.value = widget == self
|
||||
end
|
||||
end)
|
||||
|
||||
self:onChange(function ()
|
||||
local subtype = self.value and 'radio.checked' or 'radio.unchecked'
|
||||
self.type = { 'radio', subtype }
|
||||
end)
|
||||
|
||||
self.value = not not self.value
|
||||
end
|
||||
Reference in New Issue
Block a user