@sap-ux/ui-components
Version:
SAP UI Components Library
76 lines • 3.96 kB
JavaScript
import React, { useCallback, useState } from 'react';
import { UIDefaultButton } from '../UIButton/index.js';
import { UICallout, UICalloutContentPadding } from '../UICallout/index.js';
import { UiIcons } from '../Icons.js';
import { SuggestValueType } from './UITranslationButton.types.js';
import { UILoadButton } from './UILoadButton.js';
import './UITranslationInput.scss';
/**
* Method to resolve button component text strings for passed key.
* Component has default texts which can be overwritten using property `strings`.
*
* @param property Property.
* @param strings Map with all text properties.
* @returns Resolved text.
*/
const getStringText = (property, strings) => {
return strings?.[property] ?? '';
};
/**
* Returns the CSS class names for the translation callout component.
* Adds the base callout class and optionally an inverted theme modifier.
*
* @param invertedCalloutTheme - If true, applies the inverted callout theme modifier.
* @returns A string containing the computed class names.
*/
const getCalloutClassNames = (invertedCalloutTheme) => {
let classNames = ' ui-translatable__callout';
if (invertedCalloutTheme) {
classNames += ` ui-translatable__callout--inverted`;
}
return classNames;
};
/**
* Component to render translation button to provide helper callout with i18n generation option.
*
* @param props Component properties.
* @returns Component to render translation button with callout.
*/
export const UITranslationButton = (props) => {
const { id, strings, value, onCreateNewEntry, onUpdateValue, onShowExistingEntry, busy, suggestion, invertedCalloutTheme } = props;
const [calloutVisible, setCalloutVisible] = useState(false);
// Callbacks
const onToggleCallout = useCallback(() => {
if (suggestion.suggest?.type === SuggestValueType.Existing) {
setCalloutVisible(false);
// Trigger show existing entry callbACK
onShowExistingEntry?.(suggestion.suggest.entry);
}
else {
setCalloutVisible(!calloutVisible);
}
}, [calloutVisible, suggestion]);
const onAccept = useCallback(() => {
if (suggestion.suggest) {
if (suggestion.suggest.type === SuggestValueType.New) {
onCreateNewEntry?.(suggestion.suggest.entry);
}
if (value !== suggestion.suggest.i18n) {
onUpdateValue?.(suggestion.suggest.i18n ?? '');
}
}
setCalloutVisible(false);
}, [suggestion, onCreateNewEntry, onUpdateValue, value]);
const onCancel = useCallback(() => {
setCalloutVisible(false);
}, []);
return (React.createElement("div", { className: "ui-translatable__button" },
React.createElement(UILoadButton, { id: id, disabled: props.disabled ?? false, onClick: onToggleCallout, iconProps: { iconName: suggestion.suggest?.icon ?? UiIcons.World }, title: suggestion.tooltip, busy: busy?.busy, useMinWaitingTime: busy?.useMinWaitingTime }),
calloutVisible && (React.createElement(UICallout, { target: `#${id}`, gapSpace: 5, directionalHint: 5, calloutWidth: 250, calloutMinWidth: 250, beakWidth: 8, isBeakVisible: false, setInitialFocus: true, className: getCalloutClassNames(invertedCalloutTheme), onDismiss: onCancel, contentPadding: UICalloutContentPadding.Standard },
React.createElement("div", { className: "ui-translatable__message" },
suggestion.message,
React.createElement("div", { className: "ui-translatable__actions" },
React.createElement(UIDefaultButton, { id: `${id}-button-action-confirm`, primary: true, onClick: onAccept }, getStringText('acceptButtonLabel', strings)),
React.createElement(UIDefaultButton, { id: `${id}-button-action-cancel`, onClick: onCancel }, getStringText('cancelButtonLabel', strings))))))));
};
//# sourceMappingURL=UITranslationButton.js.map