@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.
139 lines • 4.18 kB
JavaScript
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../defaultTheme";
import { left } from "../utils/rtl";
import { SIZE_OPTIONS, BASE_URL } from "./consts";
import { useRandomIdSeed } from "../hooks/useRandomId";
const getRenderSize = ({
theme,
size
}) => {
const renderSizes = {
[SIZE_OPTIONS.SMALL]: parseInt(theme.orbit.heightIconSmall, 10),
[SIZE_OPTIONS.MEDIUM]: parseInt(theme.orbit.heightIconMedium, 10),
[SIZE_OPTIONS.LARGE]: parseInt(theme.orbit.heightIconLarge, 10)
};
return renderSizes[size];
};
const getWidth = ({
theme,
size,
inlineStacked,
totalCarriers
}) => {
if (inlineStacked) return "min-content";
return totalCarriers > 1 ? theme.orbit.widthCarrierLogo : `${getRenderSize({
theme,
size
})}px`;
};
const getCarrierLogoSize = ({
theme,
carriersLength,
size,
inlineStacked
}) => {
const defaultSizes = carriersLength > 1 && !inlineStacked ? getRenderSize({
theme,
size: SIZE_OPTIONS.SMALL
}) : getRenderSize({
theme,
size
});
return `${defaultSizes - (carriersLength > 2 ? 1 : 0)}px`;
};
const getURLSizes = ({
size
}) => {
const urlSizes = {
base: {
[SIZE_OPTIONS.SMALL]: 16,
[SIZE_OPTIONS.MEDIUM]: 32,
[SIZE_OPTIONS.LARGE]: 32
},
retina: {
[SIZE_OPTIONS.SMALL]: 32,
[SIZE_OPTIONS.MEDIUM]: 64,
[SIZE_OPTIONS.LARGE]: 64
}
};
return {
base: urlSizes.base[size],
retina: urlSizes.retina[size]
};
};
const StyledImage = styled.img.attrs(({
carrierType = "airline",
carriersLength,
size,
code,
inlineStacked
}) => {
const urlSizes = carriersLength > 1 && !inlineStacked ? getURLSizes({
size: SIZE_OPTIONS.SMALL
}) : getURLSizes({
size
});
return {
src: `${BASE_URL}/airlines/${urlSizes.base}x${urlSizes.base}/${code}.png?default=${carrierType}.png`,
srcSet: `${BASE_URL}/airlines/${urlSizes.retina}x${urlSizes.retina}/${code}.png?default=${carrierType}.png 2x`
};
}).withConfig({
displayName: "CarrierLogo__StyledImage",
componentId: "sc-1rhi78a-0"
})(["", ""], ({
theme,
rounded,
inlineStacked
}) => css(["background-color:", ";border-radius:", ";height:", ";width:", ";border:", ";&:not(:first-child){margin-", ":", ";}&:last-child{align-self:", ";}"], theme.orbit.backgroundCarrierLogo, rounded ? theme.orbit.borderRadiusCircle : theme.orbit.borderRadiusNormal, getCarrierLogoSize, getCarrierLogoSize, inlineStacked && `1px solid ${theme.orbit.paletteWhite}`, left, inlineStacked && `calc(-1 * ${theme.orbit.spaceXSmall})`, !inlineStacked && "flex-end"));
StyledImage.defaultProps = {
theme: defaultTheme
};
export const StyledCarrierLogo = styled.div.withConfig({
displayName: "CarrierLogo__StyledCarrierLogo",
componentId: "sc-1rhi78a-1"
})(["", ""], ({
theme,
carriers,
size,
inlineStacked
}) => css(["background-color:", ";height:", ";width:", ";display:flex;flex-direction:", ";flex-wrap:", ";align-content:space-between;justify-content:space-between;"], theme.orbit.backgroundCarrierLogo, carriers.length > 1 && !inlineStacked ? theme.orbit.heightCarrierLogo : `${getRenderSize({
theme,
size
})}px`, getWidth({
theme,
size,
inlineStacked,
totalCarriers: carriers.length
}), carriers.length > 1 && !inlineStacked ? "column" : "row", carriers.length > 2 && !inlineStacked && "wrap"));
StyledCarrierLogo.defaultProps = {
theme: defaultTheme
};
const CarrierLogo = ({
size = SIZE_OPTIONS.LARGE,
carriers,
dataTest,
id,
rounded,
inlineStacked = false
}) => {
const randomId = useRandomIdSeed();
return /*#__PURE__*/React.createElement(StyledCarrierLogo, {
carriers: carriers,
size: size,
"data-test": dataTest,
id: id,
inlineStacked: inlineStacked
}, carriers.slice(0, 4).map((carrierImage, idx) => /*#__PURE__*/React.createElement(StyledImage, {
key: randomId(idx.toString()),
rounded: rounded,
carrierType: carrierImage.type,
carriersLength: carriers.length,
code: carrierImage.code,
size: size,
inlineStacked: inlineStacked,
alt: carrierImage.name,
title: carrierImage.name
})));
};
export default CarrierLogo;