local D=game:GetService("\084\101\120\116\083\101\114\118\105\099\101")local o=script:FindFirstAncestor("\084\101\120\116\069\100\105\116\111\114")local m=require(o.Packages.Roact)local c=require(script.Parent.StudioThemeContext)local f=m.Component:extend("\084\101\120\116\066\111\120")f.defaultProps={BackgroundTransparency=1;ClipsDescendants=false;Font=Enum.Font.SourceSans;TextSize=20;Text="",TextWrapped=false;TextXAlignment=Enum.TextXAlignment.Left;TextYAlignment=Enum.TextYAlignment.Center,ClearTextOnFocus=true,MultiLine=false;RichText=false,themeType=Enum.StudioStyleGuideColor.InputFieldBackground,borderThemeType=Enum.StudioStyleGuideColor.InputFieldBorder;textThemeType=Enum.StudioStyleGuideColor.MainText;themeModifier=Enum.StudioStyleGuideModifier.Default;verticalSpacing=10}function f.init(o)o.autoUpdate=false o.size,o.updateSize=m.createBinding(UDim2.new())local c=o.props.Size if not c then o.autoUpdate=true if o.props.TextWrapped then c=UDim2.fromScale(1,0)else local m=D:GetTextSize(typeof(o.props.Text)=="\115\116\114\105\110\103"and o.props.Text or o.props.Text:getValue(),o.props.TextSize,o.props.Font,Vector2.new(math.huge,math.huge))c=UDim2.new(o.props.Width or UDim.new(0,m.X),UDim.new(0,m.Y+o.props.verticalSpacing*2))end end o.size,o.updateSize=m.createBinding(c)end function f.render(o)return m.createElement(c.Consumer,{render=function(c)return m.createElement("\084\101\120\116\066\111\120",{AnchorPoint=o.props.AnchorPoint;LayoutOrder=o.props.LayoutOrder,Position=o.props.Position,Size=o.size,BackgroundTransparency=o.props.BackgroundTransparency,BackgroundColor3=c:GetColor(o.props.themeType,o.props.themeModifier),BorderColor3=c:GetColor(o.props.borderThemeType,o.props.themeModifier);ClipsDescendants=o.props.ClipsDescendants,Font=o.props.Font;TextSize=o.props.TextSize;TextColor3=c:GetColor(o.props.textThemeType,o.props.themeModifier);Text=o.props.Text;TextWrapped=o.props.TextWrapped;TextXAlignment=o.props.TextXAlignment;TextYAlignment=o.props.TextYAlignment,ClearTextOnFocus=o.props.ClearTextOnFocus;MultiLine=o.props.MultiLine;RichText=o.props.RichText,[m.Ref]=o.props[m.Ref];[m.Change.Text]=function(c,...)if o.autoUpdate then local m=o.props.TextWrapped and c.AbsoluteSize.X-2 or math.huge local f=D:GetTextSize(c.Text,o.props.TextSize,o.props.Font,Vector2.new(m,math.huge))o.updateSize(UDim2.new(o.props.TextWrapped and UDim.new(1,0)or o.props.Width or UDim.new(0,f.X),UDim.new(0,f.Y+o.props.verticalSpacing*2)))end if o.props[m.Change.Text]then o.props[m.Change.Text](c,...)end end,[m.Change.SelectionStart]=o.props[m.Change.SelectionStart];[m.Change.CursorPosition]=o.props[m.Change.CursorPosition],[m.Event.Focused]=o.props[m.Event.Focused];[m.Event.FocusLost]=o.props[m.Event.FocusLost]},o.props[m.Children])end})end return f
-- Based off https://github.com/tiffany352/Roblox-Tag-Editor/blob/master/src/Components/TextLabel.lua
local TextService = game:GetService("TextService")
local TextEditor = script:FindFirstAncestor("TextEditor")
local Roact = require(TextEditor.Packages.Roact)

local StudioThemeContext = require(script.Parent.StudioThemeContext)
local TextBox = Roact.Component:extend("TextBox")

TextBox.defaultProps = {
  BackgroundTransparency = 1,
  ClipsDescendants = false,

  Font = Enum.Font.SourceSans,
  TextSize = 20,
  Text = "",
  TextWrapped = false,
  TextXAlignment = Enum.TextXAlignment.Left,
  TextYAlignment = Enum.TextYAlignment.Center,
  ClearTextOnFocus = true,
  MultiLine = false,
  RichText = false,

  themeType = Enum.StudioStyleGuideColor.InputFieldBackground,
  borderThemeType = Enum.StudioStyleGuideColor.InputFieldBorder,
  textThemeType = Enum.StudioStyleGuideColor.MainText,
  themeModifier = Enum.StudioStyleGuideModifier.Default,
  verticalSpacing = 10, -- Extra Y offset to add on if auto updating
}

function TextBox:init()
  self.autoUpdate = false
  self.size, self.updateSize = Roact.createBinding(UDim2.new())

  local size = self.props.Size
  if not size then
    self.autoUpdate = true
    if self.props.TextWrapped then
      size = UDim2.fromScale(1, 0)
    else
      local TextBounds = TextService:GetTextSize(typeof(self.props.Text) == "string" and self.props.Text or self.props.Text:getValue(), self.props.TextSize, self.props.Font, Vector2.new(math.huge, math.huge))
      size = UDim2.new(self.props.Width or UDim.new(0, TextBounds.X), UDim.new(0, TextBounds.Y + self.props.verticalSpacing * 2))
    end
  end
  self.size, self.updateSize = Roact.createBinding(size)
end

function TextBox:render()
  return Roact.createElement(StudioThemeContext.Consumer, {
    render = function(theme)
      return Roact.createElement("TextBox", {
        AnchorPoint = self.props.AnchorPoint,
        LayoutOrder = self.props.LayoutOrder,
        Position = self.props.Position,
        Size = self.size,
        BackgroundTransparency = self.props.BackgroundTransparency,
        BackgroundColor3 = theme:GetColor(self.props.themeType, self.props.themeModifier),
        BorderColor3 = theme:GetColor(self.props.borderThemeType, self.props.themeModifier),
        ClipsDescendants = self.props.ClipsDescendants,

        Font = self.props.Font,
        TextSize = self.props.TextSize,
        TextColor3 = theme:GetColor(self.props.textThemeType, self.props.themeModifier),
        Text = self.props.Text,
        TextWrapped = self.props.TextWrapped,
        TextXAlignment = self.props.TextXAlignment,
        TextYAlignment = self.props.TextYAlignment,
        ClearTextOnFocus = self.props.ClearTextOnFocus,
        MultiLine = self.props.MultiLine,
        RichText = self.props.RichText,

        [Roact.Ref] = self.props[Roact.Ref],
        [Roact.Change.Text] = function(rbx, ...)
          if self.autoUpdate then
            local MaxWidth = self.props.TextWrapped and rbx.AbsoluteSize.X - 2 or math.huge
            local TextBounds = TextService:GetTextSize(rbx.Text, self.props.TextSize, self.props.Font, Vector2.new(MaxWidth, math.huge))
            self.updateSize(UDim2.new(self.props.TextWrapped and UDim.new(1, 0) or self.props.Width or UDim.new(0, TextBounds.X), UDim.new(0, TextBounds.Y + self.props.verticalSpacing * 2)))
          end

          if self.props[Roact.Change.Text] then
            self.props[Roact.Change.Text](rbx, ...)
          end
        end,
        [Roact.Change.SelectionStart] = self.props[Roact.Change.SelectionStart],
        [Roact.Change.CursorPosition] = self.props[Roact.Change.CursorPosition],
        [Roact.Event.Focused] = self.props[Roact.Event.Focused],
        [Roact.Event.FocusLost] = self.props[Roact.Event.FocusLost],
      }, self.props[Roact.Children])
    end
  })
end


return TextBox