@homebound/beam 2.244.4 → 2.246.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.
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Evaluates a computed function `fn` to a regular value and triggers a re-render whenever it changes. */
|
|
2
|
-
export declare function useComputed<T>(fn: () => T, deps: readonly any[]): T;
|
|
2
|
+
export declare function useComputed<T>(fn: (prev: T | undefined) => T, deps: readonly any[]): T;
|
|
@@ -29,9 +29,9 @@ function useComputed(fn, deps) {
|
|
|
29
29
|
// If deps has changed, we're already re-running, so don't trigger a 2nd one
|
|
30
30
|
current.hasRan = false;
|
|
31
31
|
current.runner = (0, mobx_1.autorun)(() => {
|
|
32
|
-
// Always eval fn() (even on 1st render) to register our observable.
|
|
33
|
-
const newValue = fn();
|
|
34
32
|
const { value: oldValue, hasRan: oldHasRun } = current;
|
|
33
|
+
// Always eval fn() (even on 1st render) to register our observable.
|
|
34
|
+
const newValue = fn(oldValue);
|
|
35
35
|
current.value = newValue;
|
|
36
36
|
current.hasRan = true;
|
|
37
37
|
// Only trigger a re-render if this is not the 1st autorun. Note
|
|
@@ -51,7 +51,7 @@ function useComputed(fn, deps) {
|
|
|
51
51
|
// accept running the eval fn twice (here to get the value for the 1st render,
|
|
52
52
|
// and again for mobx to watch what observables we touch).
|
|
53
53
|
if (!ref.current.hasRan) {
|
|
54
|
-
ref.current.value = fn();
|
|
54
|
+
ref.current.value = fn(undefined);
|
|
55
55
|
}
|
|
56
56
|
// We can use `!` here b/c we know that `autorun` set current
|
|
57
57
|
return ref.current.value;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import { PresentationFieldProps } from "../components/PresentationContext";
|
|
3
|
+
import { TextFieldBaseProps } from "./TextFieldBase";
|
|
4
|
+
interface AutocompleteProps<T> extends Pick<PresentationFieldProps, "labelStyle">, Pick<TextFieldBaseProps<any>, "label" | "clearable" | "startAdornment"> {
|
|
5
|
+
onSelect: (item: T) => void;
|
|
6
|
+
/** A function that returns how to render the an option in the menu. If not set, `getOptionLabel` will be used */
|
|
7
|
+
getOptionMenuLabel?: (o: T) => ReactNode;
|
|
8
|
+
/** A function that returns the string value of the option. Used for accessibility purposes */
|
|
9
|
+
getOptionLabel: (o: T) => string;
|
|
10
|
+
/** A function that returns a unique key for an option */
|
|
11
|
+
getOptionValue: (o: T) => string;
|
|
12
|
+
/** Called when the input value changes */
|
|
13
|
+
onInputChange: (value: string | undefined) => void;
|
|
14
|
+
/** The current value of the input */
|
|
15
|
+
value: string | undefined;
|
|
16
|
+
/** The list of options to choose from */
|
|
17
|
+
options: T[];
|
|
18
|
+
/** The placeholder text to show when the input is empty */
|
|
19
|
+
placeholder?: string;
|
|
20
|
+
/** Whether the input is disabled */
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare function Autocomplete<T extends object>(props: AutocompleteProps<T>): import("@emotion/react/jsx-runtime").JSX.Element;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Autocomplete = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("@emotion/react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const react_aria_1 = require("react-aria");
|
|
7
|
+
const react_stately_1 = require("react-stately");
|
|
8
|
+
const components_1 = require("../components");
|
|
9
|
+
const internal_1 = require("../components/internal");
|
|
10
|
+
const ListBox_1 = require("./internal/ListBox");
|
|
11
|
+
const TextFieldBase_1 = require("./TextFieldBase");
|
|
12
|
+
const Value_1 = require("./Value");
|
|
13
|
+
function Autocomplete(props) {
|
|
14
|
+
var _a;
|
|
15
|
+
const { onSelect, getOptionLabel, getOptionValue, getOptionMenuLabel, onInputChange, value, options, disabled, ...others } = props;
|
|
16
|
+
const comboBoxProps = {
|
|
17
|
+
isDisabled: !!disabled,
|
|
18
|
+
onInputChange: onInputChange,
|
|
19
|
+
inputValue: value,
|
|
20
|
+
items: options,
|
|
21
|
+
// Allow the user to type in a value that is not in the list. Allows for the text to stay in the input when the user clicks away
|
|
22
|
+
allowsCustomValue: true,
|
|
23
|
+
children: (item) => ((0, jsx_runtime_1.jsx)(react_stately_1.Item, { textValue: getOptionValue(item), children: getOptionMenuLabel ? getOptionMenuLabel(item) : getOptionLabel(item) }, getOptionValue(item))),
|
|
24
|
+
onSelectionChange: (key) => {
|
|
25
|
+
const selectedItem = options.find((i) => getOptionValue(i) === key);
|
|
26
|
+
if (selectedItem) {
|
|
27
|
+
onInputChange(getOptionLabel(selectedItem));
|
|
28
|
+
onSelect(selectedItem);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
...others,
|
|
32
|
+
};
|
|
33
|
+
const state = (0, react_stately_1.useComboBoxState)(comboBoxProps);
|
|
34
|
+
const inputWrapRef = (0, react_1.useRef)(null);
|
|
35
|
+
const inputRef = (0, react_1.useRef)(null);
|
|
36
|
+
const listBoxRef = (0, react_1.useRef)(null);
|
|
37
|
+
const popoverRef = (0, react_1.useRef)(null);
|
|
38
|
+
const { inputProps, listBoxProps, labelProps } = (0, react_aria_1.useComboBox)({
|
|
39
|
+
...comboBoxProps,
|
|
40
|
+
inputRef,
|
|
41
|
+
listBoxRef,
|
|
42
|
+
popoverRef,
|
|
43
|
+
// When the input is focused and there are options, open the menu
|
|
44
|
+
onFocus: () => options.length > 0 && state.open(),
|
|
45
|
+
}, state);
|
|
46
|
+
// useOverlayPosition moves the overlay to the top of the DOM to avoid any z-index issues. Uses the `targetRef` to DOM placement
|
|
47
|
+
const { overlayProps: positionProps } = (0, react_aria_1.useOverlayPosition)({
|
|
48
|
+
targetRef: inputWrapRef,
|
|
49
|
+
overlayRef: popoverRef,
|
|
50
|
+
scrollRef: listBoxRef,
|
|
51
|
+
isOpen: state.isOpen,
|
|
52
|
+
onClose: state.close,
|
|
53
|
+
placement: "bottom left",
|
|
54
|
+
});
|
|
55
|
+
positionProps.style = {
|
|
56
|
+
...positionProps.style,
|
|
57
|
+
width: (_a = inputWrapRef === null || inputWrapRef === void 0 ? void 0 : inputWrapRef.current) === null || _a === void 0 ? void 0 : _a.clientWidth,
|
|
58
|
+
// Ensures the menu never gets too small.
|
|
59
|
+
minWidth: 200,
|
|
60
|
+
};
|
|
61
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(TextFieldBase_1.TextFieldBase, { inputRef: inputRef, inputWrapRef: inputWrapRef, inputProps: inputProps, labelProps: labelProps, onChange: onInputChange, clearable: true,
|
|
62
|
+
// Respect if caller to passes in `startAdornment={undefined}`
|
|
63
|
+
startAdornment: "startAdornment" in props ? props.startAdornment : (0, jsx_runtime_1.jsx)(components_1.Icon, { icon: "search" }), ...others }), state.isOpen && ((0, jsx_runtime_1.jsx)(internal_1.Popover, { triggerRef: inputRef, popoverRef: popoverRef, positionProps: positionProps, onClose: () => state.close(), isOpen: state.isOpen, minWidth: 200, children: (0, jsx_runtime_1.jsx)(ListBox_1.ListBox, { ...listBoxProps, positionProps: positionProps, state: state, listBoxRef: listBoxRef, getOptionValue: (o) => (0, Value_1.valueToKey)(getOptionValue(o)), getOptionLabel: getOptionLabel }) }))] }));
|
|
64
|
+
}
|
|
65
|
+
exports.Autocomplete = Autocomplete;
|
package/dist/inputs/index.d.ts
CHANGED
package/dist/inputs/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.RadioGroupField = void 0;
|
|
18
|
+
__exportStar(require("./Autocomplete"), exports);
|
|
18
19
|
__exportStar(require("./Checkbox"), exports);
|
|
19
20
|
__exportStar(require("./CheckboxGroup"), exports);
|
|
20
21
|
__exportStar(require("./ChipSelectField"), exports);
|