foundation.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* eslint-disable prefer-const */
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. import isEnterPress from '../utils/isEnterPress';
  4. import { BreadcrumbItemInfo, Route } from './itemFoundation';
  5. export interface BreadcrumbAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  6. notifyClick: (itemInfo: BreadcrumbItemInfo, event: any) => void;
  7. expandCollapsed: (clickEvent?: any) => void;
  8. }
  9. export default class BreadcrumbFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<BreadcrumbAdapter<P, S>, P, S> {
  10. constructor(adapter: BreadcrumbAdapter<P, S>) {
  11. super({ ...adapter });
  12. }
  13. handleClick(info: BreadcrumbItemInfo, event: any) {
  14. this._adapter.notifyClick(info, event);
  15. }
  16. handleExpand(clickEvent: any) {
  17. this._adapter.expandCollapsed(clickEvent);
  18. }
  19. /**
  20. * A11y: simulate clear button click
  21. */
  22. handleExpandEnterPress(keyboardEvent: any) {
  23. if (isEnterPress(keyboardEvent)) {
  24. this.handleExpand(keyboardEvent);
  25. }
  26. }
  27. genRoutes(routes: Array<Route>) {
  28. return routes.map(route => {
  29. if (typeof route !== 'object') {
  30. return {
  31. name: route,
  32. _origin: {
  33. name: route
  34. }
  35. };
  36. }
  37. let config: Record<string, any> = {};
  38. config._origin = route;
  39. return { ...config, ...route };
  40. });
  41. }
  42. }