import{_ as i,a,b as t,ag as h}from"./chunks/framework.BcrMLAmg.js";const g=JSON.parse('{"title":"State Management in Basalt","description":"","frontmatter":{},"headers":[],"relativePath":"guides/states.md","filePath":"guides/states.md","lastUpdated":1744151621000}'),n={name:"guides/states.md"};function l(k,s,e,p,E,d){return t(),a("div",null,s[0]||(s[0]=[h(`

State Management in Basalt

States provide a new way to manage data and UI synchronization in your Basalt applications.

Detailed State Methods

initializeState

lua
BaseFrame:initializeState("name", defaultValue, persistent, path?)

Creates a new state in a BaseFrame:

setState

lua
element:setState("name", newValue)

Updates an existing state's value:

getState

lua
local value = element:getState("name")

Retrieves the current value of a state:

computed

lua
element:computed("name", function(self)
    local otherState = self:getState("otherState")
    return someCalculation(otherState)
end)

Creates a computed state that depends on other states:

onStateChange

lua
element:onStateChange("name", function(self, newValue)
    -- React to changes
    self:someAction(newValue)
end)

Registers a listener for state changes:

bind

lua
local name = form:addInput():bind("text", "name")

Binds a property to a state

Example: Form Validation

Here's a comprehensive example showing state management in a form:

lua
local main = basalt.getMainFrame()
    -- Initialize form states
    :initializeState("username", "", true) -- make them persistent
    :initializeState("password", "", true) -- make them persistent
    :initializeState("confirmPassword", "", true) -- make them persistent

local form = main:addFrame()
    :setSize("{parent.width - 4}", "{parent.height - 4}")
    :setPosition(3, 3)

-- Add computed validation state
form:computed("isValid", function(self)
    local username = self:getState("username")
    local password = self:getState("password")
    local confirmPass = self:getState("confirmPassword")
    return #username >= 3 and #password >= 6 and password == confirmPass
end)

-- Create labels
form:addLabel({text="Username:", x = 2, y = 2, foreground = colors.lightGray})
form:addLabel({text="Password:", x = 2, y = 4, foreground = colors.lightGray})
form:addLabel({text="Confirm:", x = 2, y = 6, foreground = colors.lightGray})

local userInput = form:addInput({x = 11, y = 2, width = 20, height = 1}):bind("text", "username")
local passwordInput = form:addInput({x = 11, y = 4, width = 20, height = 1}):bind("text", "password")
local confirmInput = form:addInput({x = 11, y = 6, width = 20, height = 1}):bind("text", "confirmPassword")

-- Submit button
local submitBtn = form:addButton()
    :setText("Submit")
    :setPosition(2, 8)
    :setSize(29, 1)

-- Status label
local statusLabel = form:addLabel()
    :setPosition(2, 10)
    :setSize(29, 1)


form:onStateChange("isValid", function(self, isValid)
    if isValid then
        statusLabel:setText("Form is valid!")
            :setForeground(colors.green)
        submitBtn:setBackground(colors.green)
    else
        statusLabel:setText("Please fill all fields correctly")
            :setForeground(colors.red)
        submitBtn:setBackground(colors.red)
    end
end)

Tips

  1. State Initialization

  2. Computed States

  3. State Updates

  4. Common Patterns

`,32)]))}const y=i(n,[["render",l]]);export{g as __pageData,y as default};