Skip to content

Commit

Permalink
Implement OnCompleted prop for Slider (#46)
Browse files Browse the repository at this point in the history
* add oncompleted prop to slider
* forward numericinput onsubmitted to slider oncompleted
* update changelog
  • Loading branch information
sircfenner authored Jun 24, 2024
1 parent b825497 commit 722051b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fixed image links in documentation
- Added OnCompleted prop to Slider

## 1.0.0

Expand Down
2 changes: 2 additions & 0 deletions src/Components/NumericInput.luau
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Use the `Arrows` and `Slider` props to specify whether up/down arrows and a slider should be
included. If arrows or a slider are displayed, they will increment the value by the amount of the step.
Completing a slide with a slider will call the `OnSubmitted` prop (if provided) with the latest value.
Only decimal inputs are allowed (so, for example, hex characters a-f will not be permitted).
]=]
Expand Down Expand Up @@ -327,6 +328,7 @@ local function NumericInput(props: NumericInputProps)
Max = max,
Step = step,
OnChanged = props.OnValidChanged,
OnCompleted = props.OnSubmitted,
Disabled = props.Disabled,
}),
})
Expand Down
21 changes: 19 additions & 2 deletions src/Components/Slider.luau
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
end
```
Optionally, an `OnCompleted` callback prop can be provided. This will be called with the latest
value of the Slider when sliding is finished. It is also called if, while sliding is in progress,
the component becomes Disabled via props or is unmounted.
Two further props can optionally be provided:
1. `Border` determines whether a border is drawn around the component.
This is useful for giving visual feedback when the slider is hovered or selected.
Expand All @@ -47,6 +51,8 @@ local React = require("@pkg/@jsdotlua/react")

local CommonProps = require("../CommonProps")
local Constants = require("../Constants")

local useFreshCallback = require("../Hooks/useFreshCallback")
local useMouseDrag = require("../Hooks/useMouseDrag")
local useTheme = require("../Hooks/useTheme")

Expand All @@ -58,6 +64,7 @@ local useTheme = require("../Hooks/useTheme")
@field ... CommonProps
@field Value number
@field OnChanged ((newValue: number) -> ())?
@field OnCompleted ((newValue: number) -> ())?
@field Min number
@field Max number
@field Step number?
Expand All @@ -68,6 +75,7 @@ local useTheme = require("../Hooks/useTheme")
type SliderProps = CommonProps.T & {
Value: number,
OnChanged: ((newValue: number) -> ())?,
OnCompleted: ((newValue: number) -> ())?,
Min: number,
Max: number,
Step: number?,
Expand All @@ -86,7 +94,7 @@ local function Slider(props: SliderProps)

local onChanged: (number) -> () = props.OnChanged or function() end

local drag = useMouseDrag(function(rbx: GuiObject, input: InputObject)
local dragCallback = function(rbx: GuiObject, input: InputObject)
local regionPos = rbx.AbsolutePosition.X + PADDING_REGION_SIDE
local regionSize = rbx.AbsoluteSize.X - PADDING_REGION_SIDE * 2
local inputPos = input.Position.X
Expand All @@ -102,7 +110,16 @@ local function Slider(props: SliderProps)
if value ~= props.Value then
onChanged(value)
end
end, { props.Value, props.Min, props.Max, props.Step, onChanged } :: { unknown })
end

local dragEndedCallback = useFreshCallback(function()
if props.OnCompleted then
props.OnCompleted(props.Value)
end
end, { props.Value, props.OnCompleted } :: { unknown })

local dragDeps = { props.Value, props.Min, props.Max, props.Step, props.OnCompleted, onChanged } :: { unknown }
local drag = useMouseDrag(dragCallback, dragDeps, nil, dragEndedCallback)

local hovered, setHovered = React.useState(false)
local mainModifier = Enum.StudioStyleGuideModifier.Default
Expand Down
2 changes: 1 addition & 1 deletion src/Hooks/useMouseDrag.luau
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ local function useMouseDrag(
moveConnection.current:Disconnect()
moveConnection.current = nil
end
if onEndedCallback then
if onEndedCallback and holding.current == true then
onEndedCallback()
end
end
Expand Down

0 comments on commit 722051b

Please sign in to comment.