Skip to content
Snippets Groups Projects
link-box-list.tsx 1.94 KiB
export interface LinkBox {
  heading: string;
  description: string;
  imageUrl: string;
  imageAlt: string;
  boxUrl: string;
  isExternal: boolean;
}

import {Component, ComponentInterface, Watch, Prop, h} from '@stencil/core';
import {LinkBoxVariants} from '../../utils/utils';

@Component({
  tag: 'hy-link-box-list',
  styleUrl: 'link-box-list.scss',
  shadow: true
})
export class LinkBoxList implements ComponentInterface {
  @Prop() variant: LinkBoxVariants = LinkBoxVariants.default;

  private _dataItems: LinkBox[];
  @Prop() dataItems: LinkBox[] | string;

  @Watch('dataItems')
  arrayDataWatcher(newValue: LinkBox[] | string) {
    if (typeof newValue === 'string') {
      this._dataItems = JSON.parse(newValue);
    } else {
      this._dataItems = newValue;
    }
  }
  componentWillLoad() {
    this.arrayDataWatcher(this.dataItems);
  }

  getBoxClassName(count) {
    let className = 'small';

    if (count % 3 == 0 || count == 5) {
      className = 'big';
    } else if (count % 4 == 0 || count == 7) {
      className = 'small';
    }

    return className;
  }

  render() {
    const classAttributes = [this.variant, 'hy-link-box-list'].join(' ');
    /*
    - Logic:
    - 3 items: big
    - 4 items: small
    - 5 items: big
    - 6 items: big
    - 7 items: small
    - 8 items: small
    - 9 items: big
    * */
    const classItem = this.getBoxClassName(this._dataItems.length);
    const classItemAttributes = [this.variant, classItem].join(' ');

    return (
      <div class={classAttributes}>
        {this._dataItems.map((x) => {
          return (
            <hy-link-box
              class={classItemAttributes}
              variant={this.variant}
              image-url={x.imageUrl}
              image-alt={x.imageAlt}
              text-title={x.heading}
              text-description={x.description}
              url={x.boxUrl}
              is-external={x.isExternal}
            />
          );
        })}
      </div>
    );
  }
}