Skip to content

Commit

Permalink
[New Component] Aspect Ratio component (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
GoldGroove06 authored Dec 20, 2024
1 parent a0efb52 commit f638e36
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/components/ui/AspectRatio/AspectRatio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use client';
import React from 'react';
import { customClassSwitcher } from '~/core';
import { clsx } from 'clsx';
const COMPONENT_NAME = 'AspectRatio';

export type AspectRatioProps = {
children: React.ReactNode;
customRootClass?: string;
className?: string;
ratio?: string;
props: Record<string, any>[];
}

const AspectRatio = ({ children, customRootClass, className, ratio="1", ...props }: AspectRatioProps) => {

if (isNaN(Number(ratio)) && !ratio.match(/^(\d+)\/(\d+)$/)) ratio = "1"
if (Number(ratio) <= 0) ratio = "1"

const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
return <div style={{aspectRatio:ratio}} className={clsx(rootClass, className)} {...props}>{children} </div>
}
AspectRatio.displayName = COMPONENT_NAME;

export default AspectRatio;
25 changes: 25 additions & 0 deletions src/components/ui/AspectRatio/stories/AspectRatio.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import AspectRatio from '../AspectRatio';
import SandboxEditor from '~/components/tools/SandboxEditor/SandboxEditor';

// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
export default {
title: 'Components/AspectRatio',
component: AspectRatio,
render: (args) => <SandboxEditor>
<AspectRatio {...args} >
<img
className="Image"
src="https://images.pexels.com/photos/346529/pexels-photo-346529.jpeg?cs=srgb&dl=pexels-bri-schneiter-28802-346529.jpg&fm=jpg"
alt="Landscape photograph"
/>
</AspectRatio>
</SandboxEditor>
};

// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args
export const Default = {
args: {
className:"",
ratio: "16/9"
}
};
43 changes: 43 additions & 0 deletions src/components/ui/AspectRatio/tests/AspectRatio.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import AspectRatio from '../AspectRatio';

describe('AspectRatio', () => {
test('renders AspectRatio component', () => {
render(<AspectRatio>Content</AspectRatio>);
expect(screen.getByText('Content')).toBeInTheDocument();
});

test('applies custom classes correctly', () => {
render(<AspectRatio className="additional-class">Content</AspectRatio>);
const divElement = screen.getByText('Content');
expect(divElement).toHaveClass('additional-class');
});

test('applies correct aspect ratio', () => {
render(<AspectRatio ratio="16/9">Content</AspectRatio>);
expect(screen.getByText('Content').style.aspectRatio).toBe('16/9');
});

test('applies correct aspect ratio when ratio is not provided', () => {
render(<AspectRatio >Content</AspectRatio>);
expect(screen.getByText('Content').style.aspectRatio).toBe('1');
});

test('applies correct aspect ratio when ratio is invalid', () => {
render(<AspectRatio ratio="invalid">Content</AspectRatio>);
expect(screen.getByText('Content').style.aspectRatio).toBe('1');
});

test('applies correct aspect ratio when ratio contains a character', () => {
render(<AspectRatio ratio="16/o">Content</AspectRatio>);
expect(screen.getByText('Content').style.aspectRatio).toBe('1');
});

test('applies correct aspect ratio when ratio is negative', () => {
render(<AspectRatio ratio="-5">Content</AspectRatio>);
expect(screen.getByText('Content').style.aspectRatio).toBe('1');
});


});

0 comments on commit f638e36

Please sign in to comment.