Skip to content

Commit

Permalink
From helpers: added #tel_field helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Mar 10, 2017
1 parent ceeee84 commit fb47963
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/hanami/helpers/form_helper/form_builder.rb
Expand Up @@ -710,6 +710,34 @@ def url_field(name, attributes = {})
input _attributes(:url, name, attrs)
end

# Telephone input
#
# @param name [Symbol] the input name
# @param attributes [Hash] HTML attributes to pass to the input tag
#
# @since x.x.x
#
# @example Basic usage
# <%=
# # ...
# tel_field :telephone
# %>
#
# <!-- output -->
# <input type="tel" name="user[telephone]" id="user-telephone" value="">
#
# @example HTML Attributes
# <%=
# # ...
# telurl_field :telephone, class: "form-control"
# %>
#
# <!-- output -->
# <input type="tel" name="user[telephone]" id="user-telephone" value="" class="form-control">
def tel_field(name, attributes = {})
input _attributes(:tel, name, attributes)
end

# Hidden input
#
# @param name [Symbol] the input name
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.rb
Expand Up @@ -271,6 +271,7 @@ class Book < Dry::Struct
attribute :published_at, Types::String.optional
attribute :website, Types::String.optional
attribute :publisher_email, Types::String.optional
attribute :publisher_telephone, Types::String.optional
attribute :release_date, Types::Form::Date.optional
attribute :release_hour, Types::String.optional
attribute :release_week, Types::String.optional
Expand Down
92 changes: 92 additions & 0 deletions test/form_helper_test.rb
Expand Up @@ -1201,6 +1201,98 @@
end
end

describe '#tel_field' do
it 'renders' do
actual = view.form_for(:book, action) do
tel_field :publisher_telephone
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="book-publisher-telephone" value="">)
end

it "allows to override 'id' attribute" do
actual = view.form_for(:book, action) do
tel_field :publisher_telephone, id: 'publisher-telephone'
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="publisher-telephone" value="">)
end

it "allows to override 'name' attribute" do
actual = view.form_for(:book, action) do
tel_field :publisher_telephone, name: 'book[telephone]'
end.to_s

actual.must_include %(<input type="tel" name="book[telephone]" id="book-publisher-telephone" value="">)
end

it "allows to override 'value' attribute" do
actual = view.form_for(:book, action) do
tel_field :publisher_telephone, value: 'publisher@example.org'
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="book-publisher-telephone" value="publisher@example.org">)
end

it "allows to specify 'multiple' attribute" do
actual = view.form_for(:book, action) do
tel_field :publisher_telephone, multiple: true
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="book-publisher-telephone" value="" multiple="multiple">)
end

it 'allows to specify HTML attributes' do
actual = view.form_for(:book, action) do
tel_field :publisher_telephone, class: 'form-control'
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="book-publisher-telephone" value="" class="form-control">)
end

describe 'with values' do
let(:values) { Hash[book: Book.new(publisher_telephone: val)] }
let(:val) { 'maria@publisher.org' }

it 'renders with value' do
actual = view.form_for(:book, action, values: values) do
tel_field :publisher_telephone
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="book-publisher-telephone" value="maria@publisher.org">)
end

it "allows to override 'value' attribute" do
actual = view.form_for(:book, action, values: values) do
tel_field :publisher_telephone, value: 'publisher@example.org'
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="book-publisher-telephone" value="publisher@example.org">)
end
end

describe 'with filled params' do
let(:params) { Hash[book: { publisher_telephone: val }] }
let(:val) { 'maria@publisher.org' }

it 'renders with value' do
actual = view.form_for(:book, action) do
tel_field :publisher_telephone
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="book-publisher-telephone" value="maria@publisher.org">)
end

it "allows to override 'value' attribute" do
actual = view.form_for(:book, action) do
tel_field :publisher_telephone, value: 'publisher@example.org'
end.to_s

actual.must_include %(<input type="tel" name="book[publisher_telephone]" id="book-publisher-telephone" value="publisher@example.org">)
end
end
end

describe '#file_field' do
it 'renders' do
actual = view.form_for(:book, action) do
Expand Down

0 comments on commit fb47963

Please sign in to comment.