playbook_ui 13.32.0.pre.alpha.PLAY14143272 → 13.32.0.pre.alpha.PLAY14143288

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1604c4acad3552c83fee45b4132254b46891f219aad02f635708b2857d182528
4
- data.tar.gz: 2132b384be205291d751b39474026ad7451f34fa2b871dd232c01902e294b121
3
+ metadata.gz: 5618b32ebc83c42d8613bd4fa8b0a2744a51305118db04bfcf7ac130ea914db8
4
+ data.tar.gz: 54892a5dec259caa818c197140f0438179e1a9e3904438fe0f82aa5d317092b1
5
5
  SHA512:
6
- metadata.gz: 68c47886fc6650571526da2824fec4b23990ab7f42f805eddab67b1069f6900710c745e7af4446d94f7f5b10fe3c0903dc0dff65032bc1a87cfa083ea504f10e
7
- data.tar.gz: 6c44a1561c8a69c55a47ff2f4e3357ef6222d0611eb40f6c68caa177821706b222a240dab64bffb82ae105690eb84d0781e5baf9611f49087de434c7a203a890
6
+ metadata.gz: a44280282310fbba58f3998a32eaa1a0f1979bdba5d28bafcc8de6c3e195295fe5acb1c71beef38666f8918af70f86286818a03250898d4d7d1bc158ef261ec9
7
+ data.tar.gz: 7afa46a34ac936970e8f0113cf2808b2ac56fac7e75c80ab3393c7ee844dbd304879070954e918f69a6281e23ff66f7f8be06fd10e97b3a446d833895813062c
@@ -26,6 +26,7 @@ type DropdownProps = {
26
26
  className?: string;
27
27
  dark?: boolean;
28
28
  data?: { [key: string]: string };
29
+ defaultValue?: GenericObject;
29
30
  error?: string;
30
31
  htmlOptions?: { [key: string]: string | number | boolean | (() => void) },
31
32
  id?: string;
@@ -44,6 +45,7 @@ const Dropdown = (props: DropdownProps) => {
44
45
  className,
45
46
  dark = false,
46
47
  data = {},
48
+ defaultValue = {},
47
49
  error,
48
50
  htmlOptions = {},
49
51
  id,
@@ -66,7 +68,7 @@ const Dropdown = (props: DropdownProps) => {
66
68
  const [isDropDownClosed, setIsDropDownClosed, toggleDropdown] = useDropdown(isClosed);
67
69
 
68
70
  const [filterItem, setFilterItem] = useState("");
69
- const [selected, setSelected] = useState<GenericObject>({});
71
+ const [selected, setSelected] = useState<GenericObject>(defaultValue);
70
72
  const [isInputFocused, setIsInputFocused] = useState(false);
71
73
  const [hasTriggerSubcomponent, setHasTriggerSubcomponent] = useState(true);
72
74
  const [hasContainerSubcomponent, setHasContainerSubcomponent] =
@@ -0,0 +1,10 @@
1
+ <%
2
+ options = [
3
+ { label: 'United States', value: 'United States', id: 'us' },
4
+ { label: 'Canada', value: 'Canada', id: 'ca' },
5
+ { label: 'Pakistan', value: 'Pakistan', id: 'pk' },
6
+ ]
7
+
8
+ %>
9
+
10
+ <%= pb_rails("dropdown", props: {options: options, default_value: options.last}) %>
@@ -0,0 +1,31 @@
1
+ import React from 'react'
2
+ import { Dropdown } from 'playbook-ui'
3
+
4
+ const DropdownDefaultValue = (props) => {
5
+ const options = [
6
+ {
7
+ label: "United States",
8
+ value: "United States",
9
+ },
10
+ {
11
+ label: "Canada",
12
+ value: "Canada",
13
+ },
14
+ {
15
+ label: "Pakistan",
16
+ value: "Pakistan",
17
+ }
18
+ ];
19
+
20
+ return (
21
+ <>
22
+ <Dropdown
23
+ defaultValue={options[2]}
24
+ options={options}
25
+ {...props}
26
+ />
27
+ </>
28
+ )
29
+ }
30
+
31
+ export default DropdownDefaultValue
@@ -8,6 +8,7 @@ examples:
8
8
  - dropdown_with_custom_trigger_rails: Custom Trigger
9
9
  - dropdown_with_custom_padding: Custom Option Padding
10
10
  - dropdown_error: Dropdown with Error
11
+ - dropdown_default_value: Default Value
11
12
 
12
13
  react:
13
14
  - dropdown_default: Default
@@ -18,6 +19,7 @@ examples:
18
19
  - dropdown_with_custom_trigger: Custom Trigger
19
20
  - dropdown_with_custom_padding: Custom Option Padding
20
21
  - dropdown_error: Dropdown with Error
22
+ - dropdown_default_value: Default Value
21
23
  # - dropdown_with_autocomplete: Autocomplete
22
24
  # - dropdown_with_autocomplete_and_custom_display: Autocomplete with Custom Display
23
25
  # - dropdown_with_external_control: useDropdown Hook
@@ -9,4 +9,5 @@ export { default as DropdownWithLabel } from './_dropdown_with_label.jsx'
9
9
  export { default as DropdownWithExternalControl } from './_dropdown_with_external_control.jsx'
10
10
  export { default as DropdownWithHook } from './_dropdown_with_hook.jsx'
11
11
  export { default as DropdownSubcomponentStructure } from './_dropdown_subcomponent_structure.jsx'
12
- export { default as DropdownError } from './_dropdown_error.jsx'
12
+ export { default as DropdownError } from './_dropdown_error.jsx'
13
+ export { default as DropdownDefaultValue } from './_dropdown_default_value.jsx'
@@ -8,8 +8,8 @@
8
8
  <%= pb_rails("caption", props: {text: object.label, margin_bottom:"xs"}) %>
9
9
  <% end %>
10
10
  <div class="dropdown_wrapper<%= error_class %>" style="position: relative">
11
- <input type="hidden" name="<%= object.name %>" id="dropdown-selected-option" value="" />
12
- <input id="dropdown-form-validation" name="<%= object.name %>_form_validation" value="" style="display: none" <%= object.required ? "required" : ""%> />
11
+ <input type="hidden" name="<%= object.name %>" id="dropdown-selected-option" value="<%= input_default_value %>" />
12
+ <input id="dropdown-form-validation" name="<%= object.name %>_form_validation" value="<%= input_default_value %>" style="display: none" <%= object.required ? "required" : ""%> />
13
13
 
14
14
  <% if content.present? %>
15
15
  <%= content.presence %>
@@ -10,6 +10,7 @@ module Playbook
10
10
  prop :error, type: Playbook::Props::String
11
11
  prop :required, type: Playbook::Props::Boolean,
12
12
  default: false
13
+ prop :default_value
13
14
 
14
15
  def data
15
16
  Hash(prop(:data)).merge(pb_dropdown: true)
@@ -24,6 +25,10 @@ module Playbook
24
25
  def error_class
25
26
  error.present? ? " error" : ""
26
27
  end
28
+
29
+ def input_default_value
30
+ default_value.present? ? default_value.transform_keys(&:to_s) : ""
31
+ end
27
32
  end
28
33
  end
29
34
  end
@@ -21,6 +21,7 @@ export default class PbDropdown extends PbEnhancedElement {
21
21
 
22
22
  connect() {
23
23
  this.keyboardHandler = new PbDropdownKeyboard(this);
24
+ this.setDefaultValue();
24
25
  this.bindEventListeners();
25
26
  this.updateArrowDisplay(false);
26
27
  this.handleFormValidation();
@@ -179,4 +180,28 @@ export default class PbDropdown extends PbEnhancedElement {
179
180
  }
180
181
  }
181
182
  }
183
+
184
+ setDefaultValue() {
185
+ const hiddenInput = this.element.querySelector("#dropdown-selected-option");
186
+
187
+ if (hiddenInput.value) {
188
+ const inputFormValidation = this.element.querySelector(INPUT_FORM_VALIDATION);
189
+ const defaultValue = JSON.parse(hiddenInput.value.replaceAll("=>", ":"));
190
+ const options = this.element.querySelectorAll(OPTION_SELECTOR);
191
+
192
+ options.forEach((option) => {
193
+ if (defaultValue.id === JSON.parse(option.dataset.dropdownOptionLabel).id) {
194
+ option.classList.add("pb_dropdown_option_selected");
195
+ }
196
+ });
197
+
198
+ const triggerElement = this.element.querySelector("#dropdown_trigger_display");
199
+ if (triggerElement) {
200
+ triggerElement.textContent = defaultValue.label;
201
+ }
202
+
203
+ hiddenInput.value = defaultValue.id;
204
+ inputFormValidation.value = defaultValue.id;
205
+ }
206
+ }
182
207
  }