Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiCard] Allow custom component for image prop #3370

Merged
merged 11 commits into from
Apr 23, 2020
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `23.0.0`.
- Added `ReactElement` to `EuiCard` `image` property to allow custom component
cchaos marked this conversation as resolved.
Show resolved Hide resolved

## [`23.0.0`](https://github.com/elastic/eui/tree/v23.0.0)

Expand Down
5 changes: 5 additions & 0 deletions src-docs/src/views/card/card_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ export const CardExample = {
<span>
Make sure that all images are the{' '}
<strong>same proportions</strong> when used in a singular row.
<br />
If custom component is passed to <EuiCode>image</EuiCode> prop
TAYTS marked this conversation as resolved.
Show resolved Hide resolved
and it consists solely of inline elements and no{' '}
<EuiCode>{'<img />'}</EuiCode> elements, please make sure to
provide <EuiCode>{'{ width: 100%; }'}</EuiCode> styling.
</span>
}
/>
Expand Down
8 changes: 8 additions & 0 deletions src/components/card/_card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
.euiCard__image {
// Without a border, the image should stand on it's own so it needs to have all corners rounded
border-radius: $euiBorderRadius;

& img {
width: 100%;
}
TAYTS marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -149,6 +153,10 @@
border-top-left-radius: $euiBorderRadius - 1px;
border-top-right-radius: $euiBorderRadius - 1px;

& img {
width: 100%;
}
TAYTS marked this conversation as resolved.
Show resolved Hide resolved

// IF both exist, position the icon centered on top of image
+ .euiCard__icon {
position: absolute;
Expand Down
21 changes: 17 additions & 4 deletions src/components/card/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
* under the License.
*/

import React, { FunctionComponent, ReactElement, ReactNode } from 'react';
import React, {
FunctionComponent,
ReactElement,
ReactNode,
isValidElement,
} from 'react';
import classNames from 'classnames';

import { CommonProps, keysOf } from '../common';
Expand Down Expand Up @@ -89,9 +94,9 @@ type EuiCardProps = Omit<CommonProps, 'aria-label'> & {
icon?: ReactElement<EuiIconProps>;

/**
* Accepts a url in string form
* Accepts a url in string form or ReactElement for custom image component
cchaos marked this conversation as resolved.
Show resolved Hide resolved
*/
image?: string;
image?: string | ReactElement;

/**
* Content to be rendered between the description and the footer
Expand Down Expand Up @@ -229,7 +234,15 @@ export const EuiCard: FunctionComponent<EuiCardProps> = ({

let imageNode;
if (image && layout === 'vertical') {
imageNode = <img className="euiCard__image" src={image} alt="" />;
if (isValidElement(image) || typeof image === 'string') {
TAYTS marked this conversation as resolved.
Show resolved Hide resolved
imageNode = (
<div className="euiCard__image">
{isValidElement(image) ? image : <img src={image} alt="" />}
</div>
);
} else {
imageNode = null;
}
}

let iconNode;
Expand Down