@crossed/ui
Version:
A universal & performant styling library for React Native, Next.js & React
167 lines (166 loc) • 7.2 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Content_exports = {};
__export(Content_exports, {
SelectContent: () => SelectContent
});
module.exports = __toCommonJS(Content_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_Floating = require("../../overlay/Floating");
var import_context = require("./context");
var import_styled = require("@crossed/styled");
var import_react_native_reanimated = require("react-native-reanimated");
var import_display = require("../../display");
var import_form = require("../../styles/form");
var import_Input = require("../Input");
var import_fuse = __toESM(require("fuse.js"));
var import_styles = require("./styles");
var import_Focus = require("./Focus");
var import_context2 = require("../../overlay/Floating/context");
var import_Sheet = require("../../overlay/Sheet");
var import_Spinner = require("../../display/Spinner");
var import_styles2 = require("../../styles");
var import_List = require("./List");
const duration = 100;
const styles = (0, import_styled.createStyles)(() => ({ dynamic: (e) => e }));
const SelectContent = (0, import_react.memo)(
(0, import_react.forwardRef)(({ floatingStyles, onSearch, loading }, ref) => {
(0, import_styled.useTheme)();
const { onClose, open } = (0, import_context2.useFloatingContext)();
const selectValue = (0, import_context.useSelectValue)();
const { items } = selectValue;
const configContext = (0, import_context.useSelectConfig)();
const { searchable, showSheet } = configContext;
const [search, setSearch] = (0, import_react.useState)("");
const handleChangeSearch = (0, import_react.useCallback)(
(value) => {
setSearch(value);
if (onSearch)
onSearch(value);
},
[onSearch, setSearch]
);
const fuse = (0, import_react.useMemo)(
() => new import_fuse.default(items, {
keys: ["search", "value"]
}),
[items]
);
const data = (0, import_react.useMemo)(() => {
return search && !onSearch ? fuse.search(search).map(({ item }) => item) : items;
}, [fuse, search, items, onSearch]);
(0, import_react.useEffect)(() => {
var _a, _b;
if (!open) {
(_a = totoRef.current) == null ? void 0 : _a.hide();
setSearch("");
} else {
(_b = totoRef.current) == null ? void 0 : _b.show();
}
}, [open]);
const renderSearch = searchable ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_Input.Input,
{
formFieldStyle: (0, import_styled.inlineStyle)(({ space }) => ({
base: {
flexGrow: 0,
marginTop: space.xxs,
marginHorizontal: space.xs
}
})),
value: search,
onChangeText: handleChangeSearch,
clearable: true,
autoFocus: true
}
) : null;
const totoRef = (0, import_react.useRef)();
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: showSheet ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_Sheet.Sheet.Content,
{
ref: totoRef,
onClose,
containerStyle: (0, import_styled.composeStyles)(
searchable && (0, import_styled.inlineStyle)(() => ({
base: { height: "100%" }
}))
),
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_Sheet.Sheet.Padded, { testID: "content-select", children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_display.MenuList.Item, { children: renderSearch }),
loading ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Spinner.Spinner, {}) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_List.List, { data })
] })
}
) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Floating.Floating.Portal, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_Focus.Focus,
{
onEscapeKey: onClose,
onClickOutside: onClose,
enabled: open,
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_Floating.Floating.Content,
{
exiting: import_react_native_reanimated.FadeOut.duration(duration),
entering: import_react_native_reanimated.FadeIn.duration(duration),
style: (0, import_styled.composeStyles)(
(0, import_styled.inlineStyle)(({ boxShadow }) => ({
base: { zIndex: 100 },
web: { base: { boxShadow } }
}))
),
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_context.SelectConfigProvider, { ...configContext, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_context.SelectValueProvider, { ...selectValue, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
import_display.MenuList,
{
testID: "content-select",
ref,
style: (0, import_styled.composeStyles)(
import_form.form.input,
import_styles.useSelect.content,
import_styles2.gapStyles.xs,
(0, import_styled.inlineStyle)(() => ({
web: { base: { overflowY: "auto" } }
})),
styles.dynamic(floatingStyles)
),
children: [
renderSearch,
loading ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Spinner.Spinner, {}) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_List.List, { data })
]
}
) }) })
}
)
}
) }) });
})
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SelectContent
});
//# sourceMappingURL=Content.js.map