@plasmicpkgs/plasmic-rich-components 1.0.50 → 1.0.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import React, { useState, useEffect, useRef } from 'react';
1
+ import React, { useState, useEffect, useRef, useMemo } from 'react';
2
2
  import registerComponent from '@plasmicapp/host/registerComponent';
3
3
  import '@plasmicapp/host/registerGlobalContext';
4
4
  import { LogoutOutlined, PlusOutlined, EllipsisOutlined } from '@ant-design/icons';
@@ -64,37 +64,6 @@ function _objectWithoutPropertiesLoose(source, excluded) {
64
64
  }
65
65
  return target;
66
66
  }
67
- function _unsupportedIterableToArray(o, minLen) {
68
- if (!o) return;
69
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
70
- var n = Object.prototype.toString.call(o).slice(8, -1);
71
- if (n === "Object" && o.constructor) n = o.constructor.name;
72
- if (n === "Map" || n === "Set") return Array.from(o);
73
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
74
- }
75
- function _arrayLikeToArray(arr, len) {
76
- if (len == null || len > arr.length) len = arr.length;
77
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
78
- return arr2;
79
- }
80
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
81
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
82
- if (it) return (it = it.call(o)).next.bind(it);
83
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
84
- if (it) o = it;
85
- var i = 0;
86
- return function () {
87
- if (i >= o.length) return {
88
- done: true
89
- };
90
- return {
91
- done: false,
92
- value: o[i++]
93
- };
94
- };
95
- }
96
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
97
- }
98
67
 
99
68
  function useIsClient() {
100
69
  var _useState = useState(false),
@@ -1090,14 +1059,6 @@ var defaultColumnConfig = function defaultColumnConfig() {
1090
1059
  };
1091
1060
  function deriveFieldConfigs(specifiedFieldsPartial, schema) {
1092
1061
  var _schema$fields;
1093
- // Ugly: when adding a new item to an array from Plasmic Studio UI, no way to specify default values for the new item,
1094
- // so we have to do it here. The only one we need is the random key.
1095
- for (var _iterator = _createForOfIteratorHelperLoose(specifiedFieldsPartial), _step; !(_step = _iterator()).done;) {
1096
- var field = _step.value;
1097
- if (!field.key) {
1098
- field.key = mkShortId();
1099
- }
1100
- }
1101
1062
  var schemaFields = (_schema$fields = schema == null ? void 0 : schema.fields) != null ? _schema$fields : [];
1102
1063
  var fieldById = mkIdMap(schemaFields);
1103
1064
  var specifiedFieldIds = new Set(withoutNils(specifiedFieldsPartial.map(function (f) {
@@ -1105,6 +1066,7 @@ function deriveFieldConfigs(specifiedFieldsPartial, schema) {
1105
1066
  })));
1106
1067
  function defaultColumnConfigForField(field) {
1107
1068
  return _extends({}, defaultColumnConfig(), {
1069
+ key: field.id,
1108
1070
  fieldId: field.id,
1109
1071
  title: field.label || field.id,
1110
1072
  expr: function expr(currentItem) {
@@ -1112,10 +1074,12 @@ function deriveFieldConfigs(specifiedFieldsPartial, schema) {
1112
1074
  }
1113
1075
  });
1114
1076
  }
1115
- var keptSpecifiedFields = specifiedFieldsPartial.flatMap(function (f) {
1077
+ var keptSpecifiedFields = specifiedFieldsPartial.flatMap(function (f, index) {
1116
1078
  var fieldId = f.fieldId;
1117
1079
  if (!fieldId) {
1118
- return [_extends({}, defaultColumnConfig(), f)];
1080
+ return [_extends({}, defaultColumnConfig(), {
1081
+ key: index
1082
+ }, f)];
1119
1083
  }
1120
1084
  var field = fieldById.get(fieldId);
1121
1085
  // Drop configs with fieldIds no longer in the data.
@@ -1209,8 +1173,13 @@ function normalizeData(rawData) {
1209
1173
  schema: schema
1210
1174
  });
1211
1175
  }
1176
+ /**
1177
+ * Render booleans, objects, arrays, etc. as JSON repr.
1178
+ */
1179
+ function safeRender(x) {
1180
+ return x === undefined || x === null ? "" : typeof x === "string" ? x : typeof x === "number" ? x.toString() : JSON.stringify(x);
1181
+ }
1212
1182
  function RichTable(props) {
1213
- var _data$data;
1214
1183
  var className = props.className,
1215
1184
  _props$data = props.data,
1216
1185
  rawData = _props$data === void 0 ? {
@@ -1224,6 +1193,8 @@ function RichTable(props) {
1224
1193
  }]
1225
1194
  }
1226
1195
  } : _props$data,
1196
+ _props$pagination = props.pagination,
1197
+ pagination = _props$pagination === void 0 ? true : _props$pagination,
1227
1198
  defaultSize = props.defaultSize,
1228
1199
  fields = props.fields,
1229
1200
  setControlContextData = props.setControlContextData,
@@ -1231,10 +1202,12 @@ function RichTable(props) {
1231
1202
  addHref = props.addHref,
1232
1203
  actions = props.actions,
1233
1204
  customActionChildren = props.customActionChildren,
1234
- _props$showSearch = props.showSearch,
1235
- showSearch = _props$showSearch === void 0 ? true : _props$showSearch,
1236
1205
  _props$pageSize = props.pageSize,
1237
- pageSize = _props$pageSize === void 0 ? 20 : _props$pageSize;
1206
+ pageSize = _props$pageSize === void 0 ? 20 : _props$pageSize,
1207
+ hideSearch = props.hideSearch,
1208
+ hideDensity = props.hideDensity,
1209
+ hideColumnPicker = props.hideColumnPicker,
1210
+ hideExports = props.hideExports;
1238
1211
  var data = normalizeData(rawData);
1239
1212
  var _useState = useState(""),
1240
1213
  search = _useState[0],
@@ -1291,7 +1264,7 @@ function RichTable(props) {
1291
1264
  }, React.createElement(DataProvider, {
1292
1265
  name: "currentColumn",
1293
1266
  data: value
1294
- }, cconfig.expr ? cconfig.expr(record) : record)));
1267
+ }, safeRender(cconfig.expr ? cconfig.expr(record) : value))));
1295
1268
  }
1296
1269
  };
1297
1270
  return columnDefinition;
@@ -1341,50 +1314,76 @@ function RichTable(props) {
1341
1314
  normalized: normalized,
1342
1315
  columnDefinitions: columnDefinitions
1343
1316
  };
1344
- }, [fields, data, setControlContextData, Math.random()]),
1345
- columnDefinitions = _React$useMemo.columnDefinitions;
1317
+ }, [fields, data, setControlContextData]),
1318
+ columnDefinitions = _React$useMemo.columnDefinitions,
1319
+ normalized = _React$useMemo.normalized;
1346
1320
  var actionRef = useRef();
1321
+ var _useState2 = useState(undefined),
1322
+ state = _useState2[0],
1323
+ setState = _useState2[1];
1324
+ var finalData = useMemo(function () {
1325
+ var _data$data;
1326
+ var filtered = data == null ? void 0 : (_data$data = data.data) == null ? void 0 : _data$data.filter(function (row) {
1327
+ return fastStringify(Object.values(row)).toLowerCase().includes(search);
1328
+ });
1329
+ var sorted = state != null && state.sorter.column ?
1330
+ // We use .sort() rather than sortBy to use localeCompare
1331
+ function () {
1332
+ var _normalized$find$expr;
1333
+ var expr = (_normalized$find$expr = normalized.find(function (cconfig) {
1334
+ var _state$sorter$column;
1335
+ return cconfig.key === (state == null ? void 0 : (_state$sorter$column = state.sorter.column) == null ? void 0 : _state$sorter$column.key);
1336
+ }).expr) != null ? _normalized$find$expr : function (x) {
1337
+ return x;
1338
+ };
1339
+ return (filtered != null ? filtered : []).sort(function (aa, bb) {
1340
+ var _expr, _expr2;
1341
+ var a = (_expr = expr(aa)) != null ? _expr : null,
1342
+ b = (_expr2 = expr(bb)) != null ? _expr2 : null;
1343
+ // Default nil to '' here because A < null < z which is weird.
1344
+ return typeof a === "string" ? a.localeCompare(b != null ? b : "") : typeof b === "string" ? -b.localeCompare(a != null ? a : "") : a - b;
1345
+ });
1346
+ }() : filtered;
1347
+ var reversed = (state == null ? void 0 : state.sorter.order) === "descend" ? sorted == null ? void 0 : sorted.reverse() : sorted;
1348
+ return reversed;
1349
+ }, [data, normalized, state, search]);
1347
1350
  var isClient = useIsClient();
1348
1351
  if (!isClient) {
1349
1352
  return null;
1350
1353
  }
1351
- // return <NestedTest {...{ fields }} />;
1352
1354
  return React.createElement(React.Fragment, null, React.createElement(ProTable, {
1353
1355
  actionRef: actionRef,
1354
1356
  className: className,
1355
1357
  columns: columnDefinitions,
1356
- dataSource: data == null ? void 0 : (_data$data = data.data) == null ? void 0 : _data$data.filter(function (row) {
1357
- return fastStringify(Object.values(row)).toLowerCase().includes(search);
1358
- }),
1358
+ onChange: function onChange(_pagination, _filters, sorter, _extra) {
1359
+ setState({
1360
+ sorter: sorter
1361
+ });
1362
+ },
1363
+ dataSource: finalData,
1359
1364
  rowKey: "id",
1360
1365
  defaultSize: defaultSize,
1361
- // request={() =>
1362
- // Promise.resolve({
1363
- // data: data?.data?.filter((row) =>
1364
- // fastStringify(row).toLowerCase().includes(search)
1365
- // ),
1366
- // success: true,
1367
- // })
1368
- // }
1369
1366
  editable: {
1370
1367
  type: "multiple"
1371
1368
  },
1372
1369
  search: false,
1373
1370
  options: {
1374
- setting: {
1371
+ setting: hideColumnPicker ? false : {
1375
1372
  listsHeight: 400
1376
- }
1373
+ },
1374
+ reload: false,
1375
+ density: !hideDensity
1377
1376
  },
1378
- pagination: {
1377
+ pagination: pagination ? {
1379
1378
  pageSize: pageSize,
1380
1379
  onChange: function onChange(page) {
1381
1380
  return console.log(page);
1382
1381
  }
1383
- },
1382
+ } : false,
1384
1383
  dateFormatter: "string",
1385
1384
  headerTitle: title,
1386
1385
  toolbar: {
1387
- search: showSearch ? {
1386
+ search: !hideSearch ? {
1388
1387
  value: search,
1389
1388
  onChange: function onChange(e) {
1390
1389
  return setSearch(e.target.value);
@@ -1399,7 +1398,7 @@ function RichTable(props) {
1399
1398
  icon: React.createElement(PlusOutlined, null),
1400
1399
  type: "primary",
1401
1400
  href: addHref
1402
- }, "Add"), React.createElement(Dropdown, {
1401
+ }, "Add"), !hideExports && React.createElement(Dropdown, {
1403
1402
  key: "menu",
1404
1403
  menu: {
1405
1404
  items: [{
@@ -1514,17 +1513,15 @@ var dataTableMeta = {
1514
1513
  options: ["large", "middle", "small"],
1515
1514
  defaultValueHint: "large"
1516
1515
  },
1517
- // TODO
1518
- // pagination: {
1519
- // type: "boolean",
1520
- // defaultValueHint: true,
1521
- // },
1522
1516
  pageSize: {
1523
1517
  type: "number",
1524
1518
  defaultValueHint: 20
1525
1519
  },
1526
1520
  fields: {
1527
1521
  type: "array",
1522
+ hidden: function hidden(ps) {
1523
+ return !ps.data;
1524
+ },
1528
1525
  unstable__keyFunc: function unstable__keyFunc(x) {
1529
1526
  return x.key;
1530
1527
  },
@@ -1594,6 +1591,27 @@ var dataTableMeta = {
1594
1591
  }
1595
1592
  }
1596
1593
  }
1594
+ },
1595
+ pagination: {
1596
+ type: "boolean",
1597
+ advanced: true,
1598
+ defaultValueHint: true
1599
+ },
1600
+ hideSearch: {
1601
+ type: "boolean",
1602
+ advanced: true
1603
+ },
1604
+ hideExports: {
1605
+ type: "boolean",
1606
+ advanced: true
1607
+ },
1608
+ hideDensity: {
1609
+ type: "boolean",
1610
+ advanced: true
1611
+ },
1612
+ hideColumnPicker: {
1613
+ type: "boolean",
1614
+ advanced: true
1597
1615
  }
1598
1616
  },
1599
1617
  importName: "RichTable",