Yagyaveer

Lua Code Example

Sep 24th, 2020
1,794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. local Framework = script.Parent.Parent
  2. local Roact = require(Framework.Parent.Roact)
  3. local ContextServices = require(Framework.ContextServices)
  4.  
  5. local Util = require(Framework.Util)
  6. local Typecheck = Util.Typecheck
  7. local StyleModifier = Util.StyleModifier
  8. local FlagsList = Util.Flags.new({
  9.     FFlagRefactorDevFrameworkTheme = {"FW"},
  10. })
  11.  
  12. local UI = require(Framework.UI)
  13. local Button = UI.Button
  14. local Container = UI.Container
  15. local Decoration = UI.Decoration
  16. local Separator = UI.Separator
  17. local TextInput = UI.TextInput
  18.  
  19. local MainBar = Roact.PureComponent:extend("MainBar")
  20. Typecheck.wrap(MainBar, script)
  21.  
  22. function MainBar:init()
  23.     self.state = {
  24.         text = "",
  25.         lastSearchTermAsProp = "",
  26.         isFocused = false,
  27.         isHovered = false,
  28.         shouldFocus = false
  29.     }
  30.  
  31.     self.mouseEnter = function()
  32.         self:setStateAndStyleModifier({
  33.             isHovered = true
  34.         })
  35.     end
  36.  
  37.     self.mouseLeave = function()
  38.         self:setStateAndStyleModifier({
  39.             isHovered = false
  40.         })
  41.     end
  42.  
  43.     self.requestSearch = function()
  44.         if self.props.OnSearchRequested then
  45.             self.props.OnSearchRequested(self.state.text)
  46.         end
  47.     end
  48.  
  49.     self.onBackgroundInputBegan = function(rbx, input)
  50.         if input.UserInputType == Enum.UserInputType.MouseButton1 then
  51.             self:setState({
  52.                 shouldFocus = true
  53.             })
  54.         end
  55.     end
  56.  
  57.     self.onBackgroundFocusLost = function(rbx, input)
  58.         if input.UserInputType == Enum.UserInputType.Focus then
  59.             self:onFocusLost(false)
  60.         end
  61.     end
  62.  
  63.     self.onTextChanged = function(text)
  64.         self:setState(function(state)
  65.             if state.text == text then
  66.                 return
  67.             end
  68.  
  69.             return {
  70.                 text = text
  71.             }
  72.         end)
  73.     end
  74.  
  75.     self.onTextBoxFocusGained = function(rbx)
  76.         self:setStateAndStyleModifier({
  77.             isFocused = true,
  78.         })
  79.     end
  80.  
  81.     self.onTextBoxFocusLost = function(enterPressed)
  82.         self:onFocusLost(enterPressed)
  83.     end
  84.  
  85.     self.onClearButtonClicked = function()
  86.         self:setStateAndStyleModifier({
  87.             text = "",
  88.             isFocused = true,
  89.         })
  90.         self:setState({
  91.             shouldFocus = true
  92.         })
  93.  
  94.         self.requestSearch()
  95.     end
  96. end
  97.  
  98. function MainBar:didUpdate(prevProps, prevState)
  99.     if self.props.OnTextChanged and prevState.text ~= self.state.text then
  100.         self.props.OnTextChanged(self.state.text)
  101.     end
  102. end
  103.  
  104. function MainBar:setStateAndStyleModifier(newState)
  105.     self:setState(function(state)
  106.         local isNowFocused = newState.isFocused
  107.         if isNowFocused == nil then
  108.             isNowFocused = state.isFocused
  109.         end
  110.  
  111.         local isNowHovered = newState.isHovered
  112.         if isNowHovered == nil then
  113.             isNowHovered = state.isHovered
  114.         end
  115.  
  116.         if isNowFocused or isNowHovered then
  117.             newState.StyleModifier = StyleModifier.Hover
  118.         else
  119.             newState.StyleModifier = Roact.None
  120.         end
  121.  
  122.         return newState
  123.     end)
  124. end
  125.  
  126. function MainBar.getDerivedStateFromProps(nextProps, lastState)
  127.     local searchTerm = nextProps.SearchTerm
  128.     local lastSearchTermAsProp = lastState.lastSearchTermAsProp or ""
  129.  
  130.     if (searchTerm ~= lastSearchTermAsProp) then
  131.         return {
  132.             text = searchTerm,
  133.             lastSearchTermAsProp = searchTerm
  134.         }
  135.     end
  136.  
  137.     return {}
  138. end
  139.  
  140. function MainBar:onFocusLost(enterPressed)
  141.     self:setStateAndStyleModifier({
  142.         isFocused = false,
  143.         shouldFocus = false
  144.     })
  145.  
  146.     if enterPressed then
  147.         self.requestSearch()
  148.     end
  149. end
  150.  
  151. function MainBar:render()
  152.     local props = self.props
  153.     local state = self.state
  154.  
  155.     local containerWidth = props.Width or 200
  156.     local buttonWidth = props.ButtonWidth or 24
  157.     local layoutOrder = props.LayoutOrder or 0
  158.     local placeholderText = props.PlaceholderText or "Search"
  159.  
  160.     local shouldFocus = state.shouldFocus
  161.     local text = state.text
  162.     local showClearButton = #text > 0
  163.     local showSearchButton = self.props.ShowSearchButton == nil
  164.     if showSearchButton == nil then
  165.         showSearchButton = true
  166.     end
  167.  
  168.     local theme = props.Theme
  169.     local style
  170.     if FlagsList:get("FFlagRefactorDevFrameworkTheme") then
  171.         style = props.Stylizer
  172.     else
  173.         style = theme:getStyle("Framework", self)
  174.     end
  175.     local backgroundStyle = style.BackgroundStyle
  176.  
  177.     local padding = style.Padding
  178.  
  179.     local leftPadding = type(padding) == "table" and padding.Left or padding
  180.     local topPadding = type(padding) == "table" and padding.Top or padding
  181.     local separatorWidth = 1
  182.     local buttonsWidth = (showClearButton and buttonWidth or 0) + (showSearchButton and buttonWidth or 0) + separatorWidth
  183.     local buttonSize = UDim2.new(0, buttonWidth, 1, 0)
  184.  
  185.     return Roact.createElement(Container, {
  186.         Size = UDim2.new(0, containerWidth, 1, 0),
  187.         Background = Decoration.RoundBox,
  188.         BackgroundStyle = backgroundStyle,
  189.     }, {
  190.         SearchContainer = Roact.createElement("Frame", {
  191.             ClipsDescendants = true,
  192.             BackgroundTransparency = 1,
  193.             LayoutOrder = layoutOrder,
  194.             Size = UDim2.new(1, 0, 1, 0),
  195.             [Roact.Event.MouseEnter] = self.mouseEnter,
  196.             [Roact.Event.MouseLeave] = self.mouseLeave,
  197.             [Roact.Event.InputBegan] = self.onBackgroundInputBegan,
  198.             [Roact.Event.InputEnded] = self.onBackgroundFocusLost,
  199.         }, {
  200.             TextInput = Roact.createElement(TextInput, {
  201.                 Position = UDim2.new(0, leftPadding, 0.5, 0),
  202.                 Size = UDim2.new(1, -(buttonsWidth + (leftPadding * 2)), 1, -2*topPadding),
  203.                 AnchorPoint = Vector2.new(0, 0.5),
  204.                 PlaceholderText = placeholderText,
  205.                 Text = text,
  206.                 OnTextChanged = self.onTextChanged,
  207.                 OnFocusGained = self.onTextBoxFocusGained,
  208.                 OnFocusLost = self.onTextBoxFocusLost,
  209.                 ShouldFocus = shouldFocus
  210.             }),
  211.  
  212.             Buttons = Roact.createElement("Frame", {
  213.                 AnchorPoint = Vector2.new(1, 0),
  214.                 Position = UDim2.new(1, 0, 0, 0),
  215.                 Size = UDim2.new(0, buttonsWidth, 1, 0),
  216.                 BackgroundTransparency = 1,
  217.             }, {
  218.                 Layout = Roact.createElement("UIListLayout", {
  219.                     FillDirection = Enum.FillDirection.Horizontal,
  220.                     HorizontalAlignment = Enum.HorizontalAlignment.Right,
  221.                     VerticalAlignment = Enum.VerticalAlignment.Center,
  222.                     SortOrder = Enum.SortOrder.LayoutOrder,
  223.                 }),
  224.  
  225.                 ClearButton = showClearButton and Roact.createElement(Button, {
  226.                     Size = buttonSize,
  227.                     LayoutOrder = 1,
  228.                     OnClick = self.onClearButtonClicked,
  229.                     Style = style.Buttons.Clear
  230.                 }),
  231.  
  232.                 Separator = showSearchButton and Roact.createElement(Separator, {
  233.                     DominantAxis = Enum.DominantAxis.Height,
  234.                     LayoutOrder = 2,
  235.                     Position = UDim2.new(0, buttonWidth, 0.5, 0)
  236.                 }),
  237.  
  238.                 SearchButton = showSearchButton and Roact.createElement(Button, {
  239.                     Size = buttonSize,
  240.                     LayoutOrder = 3,
  241.                     OnClick = self.requestSearch,
  242.                     Style = style.Buttons.Search
  243.                 }),
  244.             })
  245.         })
  246.     })
  247. end
  248.  
  249. ContextServices.mapToProps(MainBar, {
  250.     Stylizer = FlagsList:get("FFlagRefactorDevFrameworkTheme") and ContextServices.Stylizer or nil,
  251.     Theme = (not FlagsList:get("FFlagRefactorDevFrameworkTheme")) and ContextServices.Theme or nil,
  252. })
  253.  
  254. return MainBar
Advertisement
Add Comment
Please, Sign In to add comment