Skip to content

Commit

Permalink
Fix input range on Safari (#352)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonatan Kłosko <[email protected]>
  • Loading branch information
cristineguadelupe and jonatanklosko authored Oct 6, 2023
1 parent 77a8104 commit 2ce0c36
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/kino/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,18 @@ defmodule Kino.Input do
* `:step` - the slider increment
* `:debounce` - determines when input changes are emitted. When
set to `:blur`, the change propagates when the user leaves the
input. When set to a non-negative number of milliseconds, the
change propagates after the specified delay. Defaults to `:blur`
set to a non-negative number of milliseconds, the change propagates
after the specified delay. Defaults to `250`
"""
@spec range(String.t(), keyword()) :: t()
def range(label, opts \\ []) when is_binary(label) and is_list(opts) do
min = Keyword.get(opts, :min, 0)
max = Keyword.get(opts, :max, 100)
step = Keyword.get(opts, :step, 1)
default = Keyword.get(opts, :default, min)
debounce = Keyword.get(opts, :debounce, :blur)
# In Safari range input is blurred as soon as it's clicked,
# so we don't support blur as debounce for this input
debounce = Keyword.get(opts, :debounce, 250)

if min >= max do
raise ArgumentError,
Expand All @@ -301,7 +302,10 @@ defmodule Kino.Input do
"expected :default to be between :min and :max, got: #{inspect(default)}"
end

assert_debounce_value!(debounce)
unless is_number(debounce) and debounce >= 0 do
raise ArgumentError,
~s/expected :debounce to be a non-negative number, got: #{inspect(debounce)}/
end

new(%{
type: :range,
Expand Down
8 changes: 8 additions & 0 deletions test/kino/input_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ defmodule Kino.InputTest do
Kino.Input.range("Length", min: 0, max: 10, default: 20)
end
end

test "raises an error when debounce is not a positive integer" do
assert_raise ArgumentError,
"expected :debounce to be a non-negative number, got: :blur",
fn ->
Kino.Input.range("Length", debounce: :blur)
end
end
end

describe "date/2" do
Expand Down

0 comments on commit 2ce0c36

Please sign in to comment.