@homebound/beam 2.135.2 → 2.136.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.
@@ -29,7 +29,7 @@ function Filters(props) {
29
29
  return [Object.fromEntries(impls), {}];
30
30
  }, [numberOfPageFilters, filterDefs]);
31
31
  const numModalFilters = (0, utils_1.safeKeys)(modalFilters).filter((fk) => filter[fk] !== undefined).length;
32
- const maybeGroupByField = groupBy ? ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(SelectField_1.SelectField, { label: "Group by", compact: !vertical, inlineLabel: !vertical, sizeToContent: !vertical, options: groupBy.options, getOptionValue: (o) => o.id, getOptionLabel: (o) => o.name, value: groupBy.value, onSelect: (g) => groupBy.setValue(g) }, void 0) }, void 0)) : null;
32
+ const maybeGroupByField = groupBy ? ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(SelectField_1.SelectField, { label: "Group by", compact: !vertical, inlineLabel: !vertical, sizeToContent: !vertical, options: groupBy.options, getOptionValue: (o) => o.id, getOptionLabel: (o) => o.name, value: groupBy.value, onSelect: (g) => g && groupBy.setValue(g) }, void 0) }, void 0)) : null;
33
33
  // Return list of filter components. `onSelect` should update the `filter`
34
34
  return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ css: {
35
35
  ...(vertical ? Css_1.Css.df.fdc.childGap2.$ : Css_1.Css.df.aic.childGap1.$),
@@ -2,7 +2,7 @@ import { ReactNode } from "react";
2
2
  import { Value } from "./";
3
3
  import { BeamSelectFieldBaseProps } from "./internal/SelectFieldBase";
4
4
  import { HasIdAndName, Optional } from "../types";
5
- export interface MultiSelectFieldProps<O, V extends Value> extends BeamSelectFieldBaseProps<O, V> {
5
+ export interface MultiSelectFieldProps<O, V extends Value> extends Exclude<BeamSelectFieldBaseProps<O, V>, "unsetLabel"> {
6
6
  /** Renders `opt` in the dropdown menu, defaults to the `getOptionLabel` prop. */
7
7
  getOptionMenuLabel?: (opt: O) => string | ReactNode;
8
8
  getOptionValue: (opt: O) => V;
@@ -4,7 +4,7 @@ import { HasIdAndName, Optional } from "../types";
4
4
  export interface SelectFieldProps<O, V extends Value> extends Omit<BeamSelectFieldBaseProps<O, V>, "values" | "onSelect"> {
5
5
  /** The current value; it can be `undefined`, even if `V` cannot be. */
6
6
  value: V | undefined;
7
- onSelect: (value: V, opt: O) => void;
7
+ onSelect: (value: V | undefined, opt: O | undefined) => void;
8
8
  }
9
9
  /**
10
10
  * Provides a non-native select/dropdown widget.
@@ -9,7 +9,8 @@ function SelectField(props) {
9
9
  options, onSelect, value, ...otherProps } = props;
10
10
  return ((0, jsx_runtime_1.jsx)(SelectFieldBase_1.SelectFieldBase, Object.assign({}, otherProps, { options: options, getOptionLabel: getOptionLabel, getOptionValue: getOptionValue, values: [value], onSelect: (values, options) => {
11
11
  if (values.length > 0 && options.length > 0) {
12
- onSelect && onSelect(values[0], options[0]);
12
+ const option = options[0];
13
+ onSelect(values[0], option === SelectFieldBase_1.unsetOption ? undefined : option);
13
14
  }
14
15
  } }), void 0));
15
16
  }
@@ -3,8 +3,8 @@ import { PresentationFieldProps } from "../../components/PresentationContext";
3
3
  import { Value } from "../Value";
4
4
  import { BeamFocusableProps } from "../../interfaces";
5
5
  export interface BeamSelectFieldBaseProps<O, V extends Value> extends BeamFocusableProps, PresentationFieldProps {
6
- /** Renders `opt` in the dropdown menu, defaults to the `getOptionLabel` prop. */
7
- getOptionMenuLabel?: (opt: O) => string | ReactNode;
6
+ /** Renders `opt` in the dropdown menu, defaults to the `getOptionLabel` prop. `isUnsetOpt` is only defined for single SelectField */
7
+ getOptionMenuLabel?: (opt: O, isUnsetOpt?: boolean) => string | ReactNode;
8
8
  getOptionValue: (opt: O) => V;
9
9
  getOptionLabel: (opt: O) => string;
10
10
  /** The current value; it can be `undefined`, even if `V` cannot be. */
@@ -12,12 +12,7 @@ export interface BeamSelectFieldBaseProps<O, V extends Value> extends BeamFocusa
12
12
  onSelect: (values: V[], opts: O[]) => void;
13
13
  multiselect?: boolean;
14
14
  disabledOptions?: V[];
15
- options: O[] | {
16
- initial: O[];
17
- load: () => Promise<{
18
- options: O[];
19
- }>;
20
- };
15
+ options: OptionsOrLoad<O>;
21
16
  /** Whether the field is disabled. If a ReactNode, it's treated as a "disabled reason" that's shown in a tooltip. */
22
17
  disabled?: boolean | ReactNode;
23
18
  required?: boolean;
@@ -39,6 +34,8 @@ export interface BeamSelectFieldBaseProps<O, V extends Value> extends BeamFocusa
39
34
  contrast?: boolean;
40
35
  /** Placeholder content */
41
36
  placeholder?: string;
37
+ /** Only used for Single Select Fields. If set, prepends an option with a `undefined` value at the top of the list */
38
+ unsetLabel?: string;
42
39
  }
43
40
  /**
44
41
  * Provides a non-native select/dropdown widget.
@@ -50,3 +47,12 @@ export interface BeamSelectFieldBaseProps<O, V extends Value> extends BeamFocusa
50
47
  * and so we cannot easily change them.
51
48
  */
52
49
  export declare function SelectFieldBase<O, V extends Value>(props: BeamSelectFieldBaseProps<O, V>): JSX.Element;
50
+ declare type OptionsOrLoad<O> = O[] | {
51
+ initial: O[];
52
+ load: () => Promise<{
53
+ options: O[];
54
+ }>;
55
+ };
56
+ export declare function initializeOptions<O>(options: OptionsOrLoad<O>, unsetLabel: string | undefined): OptionsOrLoad<O>;
57
+ export declare const unsetOption: {};
58
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SelectFieldBase = void 0;
3
+ exports.unsetOption = exports.initializeOptions = exports.SelectFieldBase = void 0;
4
4
  const jsx_runtime_1 = require("@emotion/react/jsx-runtime");
5
5
  const react_1 = require("react");
6
6
  const react_aria_1 = require("react-aria");
@@ -23,7 +23,15 @@ const utils_1 = require("../../utils");
23
23
  */
24
24
  function SelectFieldBase(props) {
25
25
  var _a;
26
- const { disabled, readOnly, onSelect, options: maybeOptions, multiselect = false, getOptionLabel, getOptionValue, getOptionMenuLabel = getOptionLabel, values = [], nothingSelectedText = "", contrast, disabledOptions, borderless, ...otherProps } = props;
26
+ const { disabled, readOnly, onSelect, options, multiselect = false, values = [], nothingSelectedText = "", contrast, disabledOptions, borderless, unsetLabel, ...otherProps } = props;
27
+ // Call `initializeOptions` to prepend the `unset` option if the `unsetLabel` was provided.
28
+ const maybeOptions = (0, react_1.useMemo)(() => initializeOptions(options, unsetLabel), [options, unsetLabel]);
29
+ // Memoize the callback functions and handle the `unset` option if provided.
30
+ const getOptionLabel = (0, react_1.useCallback)((o) => (unsetLabel && o === exports.unsetOption ? unsetLabel : props.getOptionLabel(o)), [props.getOptionLabel, unsetLabel]);
31
+ const getOptionValue = (0, react_1.useCallback)((o) => (unsetLabel && o === exports.unsetOption ? undefined : props.getOptionValue(o)), [props.getOptionValue, unsetLabel]);
32
+ const getOptionMenuLabel = (0, react_1.useCallback)((o) => props.getOptionMenuLabel
33
+ ? props.getOptionMenuLabel(o, Boolean(unsetLabel) && o === exports.unsetOption)
34
+ : getOptionLabel(o), [props.getOptionValue, unsetLabel, getOptionLabel]);
27
35
  const { contains } = (0, react_aria_1.useFilter)({ sensitivity: "base" });
28
36
  const isDisabled = !!disabled;
29
37
  const isReadOnly = !!readOnly;
@@ -105,7 +113,9 @@ function SelectFieldBase(props) {
105
113
  async function maybeInitLoad() {
106
114
  if (!Array.isArray(maybeOptions)) {
107
115
  setFieldState((prevState) => ({ ...prevState, optionsLoading: true }));
108
- const { options } = await maybeOptions.load();
116
+ const loadedOptions = (await maybeOptions.load()).options;
117
+ // Ensure the `unset` option is prepended to the top of the list if `unsetLabel` was provided
118
+ const options = !unsetLabel ? loadedOptions : getOptionsWithUnset(unsetLabel, loadedOptions);
109
119
  setFieldState((prevState) => ({
110
120
  ...prevState,
111
121
  filteredOptions: options,
@@ -245,3 +255,17 @@ function getInputValue(selectedOptions, getOptionLabel, multiselect, nothingSele
245
255
  ? nothingSelectedText
246
256
  : "";
247
257
  }
258
+ function initializeOptions(options, unsetLabel) {
259
+ if (!unsetLabel) {
260
+ return options;
261
+ }
262
+ if (Array.isArray(options)) {
263
+ return getOptionsWithUnset(unsetLabel, options);
264
+ }
265
+ return { ...options, initial: getOptionsWithUnset(unsetLabel, options.initial) };
266
+ }
267
+ exports.initializeOptions = initializeOptions;
268
+ function getOptionsWithUnset(unsetLabel, options) {
269
+ return [exports.unsetOption, ...options];
270
+ }
271
+ exports.unsetOption = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/beam",
3
- "version": "2.135.2",
3
+ "version": "2.136.0",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",