From 2ce0c368246bee62aae83a33d0d0b837045c39a7 Mon Sep 17 00:00:00 2001 From: Cristine Guadelupe Date: Fri, 6 Oct 2023 14:06:47 +0700 Subject: [PATCH] Fix input range on Safari (#352) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonatan KÅ‚osko --- lib/kino/input.ex | 14 +++++++++----- test/kino/input_test.exs | 8 ++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/kino/input.ex b/lib/kino/input.ex index 74cfe59d..f38e1a03 100644 --- a/lib/kino/input.ex +++ b/lib/kino/input.ex @@ -273,9 +273,8 @@ 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 @@ -283,7 +282,9 @@ defmodule Kino.Input do 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, @@ -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, diff --git a/test/kino/input_test.exs b/test/kino/input_test.exs index abd4f205..e8d7e212 100644 --- a/test/kino/input_test.exs +++ b/test/kino/input_test.exs @@ -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