@kne/form-creator 0.1.28 → 0.1.30

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.
@@ -7871,8 +7871,26 @@ const FocusStepsActions = ({
7871
7871
  }
7872
7872
  setLoading(true);
7873
7873
  try {
7874
- if (await validateFieldsByName(openApi, getStepFieldNames(items[current]))) {
7874
+ var _openApi$getField;
7875
+ const names = getStepFieldNames(items[current]);
7876
+ // validateFieldsByName 失败时会滚到首个错字段(同提交 ScrollToError);旧版 form-info 无此行为时下方兜底
7877
+ if (await validateFieldsByName(openApi, names)) {
7875
7878
  onChange(current + 1);
7879
+ return;
7880
+ }
7881
+ const firstInvalid = names.find(name => {
7882
+ const field = openApi == null || openApi.getField == null ? void 0 : openApi.getField({
7883
+ name
7884
+ });
7885
+ return field && field.isPass === false;
7886
+ });
7887
+ const el = firstInvalid ? openApi == null || openApi.getField == null || (_openApi$getField = openApi.getField({
7888
+ name: firstInvalid
7889
+ })) == null || (_openApi$getField = _openApi$getField.fieldRef) == null ? void 0 : _openApi$getField.current : null;
7890
+ if (el && typeof el.scrollIntoView === 'function') {
7891
+ requestAnimationFrame(() => {
7892
+ el.scrollIntoView();
7893
+ });
7876
7894
  }
7877
7895
  } finally {
7878
7896
  setLoading(false);
@@ -7926,10 +7944,17 @@ const FocusSteps = _ref => {
7926
7944
  stepsClassName
7927
7945
  } = _ref,
7928
7946
  props = _objectWithoutPropertiesLoose(_ref, _excluded$2);
7947
+ const {
7948
+ openApi
7949
+ } = useFormContext() || {};
7929
7950
  const [current, setCurrent] = useState(0);
7930
7951
  const headerRef = useRef(null);
7931
7952
  const mountedRef = useRef(false);
7932
7953
  const safeCurrent = Math.min(current, Math.max(items.length - 1, 0));
7954
+ const currentRef = useRef(safeCurrent);
7955
+ currentRef.current = safeCurrent;
7956
+ const itemsRef = useRef(items);
7957
+ itemsRef.current = items;
7933
7958
  const stepTitle = index => {
7934
7959
  var _items$index;
7935
7960
  return ((_items$index = items[index]) == null ? void 0 : _items$index.title) || formatMessage({
@@ -7951,6 +7976,38 @@ const FocusSteps = _ref => {
7951
7976
  });
7952
7977
  }
7953
7978
  }, [safeCurrent]);
7979
+
7980
+ // 提交失败时:若当前步字段有错则强制留在当前步,避免误跳到后续错步
7981
+ useEffect(() => {
7982
+ const emitter = openApi == null ? void 0 : openApi.emitter;
7983
+ if (!(emitter != null && emitter.addListener)) {
7984
+ return undefined;
7985
+ }
7986
+ const stayIfCurrentHasError = errors => {
7987
+ const step = itemsRef.current[currentRef.current];
7988
+ const names = new Set(getStepFieldNames(step));
7989
+ if (!names.size) {
7990
+ return;
7991
+ }
7992
+ const currentHasError = (errors || []).some(item => names.has(item == null ? void 0 : item.name) || names.has(item == null ? void 0 : item.groupName));
7993
+ if (currentHasError) {
7994
+ setCurrent(currentRef.current);
7995
+ }
7996
+ };
7997
+ const errorSub = emitter.addListener('form:submit:error', stayIfCurrentHasError);
7998
+ const completeSub = emitter.addListener('form:submit:complete', ({
7999
+ isPass,
8000
+ errors
8001
+ } = {}) => {
8002
+ if (!isPass) {
8003
+ stayIfCurrentHasError(errors);
8004
+ }
8005
+ });
8006
+ return () => {
8007
+ errorSub == null || errorSub.remove == null || errorSub.remove();
8008
+ completeSub == null || completeSub.remove == null || completeSub.remove();
8009
+ };
8010
+ }, [openApi]);
7954
8011
  const focusItems = items.map((item, index) => {
7955
8012
  if (index !== safeCurrent) {
7956
8013
  return item;
@@ -7970,7 +8027,8 @@ const FocusSteps = _ref => {
7970
8027
  children: [item.children != null ? item.children : /*#__PURE__*/jsx(FormInfo, {
7971
8028
  column: item.column,
7972
8029
  gap: item.gap,
7973
- list: item.list || []
8030
+ list: item.list || [],
8031
+ className: item.className
7974
8032
  }), /*#__PURE__*/jsx(FocusStepsActions, {
7975
8033
  items: items,
7976
8034
  current: safeCurrent,
@@ -8393,6 +8451,8 @@ const renderBlockElement = (block, preview = false, ctx = {}) => {
8393
8451
  case 'steps':
8394
8452
  {
8395
8453
  const isFocusSteps = block.stepsStyle === 'focus';
8454
+ // 与 formInfo 一致:block.showIndex(或 step 上覆盖)时给题干加序号
8455
+ const blockShowIndex = !!block.showIndex;
8396
8456
  return withBlockLayout(/*#__PURE__*/createElement(isFocusSteps ? FocusSteps$1 : Steps, _extends({
8397
8457
  key: block.id,
8398
8458
  title: block.title || undefined,
@@ -8405,10 +8465,14 @@ const renderBlockElement = (block, preview = false, ctx = {}) => {
8405
8465
  prevIcon: /*#__PURE__*/createElement(ArrowLeftOutlined),
8406
8466
  nextIcon: /*#__PURE__*/createElement(ArrowRightOutlined),
8407
8467
  items: (block.items || []).map(step => {
8408
- const entries = renderFieldEntries(step.list, preview, fieldRenderOpts);
8468
+ const showIndex = step.showIndex != null ? !!step.showIndex : blockShowIndex;
8469
+ const entries = renderFieldEntries(step.list, preview, _extends({}, fieldRenderOpts, {
8470
+ showIndex
8471
+ }));
8409
8472
  const nested = renderChildBlocks(step.blocks || [], nextCtx);
8410
8473
  const fieldNames = [...(step.list || []).filter(field => field && !field.hidden && field.name).map(field => field.name), ...collectBlockFieldNames(step.blocks || [])];
8411
8474
  const hasConditional = entries.some(entry => entry.visibleWhen);
8475
+ const indexClassName = showIndex ? style['form-info-show-index'] : null;
8412
8476
  if (nested.length || hasConditional) {
8413
8477
  return {
8414
8478
  key: step.id,
@@ -8421,7 +8485,8 @@ const renderBlockElement = (block, preview = false, ctx = {}) => {
8421
8485
  }, entries.length ? createFieldsFormInfo({
8422
8486
  key: `${step.id}-fields`,
8423
8487
  column: step.column,
8424
- gap: block.gap
8488
+ gap: block.gap,
8489
+ className: indexClassName
8425
8490
  }, entries, [], knownNames) : null, ...nested)
8426
8491
  };
8427
8492
  }
@@ -8431,6 +8496,7 @@ const renderBlockElement = (block, preview = false, ctx = {}) => {
8431
8496
  column: step.column,
8432
8497
  gap: block.gap,
8433
8498
  list: entries.map(entry => entry.element),
8499
+ className: indexClassName,
8434
8500
  fieldNames
8435
8501
  };
8436
8502
  })
@@ -10200,6 +10266,41 @@ const renderFieldsContent = (fields, data, formatMessage, column, {
10200
10266
  });
10201
10267
  };
10202
10268
  const renderBlockList = (blocks, data, ctx) => (blocks || []).map(block => renderBlock(block, data, ctx)).filter(Boolean);
10269
+
10270
+ /** 列表项标题:优先 itemTitle,其次首个带标题的 itemBlock(与填写态 List 常见结构对齐),避免只显示「第 N 项」 */
10271
+ const resolveListItemTitle = (block, item, index, formatMessage) => {
10272
+ if (typeof block.itemTitle === 'function') {
10273
+ try {
10274
+ const title = block.itemTitle({
10275
+ index,
10276
+ data: item
10277
+ });
10278
+ if (title != null && title !== '') {
10279
+ return {
10280
+ title,
10281
+ titleFromItemBlockId: null
10282
+ };
10283
+ }
10284
+ } catch (e) {
10285
+ // ignore bad itemTitle
10286
+ }
10287
+ }
10288
+ const head = (block.itemBlocks || []).find(child => child && (child.title || child.label));
10289
+ if (head) {
10290
+ return {
10291
+ title: head.title || head.label,
10292
+ titleFromItemBlockId: head.id
10293
+ };
10294
+ }
10295
+ return {
10296
+ title: formatMessage({
10297
+ id: 'schemaContentItemIndex'
10298
+ }, {
10299
+ index: index + 1
10300
+ }),
10301
+ titleFromItemBlockId: null
10302
+ };
10303
+ };
10203
10304
  const renderBlock = (block, data, ctx) => {
10204
10305
  const {
10205
10306
  formatMessage,
@@ -10249,15 +10350,21 @@ const renderBlock = (block, data, ctx) => {
10249
10350
  const items = name ? getByPath(data, name) : null;
10250
10351
  const rows = Array.isArray(items) ? items : [];
10251
10352
  return /*#__PURE__*/jsx(InfoPage.Part, _extends({}, partProps, {
10252
- children: rows.length ? rows.map((item, index) => /*#__PURE__*/jsxs(InfoPage.Part, {
10253
- title: formatMessage({
10254
- id: 'schemaContentItemIndex'
10255
- }, {
10256
- index: index + 1
10257
- }),
10258
- bordered: false,
10259
- children: [renderFieldsContent(block.list, item || {}, formatMessage, 2), supportsItemBlocks(block.kind) ? renderBlockList(block.itemBlocks, item || {}, nextCtx) : null]
10260
- }, `${block.id}-${index}`)) : formatMessage({
10353
+ children: rows.length ? rows.map((item, index) => {
10354
+ const {
10355
+ title: itemTitle,
10356
+ titleFromItemBlockId
10357
+ } = resolveListItemTitle(block, item || {}, index, formatMessage);
10358
+ const itemBlocks = supportsItemBlocks(block.kind) && titleFromItemBlockId ? (block.itemBlocks || []).map(child => child && child.id === titleFromItemBlockId ? _extends({}, child, {
10359
+ title: undefined,
10360
+ label: undefined
10361
+ }) : child) : block.itemBlocks;
10362
+ return /*#__PURE__*/jsxs(InfoPage.Part, {
10363
+ title: itemTitle,
10364
+ bordered: false,
10365
+ children: [renderFieldsContent(block.list, item || {}, formatMessage, 2), supportsItemBlocks(block.kind) ? renderBlockList(itemBlocks, item || {}, nextCtx) : null]
10366
+ }, `${block.id}-${index}`);
10367
+ }) : formatMessage({
10261
10368
  id: 'schemaContentEmptyValue'
10262
10369
  })
10263
10370
  }));