mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-09-08 22:14:58 +08:00
Add numeric input (#352)
This commit is contained in:
parent
4cd62e5285
commit
e75ef5f339
3 changed files with 61 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
49
test/livebook/notebook/cell/input.exs
Normal file
49
test/livebook/notebook/cell/input.exs
Normal 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
|
Loading…
Add table
Reference in a new issue