mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-09-10 06:54:28 +08:00
Add support for a color input type (#410)
This renders as input type=color, and stores a 6 digit hex color with leading hash
This commit is contained in:
parent
ea7bba11cd
commit
4241866b5d
6 changed files with 51 additions and 5 deletions
|
@ -75,6 +75,10 @@
|
||||||
@apply w-full px-3 py-2 bg-gray-50 text-sm border border-gray-200 rounded-lg placeholder-gray-400 text-gray-600;
|
@apply w-full px-3 py-2 bg-gray-50 text-sm border border-gray-200 rounded-lg placeholder-gray-400 text-gray-600;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.input[type="color"] {
|
||||||
|
@apply py-0 w-16;
|
||||||
|
}
|
||||||
|
|
||||||
.input--error {
|
.input--error {
|
||||||
@apply bg-red-50 border-red-600 text-red-600;
|
@apply bg-red-50 border-red-600 text-red-600;
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,14 @@ defmodule Livebook.Notebook.Cell.Input do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp validate_value(value, :color) do
|
||||||
|
if Utils.valid_hex_color?(value) do
|
||||||
|
:ok
|
||||||
|
else
|
||||||
|
{:error, "not a valid hex color"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Checks if the input changed in terms of content.
|
Checks if the input changed in terms of content.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -63,7 +63,7 @@ defmodule Livebook.Users.User do
|
||||||
defp change_name({user, errors}, _attrs), do: {user, errors}
|
defp change_name({user, errors}, _attrs), do: {user, errors}
|
||||||
|
|
||||||
defp change_hex_color({user, errors}, %{"hex_color" => hex_color}) do
|
defp change_hex_color({user, errors}, %{"hex_color" => hex_color}) do
|
||||||
if hex_color_valid?(hex_color) do
|
if Utils.valid_hex_color?(hex_color) do
|
||||||
{%{user | hex_color: hex_color}, errors}
|
{%{user | hex_color: hex_color}, errors}
|
||||||
else
|
else
|
||||||
{user, [{:hex_color, "not a valid color"} | errors]}
|
{user, [{:hex_color, "not a valid color"} | errors]}
|
||||||
|
@ -72,8 +72,6 @@ defmodule Livebook.Users.User do
|
||||||
|
|
||||||
defp change_hex_color({user, errors}, _attrs), do: {user, errors}
|
defp change_hex_color({user, errors}, _attrs), do: {user, errors}
|
||||||
|
|
||||||
defp hex_color_valid?(hex_color), do: hex_color =~ ~r/^#[0-9a-fA-F]{6}$/
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns a random hex color for a user.
|
Returns a random hex color for a user.
|
||||||
|
|
||||||
|
|
|
@ -115,4 +115,24 @@ defmodule Livebook.Utils do
|
||||||
uri = URI.parse(url)
|
uri = URI.parse(url)
|
||||||
uri.scheme != nil and uri.host != nil and uri.host =~ "."
|
uri.scheme != nil and uri.host != nil and uri.host =~ "."
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Validates if the given hex color is the correct format
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> Livebook.Utils.valid_hex_color?("#111111")
|
||||||
|
true
|
||||||
|
|
||||||
|
iex> Livebook.Utils.valid_hex_color?("#ABC123")
|
||||||
|
true
|
||||||
|
|
||||||
|
iex> Livebook.Utils.valid_hex_color?("ABCDEF")
|
||||||
|
false
|
||||||
|
|
||||||
|
iex> Livebook.Utils.valid_hex_color?("#111")
|
||||||
|
false
|
||||||
|
"""
|
||||||
|
@spec valid_hex_color?(String.t()) :: boolean()
|
||||||
|
def valid_hex_color?(hex_color), do: hex_color =~ ~r/^#[0-9a-fA-F]{6}$/
|
||||||
end
|
end
|
||||||
|
|
|
@ -200,7 +200,7 @@ defmodule LivebookWeb.SessionLive.CellComponent do
|
||||||
spellcheck="false"
|
spellcheck="false"
|
||||||
tabindex="-1"><%= [?\n, @cell_view.value] %></textarea>
|
tabindex="-1"><%= [?\n, @cell_view.value] %></textarea>
|
||||||
<% else %>
|
<% else %>
|
||||||
<input type="<%= if(@cell_view.input_type == :password, do: "password", else: "text") %>"
|
<input type="<%= html_input_type(@cell_view.input_type) %>"
|
||||||
data-element="input"
|
data-element="input"
|
||||||
class="input <%= if(@cell_view.error, do: "input--error") %>"
|
class="input <%= if(@cell_view.error, do: "input--error") %>"
|
||||||
name="value"
|
name="value"
|
||||||
|
@ -440,4 +440,9 @@ defmodule LivebookWeb.SessionLive.CellComponent do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp evaluated_label(_time_ms), do: nil
|
defp evaluated_label(_time_ms), do: nil
|
||||||
|
|
||||||
|
defp html_input_type(:password), do: "password"
|
||||||
|
defp html_input_type(:number), do: "number"
|
||||||
|
defp html_input_type(:color), do: "color"
|
||||||
|
defp html_input_type(_), do: "text"
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,7 +28,7 @@ defmodule LivebookWeb.SessionLive.InputCellSettingsComponent do
|
||||||
<div class="flex flex-col space-y-6">
|
<div class="flex flex-col space-y-6">
|
||||||
<div>
|
<div>
|
||||||
<div class="input-label">Type</div>
|
<div class="input-label">Type</div>
|
||||||
<%= render_select("type", [number: "Number", password: "Password", text: "Text", textarea: "Textarea", url: "URL"], @type) %>
|
<%= render_select("type", input_types(), @type) %>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<div class="input-label">Name</div>
|
<div class="input-label">Name</div>
|
||||||
|
@ -69,4 +69,15 @@ defmodule LivebookWeb.SessionLive.InputCellSettingsComponent do
|
||||||
|
|
||||||
%{name: name, type: type, reactive: reactive}
|
%{name: name, type: type, reactive: reactive}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp input_types do
|
||||||
|
[
|
||||||
|
color: "Color",
|
||||||
|
number: "Number",
|
||||||
|
password: "Password",
|
||||||
|
text: "Text",
|
||||||
|
textarea: "Textarea",
|
||||||
|
url: "URL"
|
||||||
|
]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue