polaris_view_components 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/polaris_view_components/autocomplete_controller.js +32 -15
  3. data/app/assets/javascripts/polaris_view_components/dropzone_controller.js +495 -0
  4. data/app/assets/javascripts/polaris_view_components/index.js +2 -0
  5. data/app/assets/javascripts/polaris_view_components/popover_controller.js +9 -2
  6. data/app/assets/javascripts/polaris_view_components/utils.js +23 -0
  7. data/app/assets/javascripts/polaris_view_components.js +533 -63
  8. data/app/assets/stylesheets/polaris_view_components/custom.css +38 -6
  9. data/app/assets/stylesheets/polaris_view_components.css +2102 -2021
  10. data/app/assets/stylesheets/polaris_view_components.postcss.css +1 -1
  11. data/app/components/polaris/action_list/item_component.rb +1 -1
  12. data/app/components/polaris/action_list/section_component.rb +1 -1
  13. data/app/components/polaris/action_list_component.rb +1 -1
  14. data/app/components/polaris/autocomplete/action_component.rb +1 -1
  15. data/app/components/polaris/autocomplete/option_component.rb +1 -1
  16. data/app/components/polaris/autocomplete/section_component.rb +1 -1
  17. data/app/components/polaris/autocomplete_component.rb +9 -2
  18. data/app/components/polaris/avatar_component.rb +1 -1
  19. data/app/components/polaris/badge_component.rb +1 -1
  20. data/app/components/polaris/banner_component.rb +2 -2
  21. data/app/components/polaris/base_button.rb +1 -1
  22. data/app/components/polaris/base_checkbox.rb +1 -1
  23. data/app/components/polaris/base_component.rb +1 -1
  24. data/app/components/polaris/base_radio_button.rb +1 -1
  25. data/app/components/polaris/button_group_component.rb +3 -3
  26. data/app/components/polaris/callout_card_component.rb +1 -1
  27. data/app/components/polaris/caption_component.rb +1 -1
  28. data/app/components/polaris/card/header_component.rb +1 -1
  29. data/app/components/polaris/card/section_component.rb +2 -2
  30. data/app/components/polaris/card_component.rb +1 -1
  31. data/app/components/polaris/checkbox_component.rb +1 -1
  32. data/app/components/polaris/choice_component.rb +1 -1
  33. data/app/components/polaris/choice_list_component.rb +1 -1
  34. data/app/components/polaris/component.rb +6 -1
  35. data/app/components/polaris/data_table/cell_component.rb +1 -1
  36. data/app/components/polaris/data_table/column_component.rb +1 -1
  37. data/app/components/polaris/data_table_component.rb +1 -1
  38. data/app/components/polaris/description_list_component.rb +2 -2
  39. data/app/components/polaris/display_text_component.rb +1 -1
  40. data/app/components/polaris/dropzone_component.html.erb +156 -0
  41. data/app/components/polaris/dropzone_component.rb +150 -0
  42. data/app/components/polaris/empty_state_component.rb +1 -1
  43. data/app/components/polaris/exception_list/item_component.rb +1 -1
  44. data/app/components/polaris/exception_list_component.rb +1 -1
  45. data/app/components/polaris/filters_component.rb +3 -3
  46. data/app/components/polaris/footer_help_component.rb +1 -1
  47. data/app/components/polaris/form_layout/group_component.rb +2 -2
  48. data/app/components/polaris/form_layout/item_component.rb +1 -1
  49. data/app/components/polaris/form_layout_component.rb +1 -1
  50. data/app/components/polaris/frame/save_bar_component.rb +1 -1
  51. data/app/components/polaris/frame/top_bar_component.rb +1 -1
  52. data/app/components/polaris/frame_component.rb +1 -1
  53. data/app/components/polaris/heading_component.rb +1 -1
  54. data/app/components/polaris/headless_button.rb +1 -1
  55. data/app/components/polaris/icon_component.rb +1 -1
  56. data/app/components/polaris/index_table/cell_component.rb +1 -1
  57. data/app/components/polaris/index_table/column_component.rb +1 -1
  58. data/app/components/polaris/index_table_component.rb +1 -1
  59. data/app/components/polaris/inline_error_component.rb +1 -1
  60. data/app/components/polaris/label_component.rb +1 -1
  61. data/app/components/polaris/labelled_component.rb +1 -1
  62. data/app/components/polaris/layout/annotated_section.rb +1 -1
  63. data/app/components/polaris/layout/section.rb +1 -1
  64. data/app/components/polaris/layout_component.rb +1 -1
  65. data/app/components/polaris/link_component.rb +1 -1
  66. data/app/components/polaris/list_component.rb +2 -2
  67. data/app/components/polaris/modal/section_component.rb +1 -1
  68. data/app/components/polaris/modal_component.rb +1 -1
  69. data/app/components/polaris/navigation/item_component.rb +2 -2
  70. data/app/components/polaris/navigation/section_component.rb +2 -2
  71. data/app/components/polaris/navigation_component.rb +1 -1
  72. data/app/components/polaris/option_list/checkbox_component.rb +1 -1
  73. data/app/components/polaris/option_list/option_component.rb +1 -1
  74. data/app/components/polaris/option_list/radio_button_component.rb +1 -1
  75. data/app/components/polaris/option_list/section_component.rb +1 -1
  76. data/app/components/polaris/option_list_component.rb +1 -1
  77. data/app/components/polaris/page_actions_component.rb +1 -1
  78. data/app/components/polaris/page_component.rb +1 -1
  79. data/app/components/polaris/pagination_component.rb +1 -1
  80. data/app/components/polaris/popover/pane_component.rb +1 -1
  81. data/app/components/polaris/popover/section_component.rb +1 -1
  82. data/app/components/polaris/popover_component.rb +10 -4
  83. data/app/components/polaris/progress_bar_component.rb +1 -1
  84. data/app/components/polaris/radio_button_component.rb +1 -1
  85. data/app/components/polaris/resource_item_component.rb +1 -1
  86. data/app/components/polaris/resource_list_component.rb +1 -1
  87. data/app/components/polaris/scrollable_component.rb +1 -1
  88. data/app/components/polaris/select_component.rb +1 -1
  89. data/app/components/polaris/setting_toggle_component.rb +1 -1
  90. data/app/components/polaris/shopify_navigation_component.rb +2 -2
  91. data/app/components/polaris/skeleton_body_text_component.rb +1 -1
  92. data/app/components/polaris/spacer_component.rb +1 -1
  93. data/app/components/polaris/spinner_component.rb +1 -1
  94. data/app/components/polaris/stack/item_component.rb +15 -0
  95. data/app/components/polaris/stack_component.rb +2 -18
  96. data/app/components/polaris/subheading_component.rb +1 -1
  97. data/app/components/polaris/tabs/tab_component.rb +1 -1
  98. data/app/components/polaris/tabs_component.rb +1 -1
  99. data/app/components/polaris/tag_component.rb +3 -2
  100. data/app/components/polaris/text_container_component.rb +1 -1
  101. data/app/components/polaris/text_field_component.rb +2 -2
  102. data/app/components/polaris/text_style_component.rb +1 -1
  103. data/app/components/polaris/thumbnail_component.rb +1 -1
  104. data/app/components/polaris/toast_component.rb +1 -1
  105. data/app/components/polaris/top_bar/user_menu_component.rb +1 -1
  106. data/app/components/polaris/visually_hidden_component.rb +1 -1
  107. data/app/helpers/polaris/form_builder.rb +14 -5
  108. data/app/helpers/polaris/view_helper.rb +2 -1
  109. data/lib/polaris/view_components/engine.rb +5 -1
  110. data/lib/polaris/view_components/version.rb +1 -1
  111. metadata +7 -9
  112. data/app/components/polaris/application_component.rb +0 -35
  113. data/app/components/polaris/dropzone/component.html.erb +0 -72
  114. data/app/components/polaris/dropzone/component.rb +0 -128
  115. data/app/components/polaris/dropzone/controller.js +0 -226
  116. data/app/components/polaris/dropzone/utils.js +0 -57
  117. data/app/components/polaris/new_component.rb +0 -10
  118. data/app/helpers/polaris/conditional_helper.rb +0 -11
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Polaris
4
- class TextFieldComponent < Polaris::NewComponent
4
+ class TextFieldComponent < Polaris::Component
5
5
  TYPE_DEFAULT = :text
6
6
  TYPE_OPTIONS = %i[
7
7
  text number email password search tel url date
@@ -180,7 +180,7 @@ module Polaris
180
180
  @type == :number && !@disabled
181
181
  end
182
182
 
183
- class Affix < Polaris::NewComponent
183
+ class Affix < Polaris::Component
184
184
  def initialize(icon: nil)
185
185
  @icon = icon
186
186
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Polaris
4
- class TextStyleComponent < Polaris::NewComponent
4
+ class TextStyleComponent < Polaris::Component
5
5
  VARIATION_DEFAULT = :default
6
6
  VARIATION_MAPPINGS = {
7
7
  VARIATION_DEFAULT => "",
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Polaris
4
- class ThumbnailComponent < Polaris::NewComponent
4
+ class ThumbnailComponent < Polaris::Component
5
5
  SIZE_DEFAULT = :medium
6
6
  SIZE_MAPPINGS = {
7
7
  small: "Polaris-Thumbnail--sizeSmall",
@@ -1,4 +1,4 @@
1
- class Polaris::ToastComponent < Polaris::NewComponent
1
+ class Polaris::ToastComponent < Polaris::Component
2
2
  renders_one :action, ->(**system_arguments) do
3
3
  Polaris::ButtonComponent.new(plain: true, monochrome: true, **system_arguments)
4
4
  end
@@ -1,4 +1,4 @@
1
- class Polaris::TopBar::UserMenuComponent < Polaris::NewComponent
1
+ class Polaris::TopBar::UserMenuComponent < Polaris::Component
2
2
  renders_one :avatar, Polaris::AvatarComponent
3
3
 
4
4
  def initialize(name:, detail:, **system_arguments)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Polaris
4
- class VisuallyHiddenComponent < Polaris::NewComponent
4
+ class VisuallyHiddenComponent < Polaris::Component
5
5
  def initialize
6
6
  end
7
7
 
@@ -10,12 +10,13 @@ module Polaris
10
10
  return if object.blank?
11
11
  return unless object.errors.any?
12
12
 
13
- model_name = object.class.model_name.human.downcase
13
+ title = I18n.t(
14
+ "polaris.form_builder.errors_summary",
15
+ count: object.errors.count,
16
+ model: object.class.model_name.human.downcase
17
+ )
14
18
 
15
- render Polaris::BannerComponent.new(
16
- title: "There's #{pluralize(object.errors.count, "error")} with this #{model_name}:",
17
- status: :critical
18
- ) do
19
+ render Polaris::BannerComponent.new(title: title, status: :critical) do
19
20
  render(Polaris::ListComponent.new) do |list|
20
21
  object.errors.full_messages.each do |error|
21
22
  list.item { error.html_safe }
@@ -75,5 +76,13 @@ module Polaris
75
76
  end
76
77
  render Polaris::RadioButtonComponent.new(form: self, attribute: method, **options, &block)
77
78
  end
79
+
80
+ def polaris_dropzone(method, **options, &block)
81
+ options[:error] ||= error_for(method)
82
+ if options[:error_hidden] && options[:error]
83
+ options[:error] = !!options[:error]
84
+ end
85
+ render Polaris::DropzoneComponent.new(form: self, attribute: method, **options, &block)
86
+ end
78
87
  end
79
88
  end
@@ -22,7 +22,7 @@ module Polaris
22
22
  data_table: "Polaris::DataTableComponent",
23
23
  description_list: "Polaris::DescriptionListComponent",
24
24
  display_text: "Polaris::DisplayTextComponent",
25
- dropzone: "Polaris::Dropzone::Component",
25
+ dropzone: "Polaris::DropzoneComponent",
26
26
  empty_state: "Polaris::EmptyStateComponent",
27
27
  exception_list: "Polaris::ExceptionListComponent",
28
28
  footer_help: "Polaris::FooterHelpComponent",
@@ -51,6 +51,7 @@ module Polaris
51
51
  setting_toggle: "Polaris::SettingToggleComponent",
52
52
  shopify_navigation: "Polaris::ShopifyNavigationComponent",
53
53
  stack: "Polaris::StackComponent",
54
+ stack_item: "Polaris::Stack::ItemComponent",
54
55
  subheading: "Polaris::SubheadingComponent",
55
56
  scrollable: "Polaris::ScrollableComponent",
56
57
  spinner: "Polaris::SpinnerComponent",
@@ -15,6 +15,11 @@ module Polaris
15
15
  #{root}/app/helpers
16
16
  ]
17
17
 
18
+ # Remove default wrapping .field_with_errors for proper Shopify form validations
19
+ config.to_prepare do
20
+ ActionView::Base.field_error_proc = ->(html_tag, _instance) { html_tag.html_safe }
21
+ end
22
+
18
23
  initializer "polaris_view_components.assets" do |app|
19
24
  if app.config.respond_to?(:assets)
20
25
  app.config.assets.precompile += %w[
@@ -33,7 +38,6 @@ module Polaris
33
38
  ActiveSupport.on_load(:action_controller_base) do
34
39
  helper Polaris::ViewHelper
35
40
  helper Polaris::UrlHelper
36
- helper Polaris::ConditionalHelper
37
41
  end
38
42
  end
39
43
  end
@@ -1,5 +1,5 @@
1
1
  module Polaris
2
2
  module ViewComponents
3
- VERSION = "0.7.0"
3
+ VERSION = "0.8.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polaris_view_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Gamble
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-12-20 00:00:00.000000000 Z
12
+ date: 2022-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -450,6 +450,7 @@ files:
450
450
  - app/assets/javascripts/polaris_view_components.js
451
451
  - app/assets/javascripts/polaris_view_components/autocomplete_controller.js
452
452
  - app/assets/javascripts/polaris_view_components/button_controller.js
453
+ - app/assets/javascripts/polaris_view_components/dropzone_controller.js
453
454
  - app/assets/javascripts/polaris_view_components/frame_controller.js
454
455
  - app/assets/javascripts/polaris_view_components/index.js
455
456
  - app/assets/javascripts/polaris_view_components/modal_controller.js
@@ -461,6 +462,7 @@ files:
461
462
  - app/assets/javascripts/polaris_view_components/select_controller.js
462
463
  - app/assets/javascripts/polaris_view_components/text_field_controller.js
463
464
  - app/assets/javascripts/polaris_view_components/toast_controller.js
465
+ - app/assets/javascripts/polaris_view_components/utils.js
464
466
  - app/assets/stylesheets/polaris_view_components.css
465
467
  - app/assets/stylesheets/polaris_view_components.postcss.css
466
468
  - app/assets/stylesheets/polaris_view_components/custom.css
@@ -473,7 +475,6 @@ files:
473
475
  - app/components/polaris/action_list/section_component.rb
474
476
  - app/components/polaris/action_list_component.html.erb
475
477
  - app/components/polaris/action_list_component.rb
476
- - app/components/polaris/application_component.rb
477
478
  - app/components/polaris/autocomplete/action_component.rb
478
479
  - app/components/polaris/autocomplete/option_component.rb
479
480
  - app/components/polaris/autocomplete/section_component.html.erb
@@ -519,10 +520,8 @@ files:
519
520
  - app/components/polaris/description_list_component.html.erb
520
521
  - app/components/polaris/description_list_component.rb
521
522
  - app/components/polaris/display_text_component.rb
522
- - app/components/polaris/dropzone/component.html.erb
523
- - app/components/polaris/dropzone/component.rb
524
- - app/components/polaris/dropzone/controller.js
525
- - app/components/polaris/dropzone/utils.js
523
+ - app/components/polaris/dropzone_component.html.erb
524
+ - app/components/polaris/dropzone_component.rb
526
525
  - app/components/polaris/empty_state_component.html.erb
527
526
  - app/components/polaris/empty_state_component.rb
528
527
  - app/components/polaris/exception_list/item_component.html.erb
@@ -577,7 +576,6 @@ files:
577
576
  - app/components/polaris/navigation/section_component.rb
578
577
  - app/components/polaris/navigation_component.html.erb
579
578
  - app/components/polaris/navigation_component.rb
580
- - app/components/polaris/new_component.rb
581
579
  - app/components/polaris/option_list/checkbox_component.html.erb
582
580
  - app/components/polaris/option_list/checkbox_component.rb
583
581
  - app/components/polaris/option_list/option_component.rb
@@ -618,6 +616,7 @@ files:
618
616
  - app/components/polaris/spacer_component.rb
619
617
  - app/components/polaris/spinner_component.html.erb
620
618
  - app/components/polaris/spinner_component.rb
619
+ - app/components/polaris/stack/item_component.rb
621
620
  - app/components/polaris/stack_component.html.erb
622
621
  - app/components/polaris/stack_component.rb
623
622
  - app/components/polaris/subheading_component.rb
@@ -639,7 +638,6 @@ files:
639
638
  - app/components/polaris/top_bar/user_menu_component.rb
640
639
  - app/components/polaris/visually_hidden_component.rb
641
640
  - app/helpers/polaris/class_name_helper.rb
642
- - app/helpers/polaris/conditional_helper.rb
643
641
  - app/helpers/polaris/fetch_or_fallback_helper.rb
644
642
  - app/helpers/polaris/form_builder.rb
645
643
  - app/helpers/polaris/option_helper.rb
@@ -1,35 +0,0 @@
1
- class Polaris::ApplicationComponent < ViewComponent::Base
2
- include ActiveModel::Validations
3
-
4
- def initialize(data: {}, aria: {}, html_options: {}, **args)
5
- @data = data
6
- @aria = aria
7
- @html_options = html_options
8
- end
9
-
10
- private
11
-
12
- def classes
13
- []
14
- end
15
-
16
- def additional_data
17
- {}
18
- end
19
-
20
- def additional_aria
21
- {}
22
- end
23
-
24
- def content_tag_options
25
- {
26
- class: classes.compact,
27
- data: @data.merge(additional_data),
28
- aria: @aria.merge(additional_aria)
29
- }.merge(@html_options)
30
- end
31
-
32
- def before_render
33
- validate!
34
- end
35
- end
@@ -1,72 +0,0 @@
1
- <%= render Polaris::Labelled::Component.new(**labelled_attrs) do %>
2
- <%= content_tag('div', content_tag_options) do %>
3
- <div
4
- class="Polaris-DropZone__Overlay Polaris-VisuallyHidden"
5
- data-polaris--dropzone-target="overlay"
6
- >
7
- <%= polaris_stack(vertical: true, spacing: :tight) do |stack| %>
8
- <% stack.item do %>
9
- <%= polaris_icon(color: :primary) do %>
10
- <svg viewBox="0 0 20 20" class="Polaris-Icon__Svg" focusable="false" aria-hidden="true">
11
- <path d="M2 2h1V0H1.5A1.5 1.5 0 000 1.5V3h2V2zM5 2h3V0H5v2zM13 2h-3V0h3v2zM0 5v3h2V5H0zM0 10v3h2v-3H0zM18 5h-2v3h2V5zM5 18h3v-2H5v2zM0 17a1 1 0 001 1h2v-2H2v-1H0v2zM16 3V2h-1V0h1.5A1.5 1.5 0 0118 1.5V3h-2z" />
12
-
13
- <path d="M10.814 14H5.5A1.5 1.5 0 014 12.5v-7A1.5 1.5 0 015.5 4h7A1.5 1.5 0 0114 5.5v5.314l5.512 2.506a.832.832 0 01.028 1.501l-2.898 1.45a.832.832 0 00-.372.371l-1.449 2.898a.832.832 0 01-1.501-.028L10.814 14zm-.909-2l-.828-1.821c-.318-.7.402-1.42 1.102-1.102L12 9.905V6H6v6h3.905z" />
14
- </svg>
15
- <% end %>
16
- <% end %>
17
-
18
- <% stack.item do %>
19
- <%= polaris_display_text(size: :small) do %>
20
- <%= @overlay_text %>
21
- <% end %>
22
- <% end %>
23
- <% end %>
24
- </div>
25
-
26
- <div class="Polaris-DropZone__Container" data-polaris--dropzone-target="container">
27
- <div class="Polaris-DropZone-FileUpload" data-polaris--dropzone-target="fileUpload">
28
- <%= polaris_stack(vertical: true) do |stack| %>
29
- <% stack.item do %>
30
- <img width="40" src="data:image/svg+xml,%3csvg fill='none' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill-rule='evenodd' clip-rule='evenodd' d='M20 10a10 10 0 11-20 0 10 10 0 0120 0zM5.3 8.3l4-4a1 1 0 011.4 0l4 4a1 1 0 01-1.4 1.4L11 7.4V15a1 1 0 11-2 0V7.4L6.7 9.7a1 1 0 01-1.4-1.4z' fill='%235C5F62'/%3e%3c/svg%3e" alt="">
31
- <% end %>
32
-
33
- <% stack.item do %>
34
- <div class="Polaris-DropZone-FileUpload__Button">Add file</div>
35
- <% end %>
36
-
37
- <% stack.item do %>
38
- <%= polaris_text_style(variation: :subdued) do %>
39
- or drop files to upload
40
- <% end %>
41
- <% end %>
42
- <% end %>
43
- </div>
44
- </div>
45
-
46
- <%= polaris_visually_hidden do %>
47
- <%= @form.file_field(@attribute, input_attrs) %>
48
- <% end %>
49
-
50
- <template data-polaris--dropzone-target="itemsTemplate">
51
- <%= polaris_stack(vertical: true, data: { rendered: '' }) do %>
52
- <span class="target"></span>
53
- <% end %>
54
- </template>
55
-
56
- <template data-polaris--dropzone-target="itemTemplate">
57
- <div class="Polaris-Stack__Item">
58
- <%= polaris_stack(alignment: :center) do |stack| %>
59
- <% stack.item do %>
60
- <%# polaris_thumnail(source: "TODO", size: :small) %>
61
- <% end %>
62
-
63
- <% stack.item do %>
64
- <div class="content">
65
- <%= polaris_caption_component { "TODO" } %>
66
- </div>
67
- <% end %>
68
- <% end %>
69
- </div>
70
- </template>
71
- <% end %>
72
- <% end %>
@@ -1,128 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Polaris
4
- module Dropzone
5
- class Component < Polaris::Component
6
- ALLOWED_TYPES = %w[file image]
7
-
8
- validates :type, inclusion: {in: ALLOWED_TYPES, message: "%{value} is not a valid type"}
9
- validates :label_action, type: Action, allow_nil: true
10
-
11
- # TODO
12
- # Options missing:
13
- # - open_file_dialog
14
- # - overlay
15
- # - overlay_text
16
- # - Size CSS Class
17
- # - Doesn't take into account an initial load with files
18
-
19
- attr_reader :label_action, :type
20
-
21
- def initialize(
22
- form:,
23
- attribute:,
24
- accept: "",
25
- active: false,
26
- allow_multiple: true,
27
- disabled: false,
28
- drop_on_page: false,
29
- error: false,
30
- error_overlay_text: "",
31
- id: "",
32
- label: "",
33
- label_action: nil,
34
- label_hidden: false,
35
- overlay_text: "Drop file to upload",
36
- outline: true,
37
- type: "file",
38
- **args
39
- )
40
- super
41
-
42
- @form = form
43
- @attribute = attribute
44
- @accept = accept
45
- @active = active
46
- @allow_multiple = allow_multiple
47
- @disabled = disabled
48
- @drop_on_page = drop_on_page
49
- @error = error
50
- @error_overlay_text = error_overlay_text
51
- @id = id
52
- @label = label
53
- @label_action = label_action
54
- @label_hidden = label_hidden
55
- @overlay_text = overlay_text
56
- @outline = outline
57
- @type = type
58
- end
59
-
60
- def labelled_attrs
61
- {
62
- form: @form,
63
- attribute: @attribute,
64
- error: @error,
65
- label: @label,
66
- label_hidden: @label_hidden,
67
- index: @index,
68
- action: @label_action
69
- }
70
- end
71
-
72
- def input_attrs
73
- {
74
- accept: @accept,
75
- disabled: @disabled,
76
- type: "file",
77
- multiple: @allow_multiple,
78
- data: {
79
- action: "focus->polaris--dropzone#onFocus blur->polaris--dropzone#onBlur change->polaris--dropzone#onChange",
80
- 'polaris--dropzone-target': "input"
81
- }
82
- }
83
- end
84
-
85
- private
86
-
87
- def additional_aria
88
- {
89
- disabled: @disabled.to_s
90
- }
91
- end
92
-
93
- def additional_data
94
- {
95
- controller: "polaris--dropzone",
96
- action: "click->polaris--dropzone#onClick #{drop_actions}",
97
- 'polaris--dropzone-accept-value': @accept,
98
- 'polaris--dropzone-allowMultiple-value': @allow_multiple.to_s,
99
- 'polaris--dropzone-disabled-value': @disabled.to_s,
100
- 'polaris--dropzone-focused-value': "false",
101
- 'polaris--dropzone-drop-on-page-value': "false"
102
- }
103
- end
104
-
105
- def drop_actions
106
- event_scope = @drop_on_page ? "@document" : ""
107
-
108
- [
109
- "drop#{event_scope}->polaris--dropzone#onDrop",
110
- "dragover#{event_scope}->polaris--dropzone#onDragOver",
111
- "dragenter#{event_scope}->polaris--dropzone#onDragEnter",
112
- "dragleave#{event_scope}->polaris--dropzone#onDragLeave"
113
- ].join(" ")
114
- end
115
-
116
- def classes
117
- classes = %w[Polaris-DropZone Polaris-DropZone--sizeExtraLarge]
118
-
119
- classes << "Polaris-DropZone--isDisabled" if @disabled
120
- classes << "Polaris-DropZone--isDisabled" if @disabled
121
- classes << "Polaris-DropZone--hasError" if @error.present?
122
- classes << "Polaris-DropZone--hasOutline" if @outline
123
-
124
- classes
125
- end
126
- end
127
- end
128
- end
@@ -1,226 +0,0 @@
1
- import { Controller } from "@hotwired/stimulus"
2
-
3
- import { fileAccepted, getDataTransferFiles } from './utils'
4
-
5
- // eslint-disable-next-line import/no-default-export
6
- export default class extends Controller {
7
- static targets = ['container', 'fileUpload', 'input', 'itemTemplate', 'itemsTemplate', 'overlay']
8
- static values = {
9
- accept: String,
10
- allowMultiple: Boolean,
11
- disabled: Boolean,
12
- dropOnPage: Boolean,
13
- focused: Boolean,
14
- }
15
-
16
- _files = []
17
- _acceptedFiles = []
18
- _rejectedFiles = []
19
- _dragging = false
20
- dragTargets = []
21
- filesRendered = false
22
-
23
- onBlur () {
24
- this.focusedValue = false
25
- }
26
-
27
- onChange (e) {
28
- this.stopEvent(e)
29
- if (this.disabled) return
30
-
31
- this.clearFiles()
32
-
33
- const fileList = getDataTransferFiles(e)
34
- const { files, acceptedFiles, rejectedFiles } = this.getValidatedFiles(fileList)
35
- this.dragTargets = []
36
-
37
- this.files = files
38
- this.acceptedFiles = acceptedFiles
39
- this.rejectedFiles = rejectedFiles
40
- }
41
-
42
- onDragOver (e) {
43
- this.stopEvent(e)
44
- if (this.disabled) return
45
- }
46
-
47
- onDragEnter (e) {
48
- this.stopEvent(e)
49
- if (this.disabled) return
50
-
51
- if (e.target && !this.dragTargets.includes(e.target)) {
52
- this.dragTargets.push(e.target)
53
- }
54
-
55
- if (this.dragging) return
56
-
57
- this.dragging = true
58
- }
59
-
60
- onDragLeave (e) {
61
- this.stopEvent(e)
62
- if (this.disabled) return
63
-
64
- this.dragTargets = this.dragTargets.filter(el => {
65
- const compareNode = this.element
66
-
67
- return el !== e.target && compareNode && compareNode.contains(el)
68
- })
69
-
70
- if (this.dragTargets.length > 0) return
71
-
72
- this.dragging = false
73
- }
74
-
75
- onDrop (e) {
76
- this.stopEvent(e)
77
- if (this.disabled) return
78
-
79
- this.dragging = false
80
-
81
- this.onChange(e)
82
- }
83
-
84
- onFocus () {
85
- this.focusedValue = true
86
- }
87
-
88
- onClick () {
89
- if (this.disabledValue) return
90
-
91
- this.open()
92
- }
93
-
94
- open () {
95
- this.inputTarget.click()
96
- }
97
-
98
- focusedValueChanged () {
99
- this.element.classList.toggle('Polaris-DropZone--focused', this.focusedValue)
100
- }
101
-
102
- stopEvent (e) {
103
- e.preventDefault()
104
- e.stopPropagation()
105
- }
106
-
107
- getValidatedFiles (files) {
108
- const acceptedFiles = []
109
- const rejectedFiles = []
110
-
111
- Array.from(files).forEach(file => {
112
- if (!fileAccepted(file, this.acceptValue)) {
113
- return rejectedFiles.push(file)
114
- }
115
-
116
- acceptedFiles.push(file)
117
- })
118
-
119
- if (!this.allowMultipleValue) {
120
- acceptedFiles.splice(1, acceptedFiles.length)
121
- rejectedFiles.push(...acceptedFiles.slice(1))
122
- }
123
-
124
- return { files, acceptedFiles, rejectedFiles }
125
- }
126
-
127
- renderUploadedFiles () {
128
- if (this.files.length === 0) return
129
-
130
- const clone = this.itemsTemplateTarget.content.cloneNode(true)
131
- const filesTarget = clone.querySelector('.target')
132
-
133
- this.files
134
- .map(file => this.renderFile(file))
135
- .forEach(fragment => filesTarget.parentNode.appendChild(fragment))
136
- filesTarget.remove()
137
-
138
- this.containerTarget.prepend(clone)
139
-
140
- this.filesRendered = true
141
- }
142
-
143
- renderFile (file) {
144
- const clone = this.itemTemplateTarget.content.cloneNode(true)
145
- const [image, content, caption] = [
146
- clone.querySelector('img'),
147
- clone.querySelector('.content'),
148
- clone.querySelector('.Polaris-Caption'),
149
- ]
150
- image.alt = file.name
151
- image.src = window.URL.createObjectURL(file)
152
- content.insertAdjacentText('afterbegin', file.name)
153
- caption.textContent = `${file.size} bytes`
154
-
155
- return clone
156
- }
157
-
158
- clearFiles () {
159
- if (!this.filesRendered) return
160
-
161
- this.acceptedFiles = []
162
- this.files = []
163
- this.rejectedFiles = []
164
-
165
- const rendered = this.element.querySelector('[data-rendered]')
166
- if (!rendered) return
167
-
168
- rendered.remove()
169
- this.filesRendered = false
170
- }
171
-
172
- get dropNode () {
173
- return this.dropOnPageValue ? document : this.element
174
- }
175
-
176
- get disabled () {
177
- return this.disabledValue
178
- }
179
-
180
- set disabled (val) {
181
- this.disabledValue = val
182
- }
183
-
184
- get dragging () {
185
- return this._dragging
186
- }
187
-
188
- set dragging (val) {
189
- this._dragging = val
190
-
191
- this.element.classList.toggle('Polaris-DropZone--isDragging', val)
192
- this.overlayTarget.classList.toggle('Polaris-VisuallyHidden', !val)
193
- }
194
-
195
- get files () {
196
- return this._files
197
- }
198
-
199
- set files (val) {
200
- this._files = val
201
-
202
- const hasFiles = val.length > 0
203
-
204
- this.fileUploadTarget.classList.toggle('Polaris-VisuallyHidden', hasFiles)
205
-
206
- if (hasFiles && !this.filesRendered) {
207
- this.renderUploadedFiles()
208
- }
209
- }
210
-
211
- get acceptedFiles () {
212
- return this._acceptedFiles
213
- }
214
-
215
- set acceptedFiles (val) {
216
- this._acceptedFiles = val
217
- }
218
-
219
- get rejectedFiles () {
220
- return this._rejectedFiles
221
- }
222
-
223
- set rejectedFiles (val) {
224
- this._rejectedFiles = val
225
- }
226
- }