Fixes #5943 ### Before Light <img width="218" alt="Screenshot 2024-06-19 at 12 37 22 PM" src="https://github.com/twentyhq/twenty/assets/57673080/981d1877-be4e-4071-9a8d-9d0ed7e933ab"> Dark <img width="223" alt="Screenshot 2024-06-19 at 12 39 42 PM" src="https://github.com/twentyhq/twenty/assets/57673080/a3730ef5-21ba-4d90-998d-d330aec350ad"> ### After Light <img width="216" alt="Screenshot 2024-06-19 at 12 39 00 PM" src="https://github.com/twentyhq/twenty/assets/57673080/eef3b743-1b28-43a5-8c1c-bd944a4915c7"> Dark <img width="228" alt="Screenshot 2024-06-19 at 12 39 29 PM" src="https://github.com/twentyhq/twenty/assets/57673080/5bf10e51-5a07-4d55-99f1-734517b22781">
132 lines
3.4 KiB
TypeScript
132 lines
3.4 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import ReactPhoneNumberInput from 'react-phone-number-input';
|
|
import styled from '@emotion/styled';
|
|
import { TEXT_INPUT_STYLE } from 'twenty-ui';
|
|
|
|
import { LightCopyIconButton } from '@/object-record/record-field/components/LightCopyIconButton';
|
|
import { useRegisterInputEvents } from '@/object-record/record-field/meta-types/input/hooks/useRegisterInputEvents';
|
|
import { PhoneCountryPickerDropdownButton } from '@/ui/input/components/internal/phone/components/PhoneCountryPickerDropdownButton';
|
|
|
|
import 'react-phone-number-input/style.css';
|
|
|
|
const StyledContainer = styled.div`
|
|
align-items: center;
|
|
|
|
border: none;
|
|
border-radius: ${({ theme }) => theme.border.radius.sm};
|
|
box-shadow: ${({ theme }) => theme.boxShadow.strong};
|
|
width: 100%;
|
|
|
|
display: flex;
|
|
justify-content: start;
|
|
`;
|
|
|
|
const StyledCustomPhoneInput = styled(ReactPhoneNumberInput)`
|
|
font-family: ${({ theme }) => theme.font.family};
|
|
height: 32px;
|
|
${TEXT_INPUT_STYLE}
|
|
padding: 0;
|
|
|
|
.PhoneInputInput {
|
|
background: none;
|
|
border: none;
|
|
color: ${({ theme }) => theme.font.color.primary};
|
|
|
|
&::placeholder,
|
|
&::-webkit-input-placeholder {
|
|
color: ${({ theme }) => theme.font.color.light};
|
|
font-family: ${({ theme }) => theme.font.family};
|
|
font-weight: ${({ theme }) => theme.font.weight.medium};
|
|
}
|
|
|
|
:focus {
|
|
outline: none;
|
|
}
|
|
}
|
|
|
|
& svg {
|
|
border-radius: ${({ theme }) => theme.border.radius.xs};
|
|
height: 12px;
|
|
}
|
|
width: calc(100% - ${({ theme }) => theme.spacing(8)});
|
|
`;
|
|
|
|
const StyledLightIconButtonContainer = styled.div`
|
|
position: absolute;
|
|
top: 50%;
|
|
transform: translateY(-50%);
|
|
right: 0;
|
|
`;
|
|
|
|
export type PhoneInputProps = {
|
|
placeholder?: string;
|
|
autoFocus?: boolean;
|
|
value: string;
|
|
onEnter: (newText: string) => void;
|
|
onEscape: (newText: string) => void;
|
|
onTab?: (newText: string) => void;
|
|
onShiftTab?: (newText: string) => void;
|
|
onClickOutside: (event: MouseEvent | TouchEvent, inputValue: string) => void;
|
|
onChange?: (newText: string) => void;
|
|
hotkeyScope: string;
|
|
copyButton?: boolean;
|
|
};
|
|
|
|
export const PhoneInput = ({
|
|
autoFocus,
|
|
value,
|
|
onEnter,
|
|
onEscape,
|
|
onTab,
|
|
onShiftTab,
|
|
onClickOutside,
|
|
hotkeyScope,
|
|
onChange,
|
|
copyButton = true,
|
|
}: PhoneInputProps) => {
|
|
const [internalValue, setInternalValue] = useState<string | undefined>(value);
|
|
|
|
const wrapperRef = useRef<HTMLDivElement>(null);
|
|
const copyRef = useRef<HTMLDivElement>(null);
|
|
|
|
const handleChange = (newValue: string) => {
|
|
setInternalValue(newValue);
|
|
onChange?.(newValue);
|
|
};
|
|
|
|
useEffect(() => {
|
|
setInternalValue(value);
|
|
}, [value]);
|
|
|
|
useRegisterInputEvents({
|
|
inputRef: wrapperRef,
|
|
copyRef: copyRef,
|
|
inputValue: internalValue ?? '',
|
|
onEnter,
|
|
onEscape,
|
|
onClickOutside,
|
|
onTab,
|
|
onShiftTab,
|
|
hotkeyScope,
|
|
});
|
|
|
|
return (
|
|
<StyledContainer ref={wrapperRef}>
|
|
<StyledCustomPhoneInput
|
|
autoFocus={autoFocus}
|
|
placeholder="Phone number"
|
|
value={value}
|
|
onChange={handleChange}
|
|
international={true}
|
|
withCountryCallingCode={true}
|
|
countrySelectComponent={PhoneCountryPickerDropdownButton}
|
|
/>
|
|
{copyButton && (
|
|
<StyledLightIconButtonContainer ref={copyRef}>
|
|
<LightCopyIconButton copyText={value} />
|
|
</StyledLightIconButtonContainer>
|
|
)}
|
|
</StyledContainer>
|
|
);
|
|
};
|