-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an autocomplete form input (#2609)
Co-authored-by: camertron <[email protected]>
- Loading branch information
Showing
19 changed files
with
237 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@primer/view-components": minor | ||
--- | ||
|
||
Add an AutoComplete form input |
Binary file modified
BIN
+5.29 KB
(110%)
....test.ts-snapshots/primer/alpha/action_list/long_label_with_tooltip/focused.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+5.27 KB
(110%)
...hots.test.ts-snapshots/primer/beta/nav_list/long_label_with_tooltip/focused.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../previews/primer/url_helpers" | ||
|
||
# :nodoc: | ||
class AutoCompleteForm < ApplicationForm | ||
form do |auto_complete_form| | ||
auto_complete_form.auto_complete( | ||
name: :fruit, | ||
label: "Fruit", | ||
caption: "Please enter your favorite fruit", | ||
src: Primer::UrlHelpers.autocomplete_index_path, | ||
validation_message: "Something went wrong" | ||
) | ||
|
||
auto_complete_form.submit(label: "Submit", name: :submit) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<%= render(Primer::Beta::AutoComplete.new(**auto_complete_arguments)) do |autocomplete| %> | ||
<% @input.block.call(autocomplete) if @input.block %> | ||
<% autocomplete.with_input(**input_arguments) %> | ||
<% end %> | ||
<%= render(ValidationMessage.new(input: @input)) %> | ||
<%= render(Caption.new(input: @input)) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module Primer | ||
module Forms | ||
# :nodoc: | ||
class AutoComplete < BaseComponent | ||
ARGUMENT_TYPES = %i(keyreq key).freeze | ||
|
||
delegate :builder, :form, to: :@input | ||
|
||
def initialize(input:) | ||
@input = input | ||
@input.merge_input_arguments!(text_field_attributes.deep_symbolize_keys) | ||
end | ||
|
||
def self.auto_complete_argument_names | ||
@auto_complete_argument_names ||= | ||
Primer::Beta::AutoComplete.instance_method(:initialize) | ||
.parameters | ||
.filter_map { |(type, param_name)| next param_name if ARGUMENT_TYPES.include?(type) } | ||
end | ||
|
||
private | ||
|
||
def all_input_arguments | ||
@all_input_arguments ||= @input.input_arguments.deep_dup.tap do |args| | ||
# rails uses :class but PVC wants :classes | ||
args[:classes] = class_names( | ||
args[:classes], | ||
args.delete(:class) | ||
) | ||
end | ||
end | ||
|
||
def auto_complete_arguments | ||
all_args = all_input_arguments | ||
all_args | ||
.slice(*self.class.auto_complete_argument_names) | ||
.merge( | ||
input_name: all_args[:name], | ||
input_id: all_args[:id], | ||
label_text: @input.label, | ||
list_id: "#{all_args[:id]}-list" | ||
) | ||
end | ||
|
||
def input_arguments | ||
all_input_arguments.except(*self.class.auto_complete_argument_names, :id, :name) | ||
end | ||
|
||
def text_field_attributes | ||
builder.text_field_attributes(@input.name).except("size", "value") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Primer | ||
module Forms | ||
module Dsl | ||
# :nodoc: | ||
class AutoCompleteInput < Input | ||
attr_reader :name, :label, :block | ||
|
||
def initialize(name:, label:, **system_arguments, &block) | ||
@name = name | ||
@label = label | ||
@block = block | ||
|
||
super(**system_arguments) | ||
end | ||
|
||
def to_component | ||
AutoComplete.new(input: self) | ||
end | ||
|
||
def type | ||
:autocomplete | ||
end | ||
|
||
# The AutoComplete Primer component does not allow auto-focusing | ||
def focusable? | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= primer_form_with(url: generic_form_submission_path) do |f| %> | ||
<%= render(AutoCompleteForm.new(f)) %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require "lib/test_helper" | ||
require_relative "models/deep_thought" | ||
|
||
class Primer::Forms::AutoCompleteInputTest < Minitest::Test | ||
include Primer::ComponentTestHelpers | ||
|
||
def test_hidden_auto_complete | ||
render_in_view_context do | ||
primer_form_with(url: "/foo") do |f| | ||
render_inline_form(f) do |auto_complete_form| | ||
auto_complete_form.auto_complete(name: :foo, label: "Foo", src: "/items", hidden: true) | ||
end | ||
end | ||
end | ||
|
||
assert_selector "input[type=text]#foo", visible: :hidden | ||
end | ||
|
||
def test_only_primer_error_markup | ||
model = DeepThought.new(41) | ||
model.valid? # populate validation error messages | ||
|
||
render_in_view_context do | ||
primer_form_with(model: model, url: "/foo") do |f| | ||
render_inline_form(f) do |auto_complete_form| | ||
auto_complete_form.auto_complete(name: :ultimate_answer, label: "Ultimate answer", src: "/items") | ||
end | ||
end | ||
end | ||
|
||
# primer error markup | ||
assert_selector ".FormControl-inlineValidation", text: "Ultimate answer must be greater than 41" | ||
|
||
# no rails error markup | ||
refute_selector ".field_with_errors", visible: :all | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters