NavItem.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { numbers } from './constants';
  2. export const DEFAULT_TOGGLE_ICON = {
  3. open: 'chevron_up',
  4. closed: 'chevron_down',
  5. };
  6. export default class NavItem {
  7. items: any[];
  8. toggleIcon: any;
  9. constructor(options: any = {}) {
  10. // eslint-disable-next-line eqeqeq
  11. if (options == null || typeof options !== 'object') {
  12. // eslint-disable-next-line no-param-reassign
  13. options = {
  14. text: options,
  15. itemKey: options,
  16. maxHeight: numbers.DEFAULT_SUBNAV_MAX_HEIGHT,
  17. // selected: false,
  18. // isOpen: false,
  19. link: null,
  20. items: null,
  21. icon: '',
  22. indent: false,
  23. };
  24. }
  25. for (const key of Object.keys(options)) {
  26. this[key] = options[key];
  27. }
  28. if (options.items && Array.isArray(options.items) && options.items.length) {
  29. this.items = options.items.map((item: any) => new NavItem(item));
  30. if ('toggleIcon' in options) {
  31. this.toggleIcon = NavItem.isValidToggleIcon(options.toggleIcon)
  32. ? { ...options.toggleIcon }
  33. : { ...DEFAULT_TOGGLE_ICON };
  34. } else {
  35. this.toggleIcon = { ...DEFAULT_TOGGLE_ICON };
  36. }
  37. } else {
  38. this.items = null;
  39. }
  40. }
  41. static isValidToggleIcon(toggleIcon: any) {
  42. return Boolean(toggleIcon &&
  43. typeof toggleIcon === 'object' &&
  44. typeof toggleIcon.open === 'string' &&
  45. toggleIcon.open.length &&
  46. typeof toggleIcon.closed === 'string' &&
  47. toggleIcon.closed.length);
  48. }
  49. }