@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
120 lines (119 loc) • 4.35 kB
JavaScript
"use strict";
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useVisibility;
var _react = require("react");
var _index = _interopRequireDefault(require("../../utils/json-pointer/index.js"));
var _Context = _interopRequireDefault(require("../../DataContext/Context.js"));
var _usePath = _interopRequireDefault(require("../../hooks/usePath.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function useVisibility(props) {
const {
hasFieldError,
filterDataHandler,
mountedFieldsRef,
data: originalData
} = (0, _react.useContext)(_Context.default);
const {
makePath,
makeIteratePath
} = (0, _usePath.default)();
const propsRef = (0, _react.useRef)(props);
propsRef.current = props;
const {
withinIterate
} = props || {};
const makeLocalPath = (0, _react.useCallback)(path => {
if (withinIterate) {
return makeIteratePath(path);
}
return makePath(path);
}, [makeIteratePath, makePath, withinIterate]);
const check = (0, _react.useCallback)(({
visible,
visibleWhen,
visibleWhenNot,
pathDefined,
pathUndefined,
pathTruthy,
pathFalsy,
pathTrue,
pathFalse,
pathValue,
whenValue,
inferData,
filterData
} = propsRef.current) => {
if (typeof visible === 'boolean') {
return visible;
}
const data = filterData && (filterDataHandler === null || filterDataHandler === void 0 ? void 0 : filterDataHandler(originalData, filterData)) || originalData;
if (visibleWhen || visibleWhenNot) {
if (visibleWhenNot) {
visibleWhen = visibleWhenNot;
}
const path = 'itemPath' in visibleWhen ? makeIteratePath(visibleWhen.itemPath) : makePath(visibleWhen.path);
if ('isValid' in visibleWhen) {
const item = mountedFieldsRef.current.get(path);
if (!item || item.isMounted !== true) {
return Boolean(visibleWhenNot);
}
const result = (visibleWhen.continuousValidation || visibleWhen.validateContinuously ? true : item.isFocused !== true) && !hasFieldError(path);
return visibleWhenNot ? !result : result;
}
if ('hasValue' in visibleWhen || 'withValue' in visibleWhen) {
var _visibleWhen, _visibleWhen$hasValue, _visibleWhen2, _visibleWhen3;
const hasPath = _index.default.has(data, path);
const value = hasPath ? _index.default.get(data, path) : undefined;
if ((_visibleWhen = visibleWhen) !== null && _visibleWhen !== void 0 && _visibleWhen['withValue']) {
console.warn('VisibleWhen: "withValue" is deprecated, use "hasValue" instead');
}
const hasValue = (_visibleWhen$hasValue = (_visibleWhen2 = visibleWhen) === null || _visibleWhen2 === void 0 ? void 0 : _visibleWhen2['hasValue']) !== null && _visibleWhen$hasValue !== void 0 ? _visibleWhen$hasValue : (_visibleWhen3 = visibleWhen) === null || _visibleWhen3 === void 0 ? void 0 : _visibleWhen3['withValue'];
const result = typeof hasValue === 'function' ? hasValue(value) === false : hasValue !== value;
if (visibleWhenNot) {
if (!result) {
return false;
}
} else if (result) {
return false;
}
}
}
const getValue = path => {
if (_index.default.has(data, path)) {
return _index.default.get(data, path);
}
};
if (pathDefined) {
return getValue(makeLocalPath(pathDefined)) !== undefined;
}
if (pathUndefined) {
return getValue(makeLocalPath(pathUndefined)) === undefined;
}
if (pathTrue && getValue(makeLocalPath(pathTrue)) !== true) {
return false;
}
if (pathFalse && getValue(makeLocalPath(pathFalse)) !== false) {
return false;
}
if (pathTruthy && !getValue(makeLocalPath(pathTruthy))) {
return false;
}
if (pathFalsy && getValue(makeLocalPath(pathFalsy))) {
return false;
}
if (inferData && !inferData(data)) {
return false;
}
if (pathValue && getValue(makeLocalPath(pathValue)) !== whenValue) {
return false;
}
return true;
}, [filterDataHandler, originalData, makeLocalPath, makeIteratePath, makePath, mountedFieldsRef, hasFieldError]);
return {
check
};
}
//# sourceMappingURL=useVisibility.js.map