@chayns-components/person-finder
Version:
A set of beautiful React components for developing your own applications with chayns.
173 lines (172 loc) • 6.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _core = require("@chayns-components/core");
var _chaynsApi = require("chayns-api");
var _react = _interopRequireWildcard(require("react"));
var _uuid = require("uuid");
var _personFinder = require("../../../types/personFinder");
var _PersonFinderWrapper = require("./PersonFinderWrapper.styles");
var _PersonFinderBody = _interopRequireDefault(require("./person-finder-body/PersonFinderBody"));
var _PersonFinderProvider = require("../../PersonFinderProvider");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const PersonFinderWrapper = /*#__PURE__*/(0, _react.forwardRef)(({
container,
dropdownDirection,
filterTypes,
leftElement: leftElementProp,
maxEntries = Infinity,
onAdd,
onDropdownHide,
onDropdownShow,
friendsPriority,
onRemove,
placeholder,
shouldAllowMultiple,
shouldHideResultsOnAdd
}, ref) => {
const {
data,
updateSearch,
setTags,
tags,
search
} = (0, _PersonFinderProvider.usePersonFinder)();
const [isFocused, setIsFocused] = (0, _react.useState)(false);
const [shouldShowBody, setShouldShowBody] = (0, _react.useState)(false);
const boxRef = (0, _react.useRef)(null);
const contentRef = (0, _react.useRef)(null);
const keyRef = (0, _react.useRef)(`person-finder-${(0, _uuid.v4)()}`);
const tagInputRef = (0, _react.useRef)(null);
const {
isTouch
} = (0, _chaynsApi.useDevice)();
const leftElement = leftElementProp ?? /*#__PURE__*/_react.default.createElement(_PersonFinderWrapper.StyledPersonFinderLeftElement, null, /*#__PURE__*/_react.default.createElement(_core.Icon, {
icons: ['fa fa-search']
}));
const handleTagInputBlur = (0, _react.useCallback)(() => {
setIsFocused(false);
}, []);
const handleTagInputFocus = (0, _react.useCallback)(() => {
setIsFocused(true);
}, []);
const handleRemove = (0, _react.useCallback)(id => {
if (typeof setTags !== 'function') {
return;
}
setTags(prevState => prevState.filter(entry => entry.id !== id));
if (typeof onRemove === 'function') {
onRemove(id);
}
}, [onRemove, setTags]);
const handleClose = (0, _react.useCallback)(() => {
setShouldShowBody(false);
}, []);
const handleOpen = (0, _react.useCallback)(() => {
setShouldShowBody(true);
}, []);
const handleClear = (0, _react.useCallback)(() => {
var _tagInputRef$current;
if (typeof setTags !== 'function') {
return;
}
(_tagInputRef$current = tagInputRef.current) === null || _tagInputRef$current === void 0 || _tagInputRef$current.resetValue();
setTags([]);
}, [setTags]);
const handleDropdownOutsideClick = (0, _react.useCallback)(() => {
var _tagInputRef$current2;
(_tagInputRef$current2 = tagInputRef.current) === null || _tagInputRef$current2 === void 0 || _tagInputRef$current2.blur();
return isFocused && isTouch;
}, [isFocused, isTouch]);
const handleAdd = (0, _react.useCallback)(id => {
if (typeof setTags !== 'function') {
return;
}
const selectedEntry = Object.values(data ?? {}).flat().map(({
entries
}) => entries).flat().find(entry => (typeof entry.id === 'string' ? entry.id : entry.id.toString()) === id);
if (!selectedEntry) {
return;
}
const {
name,
firstName,
lastName
} = selectedEntry;
const tag = {
id,
text: name ?? `${firstName ?? ''} ${lastName ?? ''}`
};
setTags(prevState => {
var _tagInputRef$current3;
if (prevState.some(t => t.id === id) || prevState.length >= maxEntries) {
return prevState;
}
(_tagInputRef$current3 = tagInputRef.current) === null || _tagInputRef$current3 === void 0 || _tagInputRef$current3.resetValue();
if (typeof onAdd === 'function') {
onAdd(selectedEntry);
}
if (shouldHideResultsOnAdd) {
setShouldShowBody(false);
}
return [...prevState, tag];
});
}, [data, maxEntries, onAdd, setTags, shouldHideResultsOnAdd]);
const handleChange = (0, _react.useCallback)(event => {
if (typeof updateSearch === 'function') {
updateSearch(event.target.value);
}
}, [updateSearch]);
(0, _react.useImperativeHandle)(ref, () => ({
clear: () => handleClear()
}), [handleClear]);
(0, _react.useEffect)(() => {
if (shouldShowBody) {
if (typeof onDropdownShow === 'function') {
onDropdownShow();
}
} else if (typeof onDropdownHide === 'function') {
onDropdownHide();
}
}, [onDropdownHide, onDropdownShow, shouldShowBody]);
const showBody = (0, _react.useMemo)(() => {
if ((filterTypes === null || filterTypes === void 0 ? void 0 : filterTypes.length) === 1 && (filterTypes.includes(_personFinder.PersonFinderFilterTypes.SITE) || filterTypes.includes(_personFinder.PersonFinderFilterTypes.PERSON) && friendsPriority !== _personFinder.Priority.HIGH)) {
return shouldShowBody && !!search && (search === null || search === void 0 ? void 0 : search.length) > 2;
}
return shouldShowBody;
}, [filterTypes, friendsPriority, search, shouldShowBody]);
return /*#__PURE__*/_react.default.createElement(_PersonFinderWrapper.StyledPersonFinder, {
ref: boxRef,
onFocus: handleOpen,
key: keyRef.current
}, /*#__PURE__*/_react.default.createElement(_core.TagInput, {
leftElement: leftElement,
onBlur: handleTagInputBlur,
onChange: handleChange,
onFocus: handleTagInputFocus,
onRemove: handleRemove,
placeholder: placeholder,
ref: tagInputRef,
shouldAllowMultiple: shouldAllowMultiple,
shouldPreventEnter: true,
tags: tags
}), boxRef.current && /*#__PURE__*/_react.default.createElement(_core.DropdownBodyWrapper, {
anchorElement: boxRef.current,
container: container,
direction: dropdownDirection,
onClose: handleClose,
onOutsideClick: handleDropdownOutsideClick,
shouldShowDropdown: showBody
}, /*#__PURE__*/_react.default.createElement(_PersonFinderBody.default, {
filterTypes: filterTypes,
onAdd: handleAdd,
onRemove: handleRemove,
ref: contentRef
})));
});
PersonFinderWrapper.displayName = 'PersonFinderWrapper';
var _default = exports.default = PersonFinderWrapper;
//# sourceMappingURL=PersonFinderWrapper.js.map