@khanacademy/wonder-blocks-dropdown 5.7.0 → 5.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @khanacademy/wonder-blocks-dropdown
2
2
 
3
+ ## 5.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 2b8424ca: Allow use of JSX Element as label in SingleSelect and MultiSelect
8
+
3
9
  ## 5.7.0
4
10
 
5
11
  ### Minor Changes
@@ -66,6 +66,13 @@ type DefaultProps = Readonly<{
66
66
  * Whether to display shortcuts for Select All and Select None.
67
67
  */
68
68
  shortcuts: boolean;
69
+ /**
70
+ * When false, the SelectOpener can show a Node as a label. When true, the
71
+ * SelectOpener will use a string as a label. If using custom OptionItems, a
72
+ * plain text label can be provided with the `labelAsText` prop.
73
+ * Defaults to true.
74
+ */
75
+ showOpenerLabelAsText: boolean;
69
76
  }>;
70
77
  type Props = AriaProps & DefaultProps & Readonly<{
71
78
  /**
@@ -196,7 +203,7 @@ export default class MultiSelect extends React.Component<Props, State> {
196
203
  handleToggle: (selectedValue: string) => void;
197
204
  handleSelectAll: () => void;
198
205
  handleSelectNone: () => void;
199
- getMenuText(children: OptionItemComponentArray): string;
206
+ getMenuText(children: OptionItemComponentArray): string | JSX.Element;
200
207
  getShortcuts(numOptions: number): DropdownItem[];
201
208
  getMenuItems(children: OptionItemComponentArray): DropdownItem[];
202
209
  mapOptionItemToDropdownItem: (option: OptionItemComponent) => DropdownItem;
@@ -64,6 +64,13 @@ type DefaultProps = Readonly<{
64
64
  * The object containing the custom labels used inside this component.
65
65
  */
66
66
  labels: SingleSelectLabels;
67
+ /**
68
+ * When false, the SelectOpener can show a Node as a label. When true, the
69
+ * SelectOpener will use a string as a label. If using custom OptionItems, a
70
+ * plain text label can be provided with the `labelAsText` prop.
71
+ * Defaults to true.
72
+ */
73
+ showOpenerLabelAsText: boolean;
67
74
  }>;
68
75
  type Props = AriaProps & DefaultProps & Readonly<{
69
76
  /**
package/dist/es/index.js CHANGED
@@ -778,6 +778,12 @@ function getLabel(props) {
778
778
  }
779
779
  return "";
780
780
  }
781
+ function getSelectOpenerLabel(showOpenerLabelAsText, props) {
782
+ if (showOpenerLabelAsText) {
783
+ return getLabel(props);
784
+ }
785
+ return props.label;
786
+ }
781
787
 
782
788
  const VIRTUALIZE_THRESHOLD = 125;
783
789
  const StyledSpan$1 = addStyle("span");
@@ -1862,7 +1868,7 @@ const _generateStyles = (light, placeholder, error) => {
1862
1868
  return stateStyles[styleKey];
1863
1869
  };
1864
1870
 
1865
- const _excluded$1 = ["children", "error", "id", "light", "opener", "placeholder", "selectedValue", "testId", "alignment", "autoFocus", "dropdownStyle", "enableTypeAhead", "isFilterable", "labels", "onChange", "onToggle", "opened", "style", "className", "aria-invalid", "aria-required"];
1871
+ const _excluded$1 = ["children", "error", "id", "light", "opener", "placeholder", "selectedValue", "testId", "alignment", "autoFocus", "dropdownStyle", "enableTypeAhead", "isFilterable", "labels", "onChange", "onToggle", "opened", "style", "className", "aria-invalid", "aria-required", "showOpenerLabelAsText"];
1866
1872
  class SingleSelect extends React.Component {
1867
1873
  constructor(props) {
1868
1874
  super(props);
@@ -1969,12 +1975,13 @@ class SingleSelect extends React.Component {
1969
1975
  opener,
1970
1976
  placeholder,
1971
1977
  selectedValue,
1972
- testId
1978
+ testId,
1979
+ showOpenerLabelAsText
1973
1980
  } = _this$props,
1974
1981
  sharedProps = _objectWithoutPropertiesLoose(_this$props, _excluded$1);
1975
1982
  const items = React.Children.toArray(children);
1976
1983
  const selectedItem = items.find(option => option.props.value === selectedValue);
1977
- const menuText = selectedItem ? getLabel(selectedItem.props) : placeholder;
1984
+ const menuText = selectedItem ? getSelectOpenerLabel(showOpenerLabelAsText, selectedItem.props) : placeholder;
1978
1985
  const dropdownOpener = React.createElement(IDProvider, {
1979
1986
  id: id,
1980
1987
  scope: "single-select-opener"
@@ -2069,7 +2076,8 @@ SingleSelect.defaultProps = {
2069
2076
  filter: defaultLabels.filter,
2070
2077
  noResults: defaultLabels.noResults,
2071
2078
  someResults: defaultLabels.someSelected
2072
- }
2079
+ },
2080
+ showOpenerLabelAsText: true
2073
2081
  };
2074
2082
 
2075
2083
  const _excluded = ["id", "light", "opener", "testId", "alignment", "dropdownStyle", "implicitAllEnabled", "isFilterable", "labels", "onChange", "onToggle", "opened", "selectedValues", "shortcuts", "style", "className", "aria-invalid", "aria-required"];
@@ -2170,7 +2178,8 @@ class MultiSelect extends React.Component {
2170
2178
  getMenuText(children) {
2171
2179
  const {
2172
2180
  implicitAllEnabled,
2173
- selectedValues
2181
+ selectedValues,
2182
+ showOpenerLabelAsText
2174
2183
  } = this.props;
2175
2184
  const {
2176
2185
  noneSelected,
@@ -2185,7 +2194,7 @@ class MultiSelect extends React.Component {
2185
2194
  case 1:
2186
2195
  const selectedItem = children.find(option => option.props.value === selectedValues[0]);
2187
2196
  if (selectedItem) {
2188
- const selectedLabel = getLabel(selectedItem == null ? void 0 : selectedItem.props);
2197
+ const selectedLabel = getSelectOpenerLabel(showOpenerLabelAsText, selectedItem == null ? void 0 : selectedItem.props);
2189
2198
  if (selectedLabel) {
2190
2199
  return selectedLabel;
2191
2200
  } else {
@@ -2385,7 +2394,8 @@ MultiSelect.defaultProps = {
2385
2394
  error: false,
2386
2395
  light: false,
2387
2396
  shortcuts: false,
2388
- selectedValues: []
2397
+ selectedValues: [],
2398
+ showOpenerLabelAsText: true
2389
2399
  };
2390
2400
 
2391
2401
  function updateMultipleSelection(previousSelection, value = "") {
package/dist/index.js CHANGED
@@ -814,6 +814,12 @@ function getLabel(props) {
814
814
  }
815
815
  return "";
816
816
  }
817
+ function getSelectOpenerLabel(showOpenerLabelAsText, props) {
818
+ if (showOpenerLabelAsText) {
819
+ return getLabel(props);
820
+ }
821
+ return props.label;
822
+ }
817
823
 
818
824
  const VIRTUALIZE_THRESHOLD = 125;
819
825
  const StyledSpan$1 = wonderBlocksCore.addStyle("span");
@@ -1898,7 +1904,7 @@ const _generateStyles = (light, placeholder, error) => {
1898
1904
  return stateStyles[styleKey];
1899
1905
  };
1900
1906
 
1901
- const _excluded$1 = ["children", "error", "id", "light", "opener", "placeholder", "selectedValue", "testId", "alignment", "autoFocus", "dropdownStyle", "enableTypeAhead", "isFilterable", "labels", "onChange", "onToggle", "opened", "style", "className", "aria-invalid", "aria-required"];
1907
+ const _excluded$1 = ["children", "error", "id", "light", "opener", "placeholder", "selectedValue", "testId", "alignment", "autoFocus", "dropdownStyle", "enableTypeAhead", "isFilterable", "labels", "onChange", "onToggle", "opened", "style", "className", "aria-invalid", "aria-required", "showOpenerLabelAsText"];
1902
1908
  class SingleSelect extends React__namespace.Component {
1903
1909
  constructor(props) {
1904
1910
  super(props);
@@ -2005,12 +2011,13 @@ class SingleSelect extends React__namespace.Component {
2005
2011
  opener,
2006
2012
  placeholder,
2007
2013
  selectedValue,
2008
- testId
2014
+ testId,
2015
+ showOpenerLabelAsText
2009
2016
  } = _this$props,
2010
2017
  sharedProps = _objectWithoutPropertiesLoose__default["default"](_this$props, _excluded$1);
2011
2018
  const items = React__namespace.Children.toArray(children);
2012
2019
  const selectedItem = items.find(option => option.props.value === selectedValue);
2013
- const menuText = selectedItem ? getLabel(selectedItem.props) : placeholder;
2020
+ const menuText = selectedItem ? getSelectOpenerLabel(showOpenerLabelAsText, selectedItem.props) : placeholder;
2014
2021
  const dropdownOpener = React__namespace.createElement(wonderBlocksCore.IDProvider, {
2015
2022
  id: id,
2016
2023
  scope: "single-select-opener"
@@ -2105,7 +2112,8 @@ SingleSelect.defaultProps = {
2105
2112
  filter: defaultLabels.filter,
2106
2113
  noResults: defaultLabels.noResults,
2107
2114
  someResults: defaultLabels.someSelected
2108
- }
2115
+ },
2116
+ showOpenerLabelAsText: true
2109
2117
  };
2110
2118
 
2111
2119
  const _excluded = ["id", "light", "opener", "testId", "alignment", "dropdownStyle", "implicitAllEnabled", "isFilterable", "labels", "onChange", "onToggle", "opened", "selectedValues", "shortcuts", "style", "className", "aria-invalid", "aria-required"];
@@ -2206,7 +2214,8 @@ class MultiSelect extends React__namespace.Component {
2206
2214
  getMenuText(children) {
2207
2215
  const {
2208
2216
  implicitAllEnabled,
2209
- selectedValues
2217
+ selectedValues,
2218
+ showOpenerLabelAsText
2210
2219
  } = this.props;
2211
2220
  const {
2212
2221
  noneSelected,
@@ -2221,7 +2230,7 @@ class MultiSelect extends React__namespace.Component {
2221
2230
  case 1:
2222
2231
  const selectedItem = children.find(option => option.props.value === selectedValues[0]);
2223
2232
  if (selectedItem) {
2224
- const selectedLabel = getLabel(selectedItem == null ? void 0 : selectedItem.props);
2233
+ const selectedLabel = getSelectOpenerLabel(showOpenerLabelAsText, selectedItem == null ? void 0 : selectedItem.props);
2225
2234
  if (selectedLabel) {
2226
2235
  return selectedLabel;
2227
2236
  } else {
@@ -2421,7 +2430,8 @@ MultiSelect.defaultProps = {
2421
2430
  error: false,
2422
2431
  light: false,
2423
2432
  shortcuts: false,
2424
- selectedValues: []
2433
+ selectedValues: [],
2434
+ showOpenerLabelAsText: true
2425
2435
  };
2426
2436
 
2427
2437
  function updateMultipleSelection(previousSelection, value = "") {
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { PropsFor } from "@khanacademy/wonder-blocks-core";
2
3
  import OptionItem from "../components/option-item";
3
4
  /**
@@ -21,4 +22,9 @@ type OptionItemProps = PropsFor<typeof OptionItem>;
21
22
  * Returns a valid label for the given props.
22
23
  */
23
24
  export declare function getLabel(props: OptionItemProps): string;
25
+ /**
26
+ * Returns the label for the SelectOpener in the SingleSelect and MultiSelect.
27
+ * If the label is a Node, and `labelAsText` is undefined, returns the label.
28
+ */
29
+ export declare function getSelectOpenerLabel(showOpenerLabelAsText: boolean, props: OptionItemProps): string | JSX.Element;
24
30
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khanacademy/wonder-blocks-dropdown",
3
- "version": "5.7.0",
3
+ "version": "5.8.0",
4
4
  "design": "v1",
5
5
  "description": "Dropdown variants for Wonder Blocks.",
6
6
  "main": "dist/index.js",