CHANGED: added conditional render for CardMedia component

This commit is contained in:
doryan 2024-02-03 12:56:49 +04:00
parent 6342db227c
commit 80702813f4
2 changed files with 26 additions and 7 deletions

View File

@ -1,17 +1,33 @@
import Image from 'next/image';
import React, { forwardRef } from 'react';
import { CardMediaProps, CardMediaType } from './card.types';
export const CardMedia = forwardRef<CardMediaType, CardMediaProps>(
(
{ rounded = true, preview = true, type, className = '', ...props },
{
alt,
src,
type,
quality = 80,
rounded = true,
preview = true,
className = '',
...props
},
ref,
) => {
const classes =
`m3 m3-card-media${rounded ? ' m3-rounded' : ''}${preview ? ' m3-preview' : ''} ${className}`.trimEnd();
return React.createElement(type, {
...props,
className: classes,
ref: ref,
});
switch (type) {
case 'img':
return <Image alt={alt} quality={quality} src={src} />;
default:
return React.createElement(type, {
...props,
className: classes,
ref: ref,
});
}
},
);

View File

@ -10,9 +10,12 @@ export interface CardActionAreaProps
export interface CardProps extends ContainerProps {}
export interface CardMediaProps extends HTMLAttributes<CardMediaType> {
type: 'img' | 'video' | 'picture' | 'iframe' | 'audio';
alt: string;
src: string;
quality?: number;
rounded?: boolean;
preview?: boolean;
type: 'img' | 'video' | 'picture' | 'iframe' | 'audio';
}
export type CardMediaType = HTMLImageElement &