Skip to content
Snippets Groups Projects
hy-general-list.tsx 1.97 KiB
Newer Older
druid's avatar
druid committed
export interface ListItemValue {
  description: string;
druid's avatar
druid committed
  id: string;
  imageAlt: string;
  imageUrl: string;
druid's avatar
druid committed
  label: string;
Tuukka Turu's avatar
Tuukka Turu committed
  subLabel: string;
  date: string;
  tag: string;
druid's avatar
druid committed
  title: string;
  type: string;
druid's avatar
druid committed
  url: string;
Tuukka Turu's avatar
Tuukka Turu committed
  target: boolean;
druid's avatar
druid committed
}

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

@Component({
  tag: 'hy-general-list',
  styleUrl: 'hy-general-list.scss',
  shadow: true,
})
export class HyGeneralList {
Tuukka Turu's avatar
Tuukka Turu committed
  @Prop() type: GeneralListTypes = GeneralListTypes.list;
  @Prop() blank: boolean = true;
druid's avatar
druid committed
  @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);
  }

  render() {
Tuukka Turu's avatar
Tuukka Turu committed
    const classAttributes = ['hy-general-list', `hy-general-list--style-${this.type}`].join(' ');
druid's avatar
druid committed

Tuukka Turu's avatar
Tuukka Turu committed
    let listData = this._dataItems;
    if (typeof this._dataItems === 'object') {
      listData = Object.values(this._dataItems);
    }
    return (
      <ul class={classAttributes}>
Tuukka Turu's avatar
Tuukka Turu committed
        {listData &&
          listData.map((item) => (
            <li>
              <hy-general-list-item
                target={this.blank}
                data-location={item.url}
                description={item.description}
                id={item.id}
                imageAlt={item.imageAlt}
                imageUrl={item.imageUrl}
                item-title={item.title}
                label={item.label}
                sub-label={item.subLabel}
                tag={item.tag}
                date={item.date}
                type={item.type}
                url={item.url}
                list-style={this.type}
              ></hy-general-list-item>
            </li>
          ))}
druid's avatar
druid committed
  }
}