Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix input range on Safari #352

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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