@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.
119 lines (111 loc) • 3.5 kB
JavaScript
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../defaultTheme";
import { SIZE_OPTIONS, BASE_URL } from "./consts";
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 getCarrierLogoSize = ({
theme,
carriersLength,
size
}) => {
const defaultSizes = carriersLength > 1 ? 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
}) => {
const urlSizes = carriersLength > 1 ? 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-1fb3c06-0"
})(["", ""], ({
theme,
rounded
}) => css(["background-color:", ";border-radius:", ";height:", ";width:", ";&:last-child{align-self:flex-end;}"], theme.orbit.backgroundCarrierLogo, rounded ? theme.orbit.borderRadiusCircle : theme.orbit.borderRadiusNormal, getCarrierLogoSize, getCarrierLogoSize)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledImage.defaultProps = {
theme: defaultTheme
};
export const StyledCarrierLogo = styled.div.withConfig({
displayName: "CarrierLogo__StyledCarrierLogo",
componentId: "sc-1fb3c06-1"
})(["", ""], ({
theme,
carriers,
size
}) => 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 ? theme.orbit.heightIconLarge : `${getRenderSize({
theme,
size
})}px`, carriers.length > 1 ? theme.orbit.widthIconLarge : `${getRenderSize({
theme,
size
})}px`, carriers.length > 1 ? "column" : "row", carriers.length > 2 && "wrap")); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledCarrierLogo.defaultProps = {
theme: defaultTheme
};
const CarrierLogo = ({
size = SIZE_OPTIONS.LARGE,
carriers,
dataTest,
rounded
}) => /*#__PURE__*/React.createElement(StyledCarrierLogo, {
carriers: carriers,
size: size,
"data-test": dataTest
}, carriers.slice(0, 4).map(carrierImage => /*#__PURE__*/React.createElement(StyledImage, {
key: carrierImage.code,
rounded: rounded,
carrierType: carrierImage.type,
carriersLength: carriers.length,
code: carrierImage.code,
size: size,
alt: carrierImage.name,
title: carrierImage.name
})));
export default CarrierLogo;