adapters.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import _ from 'lodash-es';
  2. import { ModuleData, ShowStyle } from './types';
  3. import { ModulePositionMap, ModuleSizeMap } from './options';
  4. /** 将原本的 formData 格式化为 api.saveOrUpdate 需要的格式 */
  5. export function parseFormDataToParams(formData: Record<string, number | string | undefined>) {
  6. const params = {};
  7. _.forEach(formData, (v: string | undefined, k) => {
  8. if (!v) return;
  9. return _.set(params, k, v);
  10. });
  11. return params;
  12. }
  13. /** 将 api.list 返回的数据格式化,格式化之后可以支持对应的表单以用,该方法会修改源数据 */
  14. export function parseModuleData(listData: { moduleData: ModuleData; showStyle: ShowStyle }) {
  15. _.forEach(listData.showStyle, (v, k) => {
  16. listData[`showStyle.${k}`] = _.get(
  17. {
  18. ...ModuleSizeMap,
  19. ...ModulePositionMap,
  20. },
  21. v,
  22. v
  23. );
  24. });
  25. return listData;
  26. }
  27. // export function parseModuleDataToConfig(moduleData: ModuleData): {
  28. // chart: { label: string; prop: string }[];
  29. // list: { label: string; prop: string }[];
  30. // };
  31. // export function parseModuleDataToConfig(
  32. // moduleData: ModuleData,
  33. // data: any
  34. // ): {
  35. // chart: { label: string; value: string }[];
  36. // list: { label: string; value: string }[];
  37. // };
  38. /** 将 api.list 返回的 moduleData 格式化为配置对象,如果传入了 data,那么会返回带数据的配置,否则返回默认配置,这些配置通常可用于页面展示 */
  39. // export function parseModuleDataToConfig(moduleData: ModuleData): {
  40. // chart: { label: string; prop: string }[];
  41. // list: { label: string; prop: string }[];
  42. // } {
  43. // // if (data) {
  44. // // return {
  45. // // chart: _.map(_.get(moduleData, 'chart', []), (label, prop) => {
  46. // // return { label, value: _.get(data, prop, '/') };
  47. // // }),
  48. // // list: _.map(_.get(moduleData, 'list', []), (label, prop) => {
  49. // // return { label, value: _.get(data, prop, '/') };
  50. // // }),
  51. // // };
  52. // // }
  53. // return {
  54. // chart: _.map(_.get(moduleData, 'chart', []), (label, prop) => {
  55. // return { label, prop };
  56. // }),
  57. // list: _.map(_.get(moduleData, 'list', []), (label, prop) => {
  58. // return { label, prop };
  59. // }),
  60. // };
  61. // }
  62. /** 根据配置中的 formatter 将文本格式并返回 */
  63. export function getFormattedText(data: any, prop: string, formatter?: string): string {
  64. if (formatter) {
  65. return formatter.replace(/\$\{\}/g, _.get(data, prop, '-'));
  66. }
  67. return _.get(data, prop, '-');
  68. }