UNPKG

@hitachivantara/uikit-react-core

Version:

Core React components for the NEXT Design System.

149 lines (148 loc) 4.78 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const jsxRuntime = require("react/jsx-runtime"); const React = require("react"); const uikitReactUtils = require("@hitachivantara/uikit-react-utils"); const useControlled = require("../hooks/useControlled.cjs"); const useLabels = require("../hooks/useLabels.cjs"); const helpers = require("../utils/helpers.cjs"); const Context = require("./Context.cjs"); const QueryBuilder_styles = require("./QueryBuilder.styles.cjs"); const index = require("./utils/index.cjs"); const reducer = require("./utils/reducer.cjs"); const RuleGroup = require("./RuleGroup/RuleGroup.cjs"); const ConfirmationDialog = require("./ConfirmationDialog/ConfirmationDialog.cjs"); const HvQueryBuilder = (props) => { const { attributes, renderers, query: queryProp, // TODO - remove in v6 value, defaultValue, onChange, disableConfirmation = false, allowRepeatedAttributes = false, operators = Context.defaultOperators, combinators = Context.defaultCombinators, maxDepth = 1, labels: labelsProp, readOnly = false, emptyRenderer = ["Empty", "IsNotEmpty"], classes: classesProp } = uikitReactUtils.useDefaultProps("HvQueryBuilder", props); const { classes } = QueryBuilder_styles.useClasses(classesProp); const currentAttributes = React.useRef( null ); const controlled = React.useRef(value != null); const initialQuery = React.useRef( value ?? defaultValue ?? queryProp ?? index.emptyGroup() ); const [query, setQuery] = useControlled.useControlled(value, initialQuery.current); const prevQuery = React.useRef(query); const [pendingAction, setPendingAction] = React.useState(); const [initialState, setInitialState] = React.useState(true); const [state, dispatchAction] = React.useReducer( reducer.default, index.setNodeIds(structuredClone(initialQuery.current)) ); const labels = useLabels.useLabels(Context.defaultLabels, labelsProp); const contextValue = React.useMemo( () => ({ dispatchAction, askAction: setPendingAction, attributes, operators, combinators, maxDepth, labels, initialTouched: initialState, readOnly, renderers, disableConfirmation, allowRepeatedAttributes, emptyRenderer }), [ attributes, operators, combinators, maxDepth, labels, readOnly, initialState, renderers, disableConfirmation, emptyRenderer, allowRepeatedAttributes ] ); React.useEffect(() => { if (currentAttributes.current == null) { currentAttributes.current = attributes; } else if (currentAttributes.current !== attributes) { currentAttributes.current = attributes; dispatchAction({ type: "reset-query" }); } }, [attributes]); React.useEffect(() => { if (!helpers.isEqual(prevQuery.current, query)) { dispatchAction({ type: "set-query", query: index.setNodeIds(structuredClone(query), state) }); prevQuery.current = query; } else if (!helpers.isEqual( index.clearNodeIds(structuredClone(state)), index.clearNodeIds(structuredClone(query)) )) { setInitialState(false); if (!controlled.current) { onChange?.( index.clearNodeIds(structuredClone(state), true) ); } else { onChange?.(index.clearNodeIds(structuredClone(state))); } prevQuery.current = state; setQuery(state); } }, [onChange, query, setQuery, state]); const handleConfirm = () => { if (pendingAction) { setPendingAction(void 0); pendingAction.actions.forEach((action) => dispatchAction(action)); } }; const handleCancel = () => { setPendingAction(void 0); }; return /* @__PURE__ */ jsxRuntime.jsxs(Context.HvQueryBuilderProvider, { value: contextValue, children: [ /* @__PURE__ */ jsxRuntime.jsx( RuleGroup.RuleGroup, { level: 0, id: state.id, combinator: state.combinator, rules: state.rules, classes } ), /* @__PURE__ */ jsxRuntime.jsx( ConfirmationDialog.ConfirmationDialog, { isOpen: pendingAction != null, onConfirm: handleConfirm, onCancel: handleCancel, title: pendingAction?.dialog.dialogTitle, message: pendingAction?.dialog.dialogMessage, confirmButtonLabel: pendingAction?.dialog.dialogConfirm, cancelButtonLabel: pendingAction?.dialog.dialogCancel, closeButtonTooltip: pendingAction?.dialog.dialogCloseTooltip } ) ] }); }; exports.queryBuilderClasses = QueryBuilder_styles.staticClasses; exports.HvQueryBuilder = HvQueryBuilder;