UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

124 lines (118 loc) 4.44 kB
/** * Copyright IBM Corp. 2020, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; var React = require('react'); var index = require('../../../_virtual/index.js'); var uuidv4 = require('../../../global/js/utils/uuidv4.js'); const emptyState = { operator: 'or', groups: [{ groupOperator: 'and', statement: 'ifAll', id: uuidv4.default(), conditions: [{ property: undefined, operator: '', value: '', popoverToOpen: 'propertyField', id: uuidv4.default() }] }] }; const ConditionBuilderContext = /*#__PURE__*/React.createContext({ rootState: { groups: [] } }); const ConditionBuilderProvider = props => { const [rootState, setRootState] = React.useState({ groups: [] }); const [actionState, setActionState] = React.useState([]); const contextValue = { rootState, setRootState, actionState, setActionState, inputConfig: props.inputConfig, popOverSearchThreshold: props.popOverSearchThreshold, getOptions: props.getOptions, variant: props.variant, translateWithId: props.translateWithId, conditionBuilderRef: props.conditionBuilderRef, statementConfigCustom: props.statementConfigCustom, onAddItem: props.onAddItem }; return /*#__PURE__*/React.createElement(ConditionBuilderContext.Provider, { value: contextValue }, props.children); }; ConditionBuilderProvider.propTypes = { /** * Provide the contents of the ConditionBuilder. */ children: index.default.node.isRequired, /** * ref of condition builder */ /**@ts-ignore */ conditionBuilderRef: index.default.object, /** * This is an optional callback function that will be triggered when options array is not passed in the inputConfig against a property. * This can be a asynchronous function that need to return a promise, so it will allow to fetch options from API call. * options has to be in valid format * [{ label: 'label', id: 'id', },...] */ getOptions: index.default.func, /** * This is a mandatory prop that defines the input to the condition builder. */ /**@ts-ignore */ inputConfig: index.default.shape({ properties: index.default.arrayOf(index.default.shape({ id: index.default.string.isRequired, label: index.default.string.isRequired, icon: index.default.oneOfType([index.default.func, index.default.object]), description: index.default.string, type: index.default.oneOf(['text', 'textarea', 'number', 'date', 'option', 'time', 'custom']).isRequired, config: index.default.shape({ options: index.default.arrayOf(index.default.shape({ id: index.default.string.isRequired, label: index.default.string.isRequired, icon: index.default.oneOfType([index.default.func, index.default.object]) })), component: index.default.func, operators: index.default.arrayOf(index.default.shape({ id: index.default.string.isRequired, label: index.default.string.isRequired })), long: index.default.bool }) })) }).isRequired, /** * Provide an mandatory numeric value that will be used to enable search option in the popovers with list. */ popOverSearchThreshold: index.default.number.isRequired, /** * Optional prop if you want to pass translation to the texts used . Otherwise this will the defined defaults. * This callback function will receive the message id and you need to return the corresponding text for that id. * The message key will be one of [ "ifText", "addConditionText", "addConditionGroupText", "addSubgroupText", "conditionText", "propertyText", "operatorText", "valueText", "connectorText", "conditionRowText", "removeConditionText", "addConditionRowText", "startText", "endText", "clearSearchText", "actionsText", "then", "removeActionText", "addActionText", "invalidText", "invalidNumberWarnText" ] */ translateWithId: index.default.func, /** * Provide the condition builder variant: Non-Hierarchical/ Hierarchical */ variant: index.default.string.isRequired }; exports.ConditionBuilderContext = ConditionBuilderContext; exports.ConditionBuilderProvider = ConditionBuilderProvider; exports.emptyState = emptyState;