The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { isDefined } from 'twenty-shared/utils';
import { styled } from '@linaria/react';
import { useEffect, useRef, useState } from 'react';
import { useRegisterInputEvents } from '@/ui/input/hooks/useRegisterInputEvents';
import { useNumberFormat } from '@/localization/hooks/useNumberFormat';
import { CURRENCIES } from '@/ui/input/components/internal/currency/constants/Currencies';
import { CurrencyPickerDropdownButton } from '@/ui/input/components/internal/currency/components/CurrencyPickerDropdownButton';
import { CURRENCY_MICROS_DECIMAL_PLACES } from '@/ui/field/input/constants/CurrencyMicrosDecimalPlaces';
import { IMaskInput } from 'react-imask';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';
import { getSeparatorsForNumberFormat } from '@/localization/utils/getSeparatorsForNumberFormat';
export const StyledIMaskInput = styled.div`
display: contents;
> input {
background-color: transparent;
border: none;
color: ${themeCssVariables.font.color.primary};
font-family: ${themeCssVariables.font.family};
font-size: inherit;
font-weight: inherit;
margin: 0;
outline: none;
padding: ${themeCssVariables.spacing[0]} ${themeCssVariables.spacing[1.5]};
&::placeholder,
&::-webkit-input-placeholder {
color: ${themeCssVariables.font.color.light};
font-family: ${themeCssVariables.font.family};
font-weight: ${themeCssVariables.font.weight.medium};
}
width: 100%;
}
`;
const StyledContainer = styled.div`
align-items: center;
display: flex;
justify-content: center;
`;
const StyledIcon = styled.div`
align-items: center;
display: flex;
padding-left: ${themeCssVariables.spacing[1]};
`;
export type CurrencyInputProps = {
instanceId: string;
placeholder?: string;
autoFocus?: boolean;
value: string;
currencyCode: 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;
onSelect?: (newText: string) => void;
};
export const CurrencyInput = ({
instanceId,
autoFocus,
value,
currencyCode,
placeholder,
onEnter,
onEscape,
onTab,
onShiftTab,
onClickOutside,
onChange,
onSelect,
}: CurrencyInputProps) => {
const theme = useTheme();
const [internalText, setInternalText] = useState(value);
const { numberFormat } = useNumberFormat();
const wrapperRef = useRef<HTMLInputElement>(null);
const { thousandsSeparator, radix } =
getSeparatorsForNumberFormat(numberFormat);
// imask re-emits accept while formatting the incoming value, with no
// originating input event; only a user keystroke may change the draft
const handleAccept = (value: string, event?: InputEvent) => {
if (!isDefined(event)) {
return;
}
setInternalText(value);
onChange?.(value);
};
const handleCurrencyChange = (newCurrencyCode: string) => {
onSelect?.(newCurrencyCode);
};
useRegisterInputEvents({
focusId: instanceId,
inputRef: wrapperRef,
inputValue: internalText,
onEnter,
onEscape,
onClickOutside,
onTab,
onShiftTab,
});
const currency = CURRENCIES.find(({ value }) => value === currencyCode);
useEffect(() => {
setInternalText(value);
}, [value]);
const Icon = currency?.Icon;
return (
<StyledContainer ref={wrapperRef}>
<CurrencyPickerDropdownButton
selectedCurrencyCode={currencyCode}
onChange={handleCurrencyChange}
/>
<StyledIcon>
{isDefined(Icon) && (
<Icon
color={theme.font.color.tertiary}
size={theme.icon.size.md}
stroke={theme.icon.stroke.sm}
/>
)}
</StyledIcon>
<StyledIMaskInput>
<IMaskInput
mask={Number}
thousandsSeparator={thousandsSeparator}
radix={radix}
scale={CURRENCY_MICROS_DECIMAL_PLACES}
onAccept={(value: string, _maskRef: unknown, event?: InputEvent) =>
handleAccept(value, event)
}
inputRef={wrapperRef}
autoComplete="off"
placeholder={placeholder}
autoFocus={autoFocus}
value={value}
unmask
/>
</StyledIMaskInput>
</StyledContainer>
);
};