import { useEffect, useState } from 'react'; import { MuiTelInput } from 'mui-tel-input'; 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 handleEvent from '@pega/react-sdk-components/lib/components/helpers/event-utils'; import useStatus from '@pega/react-sdk-components/lib/hooks/useStatus'; import { getFieldSx } from '@pega/react-sdk-components/lib/components/helpers/field-utils'; interface PhoneProps extends PConnFieldProps { // If any, enter additional props that only exist on Phone here showFieldMessage?: boolean; messageConfig?: { content?: string; visibility?: boolean; }; variant?: string; } export default function Phone(props: PhoneProps) { // Get emitted components from map (so we can get any override that may exist) const FieldValueList = getComponentFromMap('FieldValueList'); const { getPConnect, label, required, disabled, value = '', validatemessage, onChange, readOnly, testId, helperText, displayMode, hideLabel, placeholder, showFieldMessage, messageConfig = {} } = props; const pConn = getPConnect(); const actions = pConn.getActionsApi(); const propName = (pConn.getStateProps() as any).value; const [inputValue, setInputValue] = useState(value); useEffect(() => setInputValue(value), [value]); const eligibleForFieldWarning = showFieldMessage && messageConfig.visibility && !readOnly; const helperTextToDisplay = validatemessage || (eligibleForFieldWarning ? messageConfig.content : helperText); const status = useStatus({ showFieldMessage, messageVisibility: messageConfig.visibility, validatemessage, readOnly }); let testProp = {}; testProp = { 'data-test-id': testId }; if (displayMode === 'DISPLAY_ONLY') { return ; } if (displayMode === 'STACKED_LARGE_VAL') { return ; } if (readOnly) { const disableDropdown = true; return (
); } const handleChange = inputVal => { setInputValue(inputVal); }; const handleBlur = event => { const phoneValue = event?.target?.value; let phoneNumber = phoneValue.split(' ').slice(1).join(); phoneNumber = phoneNumber ? `+${phoneValue && phoneValue.replace(/\D+/g, '')}` : ''; handleEvent(actions, 'changeNblur', propName, phoneNumber); }; return ( ); }