Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(widget): defaultProps #4874

Merged
merged 8 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 10 additions & 13 deletions components/widget/html/widget_html_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,33 @@ local Array = require('Module:Array')
local Class = require('Module:Class')
local Lua = require('Module:Lua')
local String = require('Module:StringUtils')
local Table = require('Module:Table')

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()
error('HtmlBase:render() must be overridden')
end

---@param tag string?
---@param children (Widget|Html|string|number)[]
---@param attributesInput {class: table?, style: table?, [string]: string}?
---@return Html
function HtmlBase:renderAs(tag, children, attributesInput)
function HtmlBase:renderAs(tag)
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}
htmlNode:addClass(String.nilIfEmpty(table.concat(self.props.classes, ' ')))
htmlNode:css(self.props.css)
htmlNode:attr(self.props.attributes)

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

Array.forEach(children, function(child)
Array.forEach(self.props.children, function(child)
if Class.instanceOf(child, Widget) then
---@cast child Widget
child.context = self:_nextContext()
Expand Down
6 changes: 1 addition & 5 deletions components/widget/html/widget_html_div.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

local Class = require('Module:Class')
local Lua = require('Module:Lua')
local Table = require('Module:Table')

local WidgetHtml = Lua.import('Module:Widget/Html/Base')

Expand All @@ -18,10 +17,7 @@ local Div = Class.new(WidgetHtml)

---@return Html
function Div:render()
local attributes = Table.copy(self.props.attributes or {})
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('div', self.props.children, attributes)
return self:renderAs('div')
end

return Div
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)
end

return Fragment
6 changes: 1 addition & 5 deletions components/widget/html/widget_html_li.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

local Class = require('Module:Class')
local Lua = require('Module:Lua')
local Table = require('Module:Table')

local WidgetHtml = Lua.import('Module:Widget/Html/Base')

Expand All @@ -18,10 +17,7 @@ local Li = Class.new(WidgetHtml)

---@return Html
function Li:render()
local attributes = Table.copy(self.props.attributes or {})
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('li', self.props.children, attributes)
return self:renderAs('li')
end

return Li
6 changes: 1 addition & 5 deletions components/widget/html/widget_html_span.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

local Class = require('Module:Class')
local Lua = require('Module:Lua')
local Table = require('Module:Table')

local WidgetHtml = Lua.import('Module:Widget/Html/Base')

Expand All @@ -18,10 +17,7 @@ local Span = Class.new(WidgetHtml)

---@return Html
function Span:render()
local attributes = Table.copy(self.props.attributes or {})
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('span', self.props.children, attributes)
return self:renderAs('span')
end

return Span
12 changes: 4 additions & 8 deletions components/widget/html/widget_html_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@

local Class = require('Module:Class')
local Lua = require('Module:Lua')
local Table = require('Module:Table')

local WidgetHtml = Lua.import('Module:Widget/Html/Base')

---@class WidgetTable: WidgetHtmlBase
---@operator call(table): WidgetTable
local HtmlTable = Class.new(WidgetHtml)
local Table = Class.new(WidgetHtml)

---@return Html
function HtmlTable:render()
local attributes = Table.copy(self.props.attributes or {})
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('table', self.props.children, attributes)
function Table:render()
return self:renderAs('table')
end

return HtmlTable
return Table
6 changes: 1 addition & 5 deletions components/widget/html/widget_html_td.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

local Class = require('Module:Class')
local Lua = require('Module:Lua')
local Table = require('Module:Table')

local WidgetHtml = Lua.import('Module:Widget/Html/Base')

Expand All @@ -18,10 +17,7 @@ local Td = Class.new(WidgetHtml)

---@return Html
function Td:render()
local attributes = Table.copy(self.props.attributes or {})
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('td', self.props.children, attributes)
return self:renderAs('td')
end

return Td
6 changes: 1 addition & 5 deletions components/widget/html/widget_html_th.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

local Class = require('Module:Class')
local Lua = require('Module:Lua')
local Table = require('Module:Table')

local WidgetHtml = Lua.import('Module:Widget/Html/Base')

Expand All @@ -18,10 +17,7 @@ local Th = Class.new(WidgetHtml)

---@return Html
function Th:render()
local attributes = Table.copy(self.props.attributes or {})
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('th', self.props.children, attributes)
return self:renderAs('th')
end

return Th
6 changes: 1 addition & 5 deletions components/widget/html/widget_html_tr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

local Class = require('Module:Class')
local Lua = require('Module:Lua')
local Table = require('Module:Table')

local WidgetHtml = Lua.import('Module:Widget/Html/Base')

Expand All @@ -18,10 +17,7 @@ local Tr = Class.new(WidgetHtml)

---@return Html
function Tr:render()
local attributes = Table.copy(self.props.attributes or {})
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('tr', self.props.children, attributes)
return self:renderAs('tr')
end

return Tr
6 changes: 1 addition & 5 deletions components/widget/html/widget_html_ul.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

local Class = require('Module:Class')
local Lua = require('Module:Lua')
local Table = require('Module:Table')

local WidgetHtml = Lua.import('Module:Widget/Html/Base')

Expand All @@ -18,10 +17,7 @@ local Ul = Class.new(WidgetHtml)

---@return Html
function Ul:render()
local attributes = Table.copy(self.props.attributes or {})
attributes.class = self.props.classes
attributes.style = self.props.css
return self:renderAs('ul', self.props.children, attributes)
return self:renderAs('ul')
end

return Ul
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
3 changes: 3 additions & 0 deletions 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,9 @@ local Span = HtmlWidgets.Span
---@operator call(InlineIconAndTextWidgetParameters): InlineIconAndTextWidget

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

---@return Widget
function InlineIconAndText:render()
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
5 changes: 3 additions & 2 deletions components/widget/widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ local Table = require('Module:Table')
---@operator call(table): self
---@field context Widget[]
---@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(Table.deepCopy(self.defaultProps), props)

if not Array.isArray(self.props.children) then
self.props.children = {self.props.children}
Expand All @@ -29,6 +28,8 @@ local Widget = Class.new(function(self, props)
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
Loading
Loading