Skip to content

Commit

Permalink
add basic ProductConfigurator component with typings and productpreview
Browse files Browse the repository at this point in the history
  • Loading branch information
mbpictures committed Sep 29, 2020
1 parent fd8b20e commit cee20b4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/components/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as React from "react";
import { Item } from "..";
import style from "../styles/ProductPreview.scss";

interface props {
currentSelection: { [keys: string]: Item };
}

export class ProductPreview extends React.Component<props, any> {
render() {
const selection: { [keys: string]: Item } = this.props.currentSelection;
const images = Object.keys(selection)
.sort(
(a: string, b: string) =>
(selection[a].layer ?? 0) - (selection[b].layer ?? 0)
)
.map((key: string) => {
return (
<img
className={style.images}
key={`preview-image-${key}`}
src={selection[key].image}
alt={selection[key].name}
/>
);
});

return <div className={style.preview}>{images}</div>;
}
}
57 changes: 51 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
import * as React from 'react'
import styles from './styles.module.css'
import * as React from "react";
import { ProductPreview } from "./components/preview";

interface Props {
text: string
export type ItemConfiguration = { [keys: string]: string | number };

export interface Item {
name: string;
price: number;
image: string;
thumbnail?: string;
color?: string;
default?: boolean;
layer?: number;
}

export interface Category {
name: string;
thumbnail: string;
layer?: number;
items: Item[];
}

interface props {
name: string;
onBack?: () => any;
onBuy?: (items: ItemConfiguration[]) => any;
categories: Category[];
}

interface state {
currentSelection: { [keys: string]: Item };
}

export const ExampleComponent = ({ text }: Props) => {
return <div className={styles.test}>Example Component: {text}</div>
export class ProductConfigurator extends React.Component<props, state> {
constructor(props: props) {
super(props);
const currentSelection = {};
this.props.categories.forEach((val) => {
val.items.forEach((item) => {
if (!item.default) return;
if (!item.layer && val.layer) item.layer = val.layer;
currentSelection[val.name] = item;
});
});
this.state = {
currentSelection: currentSelection,
};
}

render() {
return (
<ProductPreview currentSelection={this.state.currentSelection} />
);
}
}
16 changes: 16 additions & 0 deletions src/styles/ProductPreview.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.preview {
height: 100%;
flex: 2;
position: relative;

.images {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: grid;
max-width: 100%;
max-height: 100%;
transition: .4s ease;
}
}

0 comments on commit cee20b4

Please sign in to comment.