Skip to content

Commit

Permalink
Merge pull request #35 from stoplightio/feature/SL-631/image-component
Browse files Browse the repository at this point in the history
SL-631: image component
  • Loading branch information
casserni authored Nov 28, 2018
2 parents de0cd7a + 4ce2422 commit ad8cf71
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 46 deletions.
56 changes: 17 additions & 39 deletions src/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,33 @@
import * as React from 'react';
import { styled } from './utils';

import { borderRadius, height, opacity, styled, width } from './utils';
import { Box, IBoxProps } from './Box';

import { BorderRadius } from './types';

export interface IImageProps {
src: string;
label?: string;
radius?: BorderRadius;
export interface IImageProps extends IBoxProps {
src: HTMLImageElement['src'];
alt?: HTMLImageElement['alt'];
title?: HTMLImageElement['title'];
hidden?: boolean;
opacity?: number;
responsive?: boolean;
height?: string;
width?: string;
}

interface IPlainImageProps extends IImageProps {
className: string;
height?: HTMLImageElement['height'];
width?: HTMLImageElement['width'];
}

const PlainImage = (props: IPlainImageProps) => <img className={props.className} src={props.src} title={props.label} />;

export const Image = styled<IImageProps, 'img'>(PlainImage as any)(
{
// @ts-ignore
display: 'inline',
},

borderRadius,
opacity,
width,
height,

({ hidden }: IImageProps) =>
hidden && {
display: 'none',
},
export const Image = styled<IImageProps, 'img'>(Box).attrs({
as: 'img',
})(
// @ts-ignore
({ hidden }: IImageProps) => hidden && { display: 'none' },

({ responsive }: IImageProps) =>
responsive && {
width: 'auto',
height: 'auto',
maxWidth: '100%',
maxHeight: '100%',
}
);

Image.defaultProps = {
circular: false,
label: '',
height: 'auto',
hidden: false,
opacity: 1,
responsive: true,
rounded: false,
width: 'auto',
responsive: false,
};
14 changes: 7 additions & 7 deletions src/__stories__/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const imageKnobs = (tabName = 'Image'): any => {
radius: select('radius', BorderRadius, '', tabName),
height: text('height', '', tabName),
hidden: boolean('hidden', false, tabName),
responsive: boolean('responsive', true, tabName),
responsive: boolean('responsive', false, tabName),
opacity: number(
'opacity',
1,
Expand All @@ -22,16 +22,16 @@ export const imageKnobs = (tabName = 'Image'): any => {
} as NumberOptions,
tabName
),
src: text(
'src',
'https://s3.amazonaws.com/totem_production/assets/logos/10719/original/logo_light_bg.png?1501094221',
tabName
),
src: text('src', 'https://placehold.it/150x50', tabName),
alt: text('alt', 'Placeholder', tabName),
title: text('title', 'Placeholder', tabName),
width: text('width', '', tabName),
shadow: select('shadow', ['', 'sm', 'md', 'lg'], '', tabName),
};
};

storiesOf('Image', module)
.addDecorator(withKnobs)
.addDecorator(storyFn => <div style={{ width: '300px' }}>{storyFn()}</div>)
.add('with defaults', () => <Image {...imageKnobs()} />);
.add('with defaults', () => <Image {...imageKnobs()} />)
.add('responsive', () => <Image {...imageKnobs()} responsive />);
27 changes: 27 additions & 0 deletions src/__tests__/Image.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { mount } from 'enzyme';
import 'jest-enzyme';
import * as React from 'react';
import { IImageProps, Image } from '../Image';

describe('Image', () => {
let props: IImageProps;

beforeEach(() => {
props = {
title: 'Stoplight.io',
src: 'www.stoplight.io',
};
});

it('attaches title', () => {
const wrapper = mount(<Image {...props} />);
expect(wrapper).toHaveProp('title', props.title);
wrapper.unmount();
});

it('attaches src', () => {
const wrapper = mount(<Image {...props} />);
expect(wrapper).toHaveProp('src', props.src);
wrapper.unmount();
});
});

0 comments on commit ad8cf71

Please sign in to comment.