All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.js.components.ObjectValidation.Conditions.tsx Maven / Gradle / Ivy

The newest version!
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

import ClayAlert from '@clayui/alert';
import {
	Card,
	CodeEditor,
	SidebarCategory,
	SingleSelect,
	stringUtils,
} from '@liferay/object-js-components-web';
import {
	ILearnResourceContext,
	LearnMessage,
	LearnResourcesContext,
} from 'frontend-js-components-web';
import React, {useMemo} from 'react';

import {NAME_OUTPUT_OBJECT_FIELD_EXTERNAL_REFERENCE_CODE} from '../../utils/constants';
import {DisabledGroovyScriptAlert} from '../DisabledGroovyScriptAlert';
import {ErrorMessage} from './ErrorMessage';
import {TabProps} from './useObjectValidationForm';

export interface ConditionsProps extends TabProps {
	creationLanguageId: Liferay.Language.Locale;
	customObjectFields: ObjectField[];
	disabledGroovyValidation: boolean;
	learnResources: ILearnResourceContext;
	objectValidationRuleElements: SidebarCategory[];
}

export function Conditions({
	creationLanguageId,
	customObjectFields,
	disabled,
	disabledGroovyValidation,
	errors,
	learnResources,
	objectValidationRuleElements,
	scriptManagementConfigurationPortletURL,
	selectedPartialValidationField,
	setValues,
	values,
}: ConditionsProps) {
	const engine = values.engine;
	const ddmTooltip = {
		content: Liferay.Language.get(
			'use-the-expression-builder-to-define-the-format-of-a-valid-object-entry'
		),
		symbol: 'question-circle-full',
	};
	let placeholder;

	if (engine === 'groovy') {
		placeholder = Liferay.Language.get(
			'insert-a-groovy-script-to-define-your-validation'
		);
	}
	else if (engine === 'ddm') {
		placeholder = Liferay.Language.get(
			'add-elements-from-the-sidebar-to-define-your-validation'
		);
	}
	else {
		placeholder = '';
	}

	const objectFieldsItems = useMemo(() => {
		return customObjectFields.map(
			({externalReferenceCode, label, name}) => ({
				label: stringUtils.getLocalizableLabel({
					fallbackLabel: name,
					fallbackLanguageId: creationLanguageId,
					labels: label,
				}),
				value: externalReferenceCode,
			})
		);
	}, [creationLanguageId, customObjectFields]);

	const hasLocalizedField = useMemo(() => {
		return customObjectFields.some((field) => field.localized);
	}, [customObjectFields]);

	return (
		<>
			{disabledGroovyValidation && (
				
			)}

			
				{Liferay.Language.get('create-validations-using-expressions')}
				 
				
					
				
			
			
				{hasLocalizedField && (
					
						{`${Liferay.Language.get(
							'this-object-includes-translatable-fields'
						)} ${Liferay.Language.get(
							'validations-always-use-the-object-entrys-default-language'
						)}`}
					
				)}

				
						setValues({lineCount, script})
					}
					placeholder={placeholder}
					readOnly={disabled || disabledGroovyValidation}
					sidebarElements={objectValidationRuleElements}
					value={values.script ?? ''}
				/>
			

			
				 {
						setValues({
							objectValidationRuleSettings: [
								{
									name: NAME_OUTPUT_OBJECT_FIELD_EXTERNAL_REFERENCE_CODE,
									value: value as string,
								},
							],
						});
					}}
					required
					selectedKey={selectedPartialValidationField}
				/>
			
		
	);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy