2021-08-26 10:52:45 +00:00
|
|
|
import React from "react";
|
2022-05-04 09:45:57 +00:00
|
|
|
import styled, { createGlobalStyle } from "styled-components";
|
2021-08-26 10:52:45 +00:00
|
|
|
import Dropdown, { DropdownOption } from "components/ads/Dropdown";
|
|
|
|
|
import Icon, { IconSize } from "components/ads/Icon";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { countryToFlag } from "./utilities";
|
2021-08-26 10:52:45 +00:00
|
|
|
import { ISDCodeOptions, ISDCodeProps } from "constants/ISDCodes";
|
2021-11-01 14:44:08 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2022-05-04 09:45:57 +00:00
|
|
|
import { Classes } from "@blueprintjs/core";
|
|
|
|
|
import { lightenColor } from "widgets/WidgetUtils";
|
2021-08-26 10:52:45 +00:00
|
|
|
|
|
|
|
|
const DropdownTriggerIconWrapper = styled.div<{ disabled?: boolean }>`
|
2022-05-04 09:45:57 +00:00
|
|
|
height: 100%;
|
2021-08-26 10:52:45 +00:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
font-size: 14px;
|
2021-11-01 14:44:08 +00:00
|
|
|
line-height: ${(props) => (props.disabled ? 36 : 18)}px;
|
2021-08-26 10:52:45 +00:00
|
|
|
letter-spacing: -0.24px;
|
|
|
|
|
color: #090707;
|
2022-05-04 09:45:57 +00:00
|
|
|
cursor: pointer;
|
2021-08-26 10:52:45 +00:00
|
|
|
position: ${(props) => props.disabled && "absolute"};
|
2021-11-01 14:44:08 +00:00
|
|
|
|
|
|
|
|
.dropdown {
|
|
|
|
|
svg {
|
|
|
|
|
width: 14px;
|
|
|
|
|
height: 14px;
|
|
|
|
|
|
|
|
|
|
path {
|
|
|
|
|
fill: ${Colors.GREY_10} !important;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-26 10:52:45 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-11-01 14:44:08 +00:00
|
|
|
const FlagWrapper = styled.span`
|
|
|
|
|
font-size: 20px;
|
|
|
|
|
line-height: 19px;
|
|
|
|
|
`;
|
|
|
|
|
|
2022-05-04 09:45:57 +00:00
|
|
|
export const PopoverStyles = createGlobalStyle<{
|
|
|
|
|
borderRadius: string;
|
|
|
|
|
portalClassName: string;
|
|
|
|
|
accentColor: string;
|
|
|
|
|
}>`
|
|
|
|
|
${(props) => `
|
|
|
|
|
.${props.portalClassName} .${Classes.POPOVER} {
|
|
|
|
|
border-radius: ${props.borderRadius} !important;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
box-shadow: 0 6px 20px 0px rgba(0, 0, 0, 0.15) !important;
|
|
|
|
|
margin-top: 4px !important;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.${props.portalClassName} .${Classes.BUTTON} {
|
|
|
|
|
border-radius: ${props.borderRadius} !important;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.${props.portalClassName} .${Classes.INPUT} {
|
|
|
|
|
border-radius: ${props.borderRadius} !important;
|
|
|
|
|
min-height: 32px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.${props.portalClassName} .${Classes.INPUT}:focus, .${
|
|
|
|
|
props.portalClassName
|
|
|
|
|
} .${Classes.INPUT}:active {
|
|
|
|
|
border: 1px solid ${props.accentColor} !important;
|
|
|
|
|
box-shadow: 0px 0px 0px 3px ${lightenColor(
|
|
|
|
|
props.accentColor,
|
|
|
|
|
)} !important;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.${props.portalClassName} .t--dropdown-option:hover,
|
|
|
|
|
.${props.portalClassName} .t--dropdown-option.selected {
|
|
|
|
|
background-color: ${lightenColor(props.accentColor)} !important;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.${props.portalClassName} .ads-dropdown-options-wrapper {
|
|
|
|
|
border: 0px solid !important;
|
|
|
|
|
}
|
|
|
|
|
`}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-08-26 10:52:45 +00:00
|
|
|
const getISDCodeOptions = (): Array<DropdownOption> => {
|
|
|
|
|
return ISDCodeOptions.map((item: ISDCodeProps) => {
|
|
|
|
|
return {
|
|
|
|
|
leftElement: countryToFlag(item.code),
|
|
|
|
|
searchText: item.name,
|
|
|
|
|
label: `${item.name} (${item.dial_code})`,
|
|
|
|
|
value: item.code,
|
|
|
|
|
id: item.dial_code,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ISDCodeDropdownOptions = getISDCodeOptions();
|
|
|
|
|
|
|
|
|
|
export const getSelectedISDCode = (code?: string): DropdownOption => {
|
|
|
|
|
let selectedCountry: ISDCodeProps | undefined = code
|
|
|
|
|
? ISDCodeOptions.find((item: ISDCodeProps) => {
|
|
|
|
|
return item.code === code;
|
|
|
|
|
})
|
|
|
|
|
: undefined;
|
|
|
|
|
if (!selectedCountry) {
|
|
|
|
|
selectedCountry = {
|
|
|
|
|
name: "United States",
|
|
|
|
|
dial_code: "+1",
|
|
|
|
|
code: "US",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
label: `${selectedCountry.name} (${selectedCountry.dial_code})`,
|
|
|
|
|
searchText: selectedCountry.name,
|
|
|
|
|
value: selectedCountry.code,
|
|
|
|
|
id: selectedCountry.dial_code,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface ISDCodeDropdownProps {
|
|
|
|
|
onISDCodeChange: (code?: string) => void;
|
|
|
|
|
options: Array<DropdownOption>;
|
|
|
|
|
selected: DropdownOption;
|
|
|
|
|
allowCountryCodeChange?: boolean;
|
|
|
|
|
disabled: boolean;
|
2022-05-04 09:45:57 +00:00
|
|
|
borderRadius: string;
|
|
|
|
|
accentColor: string;
|
|
|
|
|
widgetId: string;
|
2021-08-26 10:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function ISDCodeDropdown(props: ISDCodeDropdownProps) {
|
|
|
|
|
const selectedCountry = getSelectedISDCode(props.selected.value);
|
|
|
|
|
const dropdownTrigger = (
|
|
|
|
|
<DropdownTriggerIconWrapper
|
2022-05-04 09:45:57 +00:00
|
|
|
className="gap-2 px-3 t--input-country-code-change focus:bg-gray-50"
|
2021-08-26 10:52:45 +00:00
|
|
|
disabled={props.disabled}
|
2022-05-04 09:45:57 +00:00
|
|
|
tabIndex={0}
|
2021-08-26 10:52:45 +00:00
|
|
|
>
|
2021-11-01 14:44:08 +00:00
|
|
|
<FlagWrapper>
|
2021-08-26 10:52:45 +00:00
|
|
|
{selectedCountry.value && countryToFlag(selectedCountry.value)}
|
2021-11-01 14:44:08 +00:00
|
|
|
</FlagWrapper>
|
2021-08-26 10:52:45 +00:00
|
|
|
<div className="code">{selectedCountry.id && selectedCountry.id}</div>
|
2021-11-01 14:44:08 +00:00
|
|
|
<Icon className="dropdown" name="down-arrow" size={IconSize.XXS} />
|
2021-08-26 10:52:45 +00:00
|
|
|
</DropdownTriggerIconWrapper>
|
|
|
|
|
);
|
|
|
|
|
if (props.disabled) {
|
|
|
|
|
return dropdownTrigger;
|
|
|
|
|
}
|
|
|
|
|
return (
|
2022-05-04 09:45:57 +00:00
|
|
|
<>
|
|
|
|
|
<Dropdown
|
|
|
|
|
containerClassName="country-type-filter"
|
|
|
|
|
dropdownHeight="139px"
|
|
|
|
|
dropdownTriggerIcon={dropdownTrigger}
|
|
|
|
|
enableSearch
|
|
|
|
|
height="36px"
|
|
|
|
|
onSelect={props.onISDCodeChange}
|
|
|
|
|
optionWidth="340px"
|
|
|
|
|
options={props.options}
|
|
|
|
|
portalClassName={`country-type-filter-dropdown-${props.widgetId}`}
|
|
|
|
|
searchPlaceholder="Search by ISD code or country"
|
|
|
|
|
selected={props.selected}
|
|
|
|
|
showLabelOnly
|
|
|
|
|
/>
|
|
|
|
|
<PopoverStyles
|
|
|
|
|
accentColor={props.accentColor}
|
|
|
|
|
borderRadius={props.borderRadius}
|
|
|
|
|
portalClassName={`country-type-filter-dropdown-${props.widgetId}`}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2021-08-26 10:52:45 +00:00
|
|
|
);
|
|
|
|
|
}
|