Add numeric input (#352)

This commit is contained in:
Jonatan Kłosko 2021-06-16 12:28:47 +02:00 committed by GitHub
parent 4cd62e5285
commit e75ef5f339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 4 deletions

View file

@ -19,7 +19,7 @@ defmodule Livebook.Notebook.Cell.Input do
value: String.t()
}
@type type :: :text | :url
@type type :: :text | :url | :number
@doc """
Returns an empty cell.
@ -53,4 +53,11 @@ defmodule Livebook.Notebook.Cell.Input do
{:error, "not a valid URL"}
end
end
defp validate_value(value, :number) do
case Float.parse(value) do
{_number, ""} -> :ok
_ -> {:error, "not a valid number"}
end
end
end

View file

@ -29,7 +29,7 @@ defmodule LivebookWeb.SessionLive.InputCellSettingsComponent do
<input type="text" class="input" name="name" value="<%= @name %>" spellcheck="false" autocomplete="off" autofocus />
</div>
<div class="mt-4 flex space-x-8 items-center">
<%= render_radios("type", [text: "Text", url: "URL"], @type) %>
<%= render_radios("type", [text: "Text", url: "URL", number: "Number"], @type) %>
</div>
<div class="mt-8 flex justify-end space-x-2">
<%= live_patch "Cancel", to: @return_to, class: "button button-outlined-gray" %>
@ -44,8 +44,9 @@ defmodule LivebookWeb.SessionLive.InputCellSettingsComponent do
end
@impl true
def handle_event("validate", %{"name" => name}, socket) do
{:noreply, assign(socket, name: name)}
def handle_event("validate", %{"name" => name, "type" => type}, socket) do
type = String.to_existing_atom(type)
{:noreply, assign(socket, name: name, type: type)}
end
def handle_event("save", %{"name" => name, "type" => type}, socket) do

View file

@ -0,0 +1,49 @@
defmodule Livebook.Notebook.Cell.InputText do
use ExUnit.Case, async: true
alias Livebook.Notebook.Cell.Input
describe "validate/1" do
test "given text input allows any value" do
input = %{Input.new() | type: :text, value: "some 🐈 text"}
assert Input.validate(input) == :ok
end
test "given url input allows full urls" do
input = %{Input.new() | type: :url, value: "https://example.com"}
assert Input.validate(input) == :ok
input = %{Input.new() | type: :url, value: "https://example.com/some/path"}
assert Input.validate(input) == :ok
input = %{Input.new() | type: :url, value: ""}
assert Input.validate(input) == {:error, "not a valid URL"}
input = %{Input.new() | type: :url, value: "example.com"}
assert Input.validate(input) == {:error, "not a valid URL"}
input = %{Input.new() | type: :url, value: "https://"}
assert Input.validate(input) == {:error, "not a valid URL"}
end
test "given number input allows integers and floats" do
input = %{Input.new() | type: :number, value: "-12"}
assert Input.validate(input) == :ok
input = %{Input.new() | type: :number, value: "3.14"}
assert Input.validate(input) == :ok
input = %{Input.new() | type: :number, value: ""}
assert Input.validate(input) == {:error, "not a valid number"}
input = %{Input.new() | type: :number, value: "1."}
assert Input.validate(input) == {:error, "not a valid number"}
input = %{Input.new() | type: :number, value: ".0"}
assert Input.validate(input) == {:error, "not a valid number"}
input = %{Input.new() | type: :number, value: "-"}
assert Input.validate(input) == {:error, "not a valid number"}
end
end
end