mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Add create(a, { props }) syntax (#42)
* Add new create syntax * Add new create syntax to changelog * Fix new create syntax test
This commit is contained in:
parent
8799988851
commit
44fe65ee5e
3 changed files with 70 additions and 40 deletions
|
|
@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `create("ClassName", { props })` and `create(Instance, { props })` syntax.
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
## [0.3.1] - 2024-10-09
|
## [0.3.1] - 2024-10-09
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
||||||
|
|
@ -40,44 +40,53 @@ local function clone_instance(instance: Instance)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create(class_or_instance: string|Instance): (Props) -> Instance
|
local function create(class_or_instance: string | Instance, props: Props?): ((Props) -> Instance) | Instance
|
||||||
|
local result: (Props) -> Instance
|
||||||
if type(class_or_instance) == "string" then
|
if type(class_or_instance) == "string" then
|
||||||
return create_instance(class_or_instance)
|
result = create_instance(class_or_instance)
|
||||||
elseif typeof(class_or_instance) == "Instance" then
|
elseif typeof(class_or_instance) == "Instance" then
|
||||||
return clone_instance(class_or_instance)
|
result = clone_instance(class_or_instance)
|
||||||
else
|
else
|
||||||
throw("bad argument #1, expected string or instance, got " .. typeof(class_or_instance))
|
throw("bad argument #1, expected string or instance, got " .. typeof(class_or_instance))
|
||||||
return nil :: never
|
return nil :: never
|
||||||
end
|
end
|
||||||
|
if props then
|
||||||
|
return result(props)
|
||||||
|
end
|
||||||
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
type Props = { [any]: any }
|
type Props = { [any]: any }
|
||||||
|
|
||||||
|
type Create<Name, Instance> = ((Name, Props) -> Instance) & ((Name) -> (Props) -> Instance)
|
||||||
|
|
||||||
return (create :: any) ::
|
return (create :: any) ::
|
||||||
( <T>(T & Instance) -> (Props) -> T ) &
|
& ( <T>(T & Instance) -> (Props) -> T )
|
||||||
( ("Folder") -> (Props) -> Folder ) &
|
& ( <T>(T & Instance, Props) -> T )
|
||||||
( ("BillboardGui") -> (Props) -> BillboardGui ) &
|
& Create<"Folder", Folder>
|
||||||
( ("CanvasGroup") -> (Props) -> CanvasGroup ) &
|
& Create<"BillboardGui", BillboardGui>
|
||||||
( ("Frame") -> (Props) -> Frame ) &
|
& Create<"CanvasGroup", CanvasGroup>
|
||||||
( ("ImageButton") -> (Props) -> ImageButton ) &
|
& Create<"Frame", Frame>
|
||||||
( ("ImageLabel") -> (Props) -> ImageLabel ) &
|
& Create<"ImageButton", ImageButton>
|
||||||
( ("ScreenGui") -> (Props) -> ScreenGui ) &
|
& Create<"ImageLabel", ImageLabel>
|
||||||
( ("ScrollingFrame") -> (Props) -> ScrollingFrame ) &
|
& Create<"ScreenGui", ScreenGui>
|
||||||
( ("SurfaceGui") -> (Props) -> SurfaceGui ) &
|
& Create<"ScrollingFrame", ScrollingFrame>
|
||||||
( ("TextBox") -> (Props) -> TextBox ) &
|
& Create<"SurfaceGui", SurfaceGui>
|
||||||
( ("TextButton") -> (Props) -> TextButton ) &
|
& Create<"TextBox", TextBox>
|
||||||
( ("TextLabel") -> (Props) -> TextLabel ) &
|
& Create<"TextButton", TextButton>
|
||||||
( ("UIAspectRatioConstraint") -> (Props) -> UIAspectRatioConstraint ) &
|
& Create<"TextLabel", TextLabel>
|
||||||
( ("UICorner") -> (Props) -> UICorner ) &
|
& Create<"UIAspectRatioConstraint", UIAspectRatioConstraint>
|
||||||
( ("UIGradient") -> (Props) -> UIGradient ) &
|
& Create<"UICorner", UICorner>
|
||||||
( ("UIGridLayout") -> (Props) -> UIGridLayout ) &
|
& Create<"UIGradient", UIGradient>
|
||||||
( ("UIListLayout") -> (Props) -> UIListLayout ) &
|
& Create<"UIGridLayout", UIGridLayout>
|
||||||
( ("UIPadding") -> (Props) -> UIPadding ) &
|
& Create<"UIListLayout", UIListLayout>
|
||||||
( ("UIPageLayout") -> (Props) -> UIPageLayout ) &
|
& Create<"UIPadding", UIPadding>
|
||||||
( ("UIScale") -> (Props) -> UIScale ) &
|
& Create<"UIPageLayout", UIPageLayout>
|
||||||
( ("UISizeConstraint") -> (Props) -> UISizeConstraint ) &
|
& Create<"UIScale", UIScale>
|
||||||
( ("UIStroke") -> (Props) -> UIStroke ) &
|
& Create<"UISizeConstraint", UISizeConstraint>
|
||||||
( ("UITableLayout") -> (Props) -> UITableLayout ) &
|
& Create<"UIStroke", UIStroke>
|
||||||
( ("UITextSizeConstraint") -> (Props) -> UITextSizeConstraint ) &
|
& Create<"UITableLayout", UITableLayout>
|
||||||
( ("VideoFrame") -> (Props) -> VideoFrame ) &
|
& Create<"UITextSizeConstraint", UITextSizeConstraint>
|
||||||
( ("ViewportFrame") -> (Props) -> ViewportFrame ) &
|
& Create<"VideoFrame", VideoFrame>
|
||||||
( (string) -> (Props) -> Instance )
|
& Create<"ViewportFrame", ViewportFrame>
|
||||||
|
& Create<string, Instance>
|
||||||
|
|
|
||||||
|
|
@ -721,6 +721,19 @@ TEST("create()", wrap_root(function()
|
||||||
local source = vide.source
|
local source = vide.source
|
||||||
local cleanup = vide.cleanup
|
local cleanup = vide.cleanup
|
||||||
|
|
||||||
|
do CASE "create(\"ClassName\", props) syntax"
|
||||||
|
local frame = create("Frame", { BackgroundTransparency = 0.5, Name = "Foo" })
|
||||||
|
CHECK(frame.BackgroundTransparency == 0.5)
|
||||||
|
CHECK(frame.Name == "Foo")
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "create(Instance, props) syntax"
|
||||||
|
local frame0 = create("Frame", { BackgroundTransparency = 0.5, Name = "Foo" })
|
||||||
|
local frame = create(frame0, { BackgroundTransparency = 1 })
|
||||||
|
CHECK(frame.BackgroundTransparency == 1)
|
||||||
|
CHECK(frame.Name == "Foo")
|
||||||
|
end
|
||||||
|
|
||||||
do CASE "apply default properties"
|
do CASE "apply default properties"
|
||||||
local defaults = require "../src/defaults"
|
local defaults = require "../src/defaults"
|
||||||
local frame = create "Frame" {} :: Instance & { BorderSizePixel: any, BorderColor3: any }
|
local frame = create "Frame" {} :: Instance & { BorderSizePixel: any, BorderColor3: any }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue