import { useState, useEffect } from 'react';
import { Radio, RadioGroup, FormControl, FormControlLabel, FormLabel, FormHelperText } from '@mui/material';
import Utils from '@pega/react-sdk-components/lib/components/helpers/utils';
import handleEvent from '@pega/react-sdk-components/lib/components/helpers/event-utils';
import { getComponentFromMap } from '@pega/react-sdk-components/lib/bridge/helpers/sdk_component_map';
import type { PConnFieldProps } from '@pega/react-sdk-components/lib/types/PConnProps';
import useStatus from '@pega/react-sdk-components/lib/hooks/useStatus';
import { getFormControlSx } from '@pega/react-sdk-components/lib/components/helpers/field-utils';
// Can't use RadioButtonProps until getLocaleRuleNameFromKeys is NOT private
interface RadioButtonsProps extends PConnFieldProps {
// If any, enter additional props that only exist on RadioButtons here
inline: boolean;
fieldMetadata?: any;
variant?: string;
hideFieldLabels?: boolean;
additionalProps?: any;
imagePosition?: string;
imageSize?: string;
showImageDescription?: boolean;
datasource?: any;
showFieldMessage?: boolean;
messageConfig?: {
content?: string;
visibility?: boolean;
};
}
export default function RadioButtons(props: RadioButtonsProps) {
// Get emitted components from map (so we can get any override that may exist)
const FieldValueList = getComponentFromMap('FieldValueList');
const SelectableCard = getComponentFromMap('SelectableCard');
const {
getPConnect,
label,
value = '',
readOnly,
validatemessage,
helperText,
required,
inline,
displayMode,
hideLabel,
fieldMetadata,
variant,
hideFieldLabels,
additionalProps,
datasource,
imagePosition,
imageSize,
showImageDescription,
showFieldMessage,
messageConfig = {}
} = props;
const [theSelectedButton, setSelectedButton] = useState(value);
const thePConn = getPConnect();
const theConfigProps = thePConn.getConfigProps();
const actionsApi = thePConn.getActionsApi();
const propName = (thePConn.getStateProps() as any).value;
const eligibleForFieldWarning = showFieldMessage && messageConfig.visibility && !readOnly;
const helperTextToDisplay = validatemessage || (eligibleForFieldWarning ? messageConfig.content : helperText);
const className = thePConn.getCaseInfo().getClassName();
const status = useStatus({
showFieldMessage,
messageVisibility: messageConfig.visibility,
validatemessage,
readOnly
});
let configProperty = (thePConn.getRawMetadata() as any)?.config?.value || '';
configProperty = configProperty.startsWith('@P') ? configProperty.substring(3) : configProperty;
configProperty = configProperty.startsWith('.') ? configProperty.substring(1) : configProperty;
const metaData = Array.isArray(fieldMetadata) ? fieldMetadata.filter(field => field?.classID === className)[0] : fieldMetadata;
let displayName = metaData?.datasource?.propertyForDisplayText;
displayName = displayName?.slice(displayName.lastIndexOf('.') + 1);
const localeContext = metaData?.datasource?.tableType === 'DataPage' ? 'datapage' : 'associated';
const localeClass = localeContext === 'datapage' ? '@baseclass' : className;
const localeName = localeContext === 'datapage' ? metaData?.datasource?.name : configProperty;
const localePath = localeContext === 'datapage' ? displayName : localeName;
// theOptions will be an array of JSON objects that are literally key/value pairs.
// Ex: [ {key: "Basic", value: "Basic"} ]
const theOptions = Utils.getOptionList(theConfigProps, thePConn.getDataObject(''));
useEffect(() => {
// This update theSelectedButton which will update the UI to show the selected button correctly
setSelectedButton(value);
}, [value]);
if (displayMode === 'DISPLAY_ONLY') {
return (