Skip to content

Commit

Permalink
feat(widget): defaultProps
Browse files Browse the repository at this point in the history
  • Loading branch information
Rathoz committed Oct 15, 2024
1 parent 774b823 commit 92e88a6
Show file tree
Hide file tree
Showing 20 changed files with 70 additions and 24 deletions.
7 changes: 6 additions & 1 deletion components/widget/basic/widget_basic_button.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ local Div = HtmlWidgets.Div
---@operator call(ButtonWidgetParameters): ButtonWidget

local Button = Class.new(Widget)
Button.defaultProps = {
linktype = 'internal',
variant = 'primary',
size = 'md',
}

---@return Widget
function Button:render()
Expand All @@ -33,7 +38,7 @@ function Button:render()
'btn',
'btn-new',
}
if self.props.variant == 'primary' or self.props.variant == nil then
if self.props.variant == 'primary' then
table.insert(cssClasses, 'btn-primary')
elseif self.props.variant == 'secondary' then
table.insert(cssClasses, 'btn-secondary')
Expand Down
8 changes: 6 additions & 2 deletions components/widget/basic/widget_basic_data_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ local Table = HtmlWidgets.Table

---@class WidgetDataTable: Widget
local DataTable = Class.new(Widget)
DataTable.defaultProps = {
classes = {},
wrapperClasses = {},
}

---@return Widget
function DataTable:render()
return Div{
children = {
Table{
children = self.props.children,
classes = WidgetUtil.collect('wikitable', unpack(self.props.classes or {})),
classes = WidgetUtil.collect('wikitable', unpack(self.props.classes)),
},
},
classes = WidgetUtil.collect('table-responsive', unpack(self.props.wrapperClasses or {})),
classes = WidgetUtil.collect('table-responsive', unpack(self.props.wrapperClasses)),
}
end

Expand Down
3 changes: 3 additions & 0 deletions components/widget/basic/widget_basic_link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ local WidgetUtil = Lua.import('Module:Widget/Util')
---@class LinkWidget: Widget
---@operator call(LinkWidgetParameters): LinkWidget
local Link = Class.new(Widget)
Link.defaultProps = {
linktype = 'internal',
}

---@return Widget
function Link:render()
Expand Down
18 changes: 13 additions & 5 deletions components/widget/html/widget_html_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ local Widget = Lua.import('Module:Widget')
---@class WidgetHtmlBase: Widget
---@operator call(table): WidgetHtmlBase
local HtmlBase = Class.new(Widget)
HtmlBase.defaultProps = {
classes = {},
css = {},
attributes = {},
}

---@return Html
function HtmlBase:render()
Expand All @@ -25,17 +30,20 @@ end

---@param tag string?
---@param children (Widget|Html|string|number)[]
---@param attributesInput {class: table?, style: table?, [string]: string}?
---@param attributesInput {style: table, class: table, [string]: string}
---@return Html
function HtmlBase:renderAs(tag, children, attributesInput)
local htmlNode = mw.html.create(tag)

local attributes = Table.copy(attributesInput or {})
local class = Table.extract(attributes, 'class') or {} --[[@as table]]
local styles = Table.extract(attributes, 'style') or {} --[[@as table]]
---@cast attributes {[string]: string}
---@type table<string, string|table>
local attributes = Table.copy(attributesInput)
local class = Table.extract(attributes, 'class') --[[@as table]]
local styles = Table.extract(attributes, 'style') --[[@as table]]
---@cast attributes table<string, string>

htmlNode:addClass(String.nilIfEmpty(table.concat(class, ' ')))
htmlNode:css(styles)

htmlNode:css(styles)
htmlNode:attr(attributes)

Expand Down
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_div.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local Div = Class.new(WidgetHtml)

---@return Html
function Div:render()
local attributes = Table.copy(self.props.attributes or {})
local attributes = Table.copy(self.props.attributes)
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('div', self.props.children, attributes)
Expand Down
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_fragment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local Fragment = Class.new(WidgetHtml)

---@return Html
function Fragment:render()
return self:renderAs(nil, self.props.children)
return self:renderAs(nil, self.props.children, {})
end

return Fragment
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_li.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local Li = Class.new(WidgetHtml)

---@return Html
function Li:render()
local attributes = Table.copy(self.props.attributes or {})
local attributes = Table.copy(self.props.attributes)
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('li', self.props.children, attributes)
Expand Down
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_span.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local Span = Class.new(WidgetHtml)

---@return Html
function Span:render()
local attributes = Table.copy(self.props.attributes or {})
local attributes = Table.copy(self.props.attributes)
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('span', self.props.children, attributes)
Expand Down
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local HtmlTable = Class.new(WidgetHtml)

---@return Html
function HtmlTable:render()
local attributes = Table.copy(self.props.attributes or {})
local attributes = Table.copy(self.props.attributes)
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('table', self.props.children, attributes)
Expand Down
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_td.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local Td = Class.new(WidgetHtml)

---@return Html
function Td:render()
local attributes = Table.copy(self.props.attributes or {})
local attributes = Table.copy(self.props.attributes)
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('td', self.props.children, attributes)
Expand Down
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_th.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local Th = Class.new(WidgetHtml)

---@return Html
function Th:render()
local attributes = Table.copy(self.props.attributes or {})
local attributes = Table.copy(self.props.attributes)
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('th', self.props.children, attributes)
Expand Down
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_tr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local Tr = Class.new(WidgetHtml)

---@return Html
function Tr:render()
local attributes = Table.copy(self.props.attributes or {})
local attributes = Table.copy(self.props.attributes)
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('tr', self.props.children, attributes)
Expand Down
2 changes: 1 addition & 1 deletion components/widget/html/widget_html_ul.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local Ul = Class.new(WidgetHtml)

---@return Html
function Ul:render()
local attributes = Table.copy(self.props.attributes or {})
local attributes = Table.copy(self.props.attributes)
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('ul', self.props.children, attributes)
Expand Down
5 changes: 4 additions & 1 deletion components/widget/image/widget_image_icon_image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ local WidgetIcon = Lua.import('Module:Widget/Image/Icon')
---@operator call(IconImageWidgetParameters): IconImageWidget
---@field props IconImageWidgetParameters
local Icon = Class.new(WidgetIcon)
Icon.defaultProps = {
link = '',
}

---@return string?
function Icon:render()
return Image.display(
self.props.imageLight,
self.props.imageDark,
{
link = self.props.link or '',
link = self.props.link,
size = 'x20px',
}
)
Expand Down
6 changes: 5 additions & 1 deletion components/widget/infobox/widget_infobox_breakdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ local HtmlWidgets = Lua.import('Module:Widget/Html/All')
---@field classes string[]
---@field contentClasses table<integer, string[]> --can have gaps in the outer table
local Breakdown = Class.new(Widget)
Breakdown.defaultProps = {
classes = {},
contentClasses = {},
}

---@return Widget?
function Breakdown:render()
Expand All @@ -34,7 +38,7 @@ function Breakdown:render()
classes = WidgetUtil.collect(
'infobox-cell-' .. number,
self.props.classes,
(self.props.contentClasses or {})['content' .. childIndex]
self.props.contentClasses['content' .. childIndex]
),
}
end)
Expand Down
10 changes: 8 additions & 2 deletions components/widget/infobox/widget_infobox_cell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ local Cell = Class.new(Widget,
self.props.children = input.children or input.content or {}
end
)
Cell.defaultProps = {
options = {
columns = 2,
makeLink = false,
surpressColon = false,
}
}

---@return Widget?
function Cell:render()
if Logic.isEmpty(self.props.children) then
return
end

local options = self.props.options or {}
options.columns = options.columns or 2
local options = self.props.options

local mappedChildren = {}
for i, child in ipairs(self.props.children) do
Expand Down
5 changes: 4 additions & 1 deletion components/widget/infobox/widget_infobox_header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ local Div = HtmlWidgets.Div
---@class HeaderWidget: Widget
---@operator call(table): HeaderWidget
local Header = Class.new(Widget)
Header.defaultProps = {
name = mw.title.getCurrentTitle().text,
}

function Header:render()
if self.props.image then
Expand Down Expand Up @@ -46,7 +49,7 @@ function Header:_name()
classes = {'infobox-header', 'wiki-backgroundcolor-light'},
children = {
self:_createInfoboxButtons(),
self.props.name or mw.title.getCurrentTitle().text,
self.props.name,
}
}}}
end
Expand Down
7 changes: 6 additions & 1 deletion components/widget/misc/widget_misc_inline_icon_and_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ local Span = HtmlWidgets.Span
---@operator call(InlineIconAndTextWidgetParameters): InlineIconAndTextWidget

local InlineIconAndText = Class.new(Widget)
InlineIconAndText.defaultProps = {
options = {
flipped = false,
},
}

---@return Widget
function InlineIconAndText:render()
Expand All @@ -37,7 +42,7 @@ function InlineIconAndText:render()
' ',
self.props.icon,
}
if self.props.options and self.props.options.flipped then
if self.props.options.flipped then
children = Array.reverse(children)
end
return Span{
Expand Down
3 changes: 3 additions & 0 deletions components/widget/squad/widget_squad_core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ local DataTable, Tr, Th = Widgets.DataTable, Widgets.Tr, Widgets.Th
---@class SquadWidget: Widget
---@operator call(table): SquadWidget
local Squad = Class.new(Widget)
Squad.defaultProps = {
type = SquadUtils.SquadType.ACTIVE,
}

---@return WidgetDataTable
function Squad:render()
Expand Down
4 changes: 3 additions & 1 deletion components/widget/widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ local Table = require('Module:Table')
---@field props table<string, any>
---@field injector WidgetInjector?
local Widget = Class.new(function(self, props)
self.props = Table.copy(props) or {}
self.props = Table.deepMerge(self.defaultProps, props)
self.props.children = self.props.children or {}
self.context = {} -- Populated by the parent
end)

Widget.defaultProps = {}

---Asserts the existence of a value and copies it
---@param value string
---@return string
Expand Down

0 comments on commit 92e88a6

Please sign in to comment.