Skip to content
Snippets Groups Projects
hy-general-list.tsx 1.45 KiB
Newer Older
druid's avatar
druid committed
export interface ListItemValue {
  id: string;
  label: string;
  type: string;
  title: string;
  description: string;
  url: string;
}

druid's avatar
druid committed
import {Component, Element, h, Prop, Watch} from '@stencil/core';
druid's avatar
druid committed

@Component({
  tag: 'hy-general-list',
  styleUrl: 'hy-general-list.scss',
  shadow: true,
})
export class HyGeneralList {
  @Prop() dataItems: ListItemValue[] | string;
  private _dataItems: ListItemValue[];
druid's avatar
druid committed
  @Element() el: HTMLElement;
druid's avatar
druid committed

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

  componentWillLoad() {
    this.arrayDataWatcher(this.dataItems);
  }

druid's avatar
druid committed
  handleCardClick(id) {
    let card = this.el.shadowRoot.querySelectorAll('#' + id)[0] as HTMLElement;
    window.open(card.dataset.location, '_blank');
  }

druid's avatar
druid committed
  render() {
    const classAttributes = ['hy-general-list'].join(' ');

    return [
      <div class={classAttributes}>
        {this._dataItems.map((x) => {
druid's avatar
druid committed
          return [
druid's avatar
druid committed
            <hy-general-list-item
              id={x.id}
              item-title={x.title}
              description={x.description}
              label={x.label}
              url={x.url}
              type={x.type}
              data-location={x.url}
              onClick={() => this.handleCardClick(x.id)}
druid's avatar
druid committed
            ></hy-general-list-item>,
          ];
druid's avatar
druid committed
        })}
      </div>,
    ];
  }
}