@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
53 lines (52 loc) • 2 kB
JavaScript
;
"use client";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _clsx = _interopRequireDefault(require("clsx"));
var _consts = require("./consts");
function getCountryProps(code, name) {
const codeNormalized = code ? code.toUpperCase().replace("-", "_") : "UNDEFINED";
const countryCodeExists = (codeNormalized in _consts.CODES);
if (!countryCodeExists) console.warn(`Country code not supported: ${code}`);
const countryCode = countryCodeExists ? _consts.CODES[codeNormalized] : _consts.CODES.UNDEFINED;
const countryName = countryCode === _consts.CODES.UNDEFINED && !name ? "Undefined" : name;
return {
code: countryCode,
name: countryName
};
}
const CountryFlag = ({
dataTest,
size = _consts.SIZES.MEDIUM,
id,
...props
}) => {
const {
code,
name
} = getCountryProps(props.code, props.name);
const width = _consts.SIZE_WIDTHS[size];
const src = `${_consts.baseURL}/flags/${width}x0/flag-${code.toLowerCase()}.jpg`;
const srcSet = `${_consts.baseURL}/flags/${width * 2}x0/flag-${code.toLowerCase()}.jpg 2x`;
return /*#__PURE__*/_react.default.createElement("div", {
className: (0, _clsx.default)("rounded-small bg-country-flag-background relative shrink-0 overflow-hidden", {
"h-country-flag-small w-country-flag-small": size === _consts.SIZES.SMALL,
"h-country-flag-medium w-country-flag-medium": size === _consts.SIZES.MEDIUM
})
}, /*#__PURE__*/_react.default.createElement("img", {
className: "block h-full w-full shrink-0",
key: code,
alt: name,
title: name,
id: id,
"data-test": dataTest,
src: src,
srcSet: srcSet
}), /*#__PURE__*/_react.default.createElement("div", {
className: "rounded-small shadow-country-flag absolute inset-0 block h-full w-full"
}));
};
var _default = CountryFlag;
exports.default = _default;