@fibrae/semi-foundation 0.7.0 → 0.9.0

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.
Files changed (51) hide show
  1. package/dist/components/calendar/event-util.js +1 -1
  2. package/dist/components/carousel/foundation.js +1 -1
  3. package/dist/components/cascader/foundation.js +1 -1
  4. package/dist/components/date-picker/months-grid-foundation.d.ts +1 -1
  5. package/dist/components/date-picker/months-grid-foundation.js +1 -1
  6. package/dist/components/dropdown/menu-foundation.d.ts +1 -1
  7. package/dist/components/form/form.css +64 -2
  8. package/dist/components/form/form.scss +65 -2
  9. package/dist/components/form/foundation.d.ts +2 -1
  10. package/dist/components/form/foundation.js +63 -3
  11. package/dist/components/form/interface.d.ts +9 -1
  12. package/dist/components/form/json-schema.d.ts +78 -2
  13. package/dist/components/form/json-schema.js +75 -8
  14. package/dist/components/form/rules.js +1 -1
  15. package/dist/components/form/variables.scss +4 -0
  16. package/dist/components/image/preview-image-foundation.js +1 -1
  17. package/dist/components/image/preview-inner-foundation.js +2 -4
  18. package/dist/components/input-number/foundation.js +2 -2
  19. package/dist/components/json-viewer/foundation.js +1 -1
  20. package/dist/components/navigation/foundation.js +6 -8
  21. package/dist/components/pagination/foundation.d.ts +1 -1
  22. package/dist/components/select/foundation.d.ts +1 -1
  23. package/dist/components/select/option-foundation.d.ts +2 -1
  24. package/dist/components/side-sheet/side-sheet-foundation.js +1 -1
  25. package/dist/components/slider/foundation.d.ts +2 -2
  26. package/dist/components/table/foundation.d.ts +8 -0
  27. package/dist/components/table/foundation.js +14 -2
  28. package/dist/components/table/rtl.scss +5 -0
  29. package/dist/components/table/table.css +123 -0
  30. package/dist/components/table/table.scss +174 -0
  31. package/dist/components/toast/toast.css +1 -1
  32. package/dist/components/toast/toast.scss +2 -1
  33. package/dist/components/tooltip/foundation.js +2 -4
  34. package/dist/components/tooltip/tooltip.css +1 -1
  35. package/dist/components/tooltip/tooltip.scss +4 -2
  36. package/dist/components/tree/tree-util.js +4 -4
  37. package/dist/components/upload/constants.d.ts +1 -1
  38. package/dist/components/upload/foundation.js +1 -1
  39. package/dist/components/video-player/foundation.js +1 -1
  40. package/dist/locale/interface.d.ts +4 -0
  41. package/dist/locale/source/en_US.js +3 -1
  42. package/dist/locale/source/zh_CN.js +3 -1
  43. package/dist/utils/a11y.d.ts +2 -0
  44. package/dist/utils/a11y.js +5 -1
  45. package/dist/utils/index.d.ts +3 -3
  46. package/dist/utils/index.js +3 -3
  47. package/dist/utils/scroll-lock.d.ts +4 -3
  48. package/dist/utils/scroll-lock.js +15 -11
  49. package/dist/utils/supports.d.ts +2 -0
  50. package/dist/utils/supports.js +7 -1
  51. package/package.json +2 -2
@@ -143,7 +143,7 @@ function parseRangeAllDayEvent(event, startDate, rangeStart, rangeEnd, parsed) {
143
143
  let dateLength;
144
144
  const j = differenceInCalendarDays(startDate, rangeStart);
145
145
  let i = 0;
146
- while (Boolean(parsed[i]) && Boolean(parsed[i][j])) i++;
146
+ while (parsed[i] && parsed[i][j]) i++;
147
147
  if (!end) dateLength = 0;
148
148
  else dateLength = isDateInRange(end, rangeStart, rangeEnd) ? differenceInCalendarDays(end, startDate) : differenceInCalendarDays(rangeEnd, startDate);
149
149
  itemInfo.leftPos = round(Number(j) / dateRangeLen);
@@ -44,7 +44,7 @@ var CarouselFoundation = class CarouselFoundation extends BaseFoundation {
44
44
  }
45
45
  }
46
46
  #play(interval) {
47
- if (this.#interval) clearInterval(this.#interval);
47
+ clearInterval(this.#interval);
48
48
  this.#interval = setInterval(() => {
49
49
  this.next();
50
50
  }, interval);
@@ -593,7 +593,7 @@ var CascaderFoundation = class CascaderFoundation extends BaseFoundation {
593
593
  }
594
594
  static getRenderData(props, state) {
595
595
  const { keyEntities, isSearching } = state;
596
- if (isSearching && Boolean(props.filterTreeNode)) return CascaderFoundation.#getFilteredData(props, state);
596
+ if (isSearching && props.filterTreeNode) return CascaderFoundation.#getFilteredData(props, state);
597
597
  return Object.values(keyEntities).filter((item) => item.parentKey === null && !CascaderFoundation.#missingEntities.has(item)).sort((a, b) => a.ind - b.ind);
598
598
  }
599
599
  static isBranch(data, loadData) {
@@ -110,7 +110,7 @@ export default class MonthsGridFoundation extends BaseFoundation<MonthsGridAdapt
110
110
  * Which panels show their year and month picker; while one does, its weeks need a minHeight, or a change of the
111
111
  * number of weeks makes the scroll list unstable
112
112
  */
113
- static getYAMOpenType(state: Pick<MonthsGridFoundationState, 'monthLeft' | 'monthRight'>): "left" | "right" | "none" | "both";
113
+ static getYAMOpenType(state: Pick<MonthsGridFoundationState, 'monthLeft' | 'monthRight'>): "none" | "left" | "right" | "both";
114
114
  static isYearOrTime(panel: PanelState): boolean;
115
115
  /** The `x-open-type` of a panel: what it shows in place of, or beside, its month */
116
116
  static getOpenType(panel: PanelState): "time" | "date" | "year";
@@ -104,7 +104,7 @@ var MonthsGridFoundation = class MonthsGridFoundation extends BaseFoundation {
104
104
  #updateSelectedFromProps(values) {
105
105
  const type = this.getProp("type");
106
106
  const { selected, rangeStart, rangeEnd } = this.getStates();
107
- if (values && values?.length) switch (type) {
107
+ if (values?.length) switch (type) {
108
108
  case "date":
109
109
  this.#initDatePickerFromValue(values);
110
110
  break;
@@ -5,7 +5,7 @@ export type DropdownSelectionMode = 'single' | 'multiple';
5
5
  * Role and checked state of a menu item, from the menu's selectionMode (as React Aria Menu) and showTick. Without a
6
6
  * selectionMode a tick shows a checked state, so showTick alone makes the items checkboxes.
7
7
  */
8
- export declare function getItemRole(selectionMode?: DropdownSelectionMode, showTick?: boolean): "menuitem" | "menuitemradio" | "menuitemcheckbox";
8
+ export declare function getItemRole(selectionMode?: DropdownSelectionMode, showTick?: boolean): "menuitemradio" | "menuitemcheckbox" | "menuitem";
9
9
  /**
10
10
  * What a Dropdown provides to everything inside its popup, read by the menus there and by a Dropdown whose trigger is
11
11
  * one of their items. A framework adapter passes it as the foundation context, with the reading direction added.
@@ -62,8 +62,8 @@
62
62
  align-items: center;
63
63
  margin-inline-start: 4px;
64
64
  }
65
- .semi-form-field-label-required .semi-form-field-label-text::after {
66
- margin-inline-start: 4px;
65
+ .semi-form-field-label-required .semi-form-field-label-text::before {
66
+ margin-inline-end: 4px;
67
67
  font-weight: var(--semi-font-weight-bold);
68
68
  color: var(--semi-color-danger);
69
69
  content: "*";
@@ -80,6 +80,40 @@
80
80
  .semi-form-field-label-right {
81
81
  text-align: end;
82
82
  }
83
+ .semi-form-field-label-with-tooltip {
84
+ display: flex;
85
+ flex-shrink: 0;
86
+ align-items: center;
87
+ max-width: 100%;
88
+ padding-inline-end: 16px;
89
+ margin-top: 0px;
90
+ margin-bottom: 4px;
91
+ }
92
+ .semi-form-field-label-with-tooltip .semi-form-field-label {
93
+ flex: 0 1 auto;
94
+ min-width: 0;
95
+ padding-inline-end: 4px;
96
+ margin: 0;
97
+ }
98
+ .semi-form-field-label-tooltip {
99
+ padding: 0;
100
+ margin: 0;
101
+ font: inherit;
102
+ color: inherit;
103
+ cursor: pointer;
104
+ background: none;
105
+ border: 0;
106
+ }
107
+ .semi-form-field-label-tooltip:focus-visible {
108
+ outline: 2px solid var(--semi-color-primary-light-active);
109
+ }
110
+ .semi-form-field-label-tooltip {
111
+ display: inline-flex;
112
+ flex-shrink: 0;
113
+ align-items: center;
114
+ font-size: var(--semi-font-size-regular);
115
+ color: var(--semi-color-tertiary);
116
+ }
83
117
  .semi-form-field-error-message, .semi-form-field-help-text {
84
118
  font-family: var(--semi-font-family-regular);
85
119
  font-size: var(--semi-font-size-regular);
@@ -122,6 +156,13 @@
122
156
  margin-inline-end: 0;
123
157
  margin-bottom: 0;
124
158
  }
159
+ .semi-form-field[x-label-pos=left] .semi-form-field-label-with-tooltip {
160
+ margin-inline-end: 0;
161
+ margin-bottom: 0;
162
+ }
163
+ .semi-form-field[x-label-pos=left] .semi-form-field-label-with-tooltip .semi-form-field-label {
164
+ margin: 0;
165
+ }
125
166
  .semi-form-field[x-label-pos=left] .semi-form-field-label-with-extra {
126
167
  display: flex;
127
168
  align-items: center;
@@ -194,6 +235,13 @@
194
235
  margin-inline-end: 0;
195
236
  margin-bottom: 0;
196
237
  }
238
+ .semi-form-field-group[x-label-pos=left] .semi-form-field-label-with-tooltip {
239
+ margin-inline-end: 0;
240
+ margin-bottom: 0;
241
+ }
242
+ .semi-form-field-group[x-label-pos=left] .semi-form-field-label-with-tooltip .semi-form-field-label {
243
+ margin: 0;
244
+ }
197
245
 
198
246
  .semi-form-section {
199
247
  min-inline-size: 0;
@@ -217,6 +265,13 @@
217
265
  color: var(--semi-color-text-0);
218
266
  border-bottom: 1px solid var(--semi-color-border);
219
267
  }
268
+ .semi-form-section-extra {
269
+ font-family: var(--semi-font-family-regular);
270
+ font-size: var(--semi-font-size-regular);
271
+ line-height: var(--semi-line-height-regular);
272
+ margin-bottom: 4px;
273
+ color: var(--semi-color-tertiary);
274
+ }
220
275
  .semi-form-section:nth-of-type(1) {
221
276
  margin-top: 0;
222
277
  }
@@ -237,4 +292,11 @@
237
292
  flex-shrink: 0;
238
293
  gap: 4px;
239
294
  padding-block-end: 12px;
295
+ }
296
+
297
+ .semi-form-schema-array-row-single {
298
+ align-items: flex-start;
299
+ }
300
+ .semi-form-schema-array-row-single .semi-form-schema-array-row-actions {
301
+ padding-block: 12px 0;
240
302
  }}
@@ -1,5 +1,6 @@
1
1
  @use 'semi:theme' as *;
2
2
  @use 'variables' as *;
3
+ @use '../../base/mixin' as *;
3
4
 
4
5
  $form: #{$prefix}-form;
5
6
  $field: #{$prefix}-form-field;
@@ -102,8 +103,8 @@ $rating: #{$prefix}-rating;
102
103
 
103
104
  &-required {
104
105
  .#{$field}-label-text {
105
- &::after {
106
- margin-inline-start: $spacing-form_label_required-marginLeft;
106
+ &::before {
107
+ margin-inline-end: $spacing-form_label_required-marginLeft;
107
108
  font-weight: $font-form_requiredMark-fontWeight;
108
109
  color: $color-form_requiredMark-text-default;
109
110
  content: '*';
@@ -128,6 +129,36 @@ $rating: #{$prefix}-rating;
128
129
  }
129
130
  }
130
131
 
132
+ // The tooltip button sits beside the label element, not in it: a label's content names the control, and a
133
+ // button must not be nested in a label. The width of the label moves to this wrapper to keep columns aligned, and
134
+ // so do the label's outer margins: align-items centers margin boxes, so a bottom margin left on the label would
135
+ // drop the button below the text's center line.
136
+ &-field-label-with-tooltip {
137
+ display: flex;
138
+ flex-shrink: 0;
139
+ align-items: center;
140
+ max-width: 100%;
141
+ padding-inline-end: $spacing-form_label-paddingRight;
142
+ margin-top: $spacing-form_label-marginTop;
143
+ margin-bottom: $spacing-form_label-marginBottom;
144
+
145
+ .#{$field}-label {
146
+ flex: 0 1 auto;
147
+ min-width: 0;
148
+ padding-inline-end: $spacing-form_label_tooltip-marginLeft;
149
+ margin: 0;
150
+ }
151
+ }
152
+
153
+ &-field-label-tooltip {
154
+ @include bare-button;
155
+ display: inline-flex;
156
+ flex-shrink: 0;
157
+ align-items: center;
158
+ font-size: $width-form_label_tooltip-icon;
159
+ color: $color-form_label_tooltip-icon-default;
160
+ }
161
+
131
162
  &-field-error-message,
132
163
  &-field-help-text {
133
164
  @include font-size-regular;
@@ -176,6 +207,14 @@ $rating: #{$prefix}-rating;
176
207
  margin-inline-end: $spacing-form_label_posLeft-marginRight;
177
208
  margin-bottom: $spacing-form_label_posLeft-marginBottom;
178
209
  }
210
+ .#{$field}-label-with-tooltip {
211
+ margin-inline-end: $spacing-form_label_posLeft-marginRight;
212
+ margin-bottom: $spacing-form_label_posLeft-marginBottom;
213
+
214
+ .#{$field}-label {
215
+ margin: 0;
216
+ }
217
+ }
179
218
  .#{$field}-label-with-extra {
180
219
  display: flex;
181
220
  align-items: center;
@@ -261,6 +300,14 @@ $rating: #{$prefix}-rating;
261
300
  margin-inline-end: $spacing-form_label_posLeft-marginRight;
262
301
  margin-bottom: $spacing-form_label_posLeft-marginBottom;
263
302
  }
303
+ .#{$field}-label-with-tooltip {
304
+ margin-inline-end: $spacing-form_label_posLeft-marginRight;
305
+ margin-bottom: $spacing-form_label_posLeft-marginBottom;
306
+
307
+ .#{$field}-label {
308
+ margin: 0;
309
+ }
310
+ }
264
311
  }
265
312
  }
266
313
 
@@ -287,6 +334,12 @@ $rating: #{$prefix}-rating;
287
334
  border-bottom: $width-form_section-border solid $color-form_section-border-default;
288
335
  }
289
336
 
337
+ &-extra {
338
+ @include font-size-regular;
339
+ margin-bottom: $spacing-form_section_extra-marginBottom;
340
+ color: $color-form_label_extra-text-default;
341
+ }
342
+
290
343
  &:nth-of-type(1) {
291
344
  margin-top: 0;
292
345
  }
@@ -310,3 +363,13 @@ $rating: #{$prefix}-rating;
310
363
  // aligns the buttons with the control of the row's last field, not with the field's bottom padding
311
364
  padding-block-end: $spacing-form_field_vertical-paddingBottom;
312
365
  }
366
+
367
+ // a single-value row has no label: the buttons align with the control's top, so an error message below the control
368
+ // only grows the row downward
369
+ .#{$form}-schema-array-row-single {
370
+ align-items: flex-start;
371
+
372
+ .#{$form}-schema-array-row-actions {
373
+ padding-block: $spacing-form_field_vertical-paddingTop 0;
374
+ }
375
+ }
@@ -65,7 +65,8 @@ export default class FormFoundation<E = unknown> extends BaseFoundation<BaseForm
65
65
  setValues: (values: any, opts?: SetValuesConfig) => void;
66
66
  submitForm: () => void;
67
67
  getFieldExist: (field: string) => boolean;
68
- scrollToField: (field: string, scrollOpts?: Options) => void;
68
+ scrollToField: (field: string, scrollOpts?: Options) => Promise<void>;
69
+ focusField: (field: string, scrollOpts?: Options) => Promise<void>;
69
70
  scrollToError: (opts?: ScrollToErrorOpts) => void;
70
71
  setValue: (field: string, value: any, opts?: CallOpts) => void;
71
72
  setError: (field: string, error: unknown, opts?: CallOpts) => void;
@@ -1,4 +1,5 @@
1
1
  import BaseFoundation from "../../base/foundation.js";
2
+ import FocusTrapHandle from "../../base/focus-handle.js";
2
3
  import isPromise from "../../utils/is-promise.js";
3
4
  import warning from "../../utils/warning.js";
4
5
  import { get as get$1, has as has$1, isEmpty, remove, set as set$1 } from "../../utils/object.js";
@@ -15,6 +16,8 @@ const SILENT = {
15
16
  skipNotify: true,
16
17
  skipRender: true
17
18
  };
19
+ const REVEAL_TIMEOUT = 1e3;
20
+ const HIDDEN_SELECTOR = "[inert], [hidden]";
18
21
  const prefix = cssClasses.PREFIX;
19
22
  const ERROR_SELECTOR = `.${prefix}-field-error-message`;
20
23
  const errorField = (errorDOM) => errorDOM.closest(`.${prefix}-field, .${prefix}-field-group`);
@@ -61,6 +64,7 @@ var FormFoundation = class extends BaseFoundation {
61
64
  #pendingSchemaRun;
62
65
  #latestSchemaRun;
63
66
  #detachedErrorPaths = [];
67
+ #cancelReveal;
64
68
  constructor(adapter) {
65
69
  super(adapter);
66
70
  let { initValues } = this.adapter.getProps();
@@ -959,12 +963,64 @@ var FormFoundation = class extends BaseFoundation {
959
963
  const errorDOM = this.#getFieldDOM(field)?.querySelector(ERROR_SELECTOR);
960
964
  return errorDOM && errorField(errorDOM);
961
965
  }
966
+ /**
967
+ * A field root, or the element a SchemaField renderer gave the `x-field-id` of an array or object node. One inside
968
+ * collapsed content kept in the DOM (Collapsible keepDOM marks it inert) counts as not rendered.
969
+ */
970
+ #findRendered(field) {
971
+ const element = this.adapter.getFormElement()?.querySelector(`[x-field-id="${CSS.escape(field)}"]`);
972
+ return element && !element.closest(HIDDEN_SELECTOR) ? element : null;
973
+ }
974
+ /**
975
+ * Asks the caller to reveal `field` (expand a collapsed card) and resolves with its element once mounted, or null
976
+ * after REVEAL_TIMEOUT; a newer call or unmounting cancels the wait.
977
+ * shortcut: the element is scrolled to as soon as it mounts, so a container still running an expand animation can
978
+ * leave it short of the target; wait for transitionend if callers report that.
979
+ */
980
+ #reveal(field) {
981
+ const formElement = this.adapter.getFormElement();
982
+ if (!formElement) return Promise.resolve(null);
983
+ return new Promise((resolve) => {
984
+ const finish = (element) => {
985
+ observer.disconnect();
986
+ clearTimeout(timer);
987
+ this.#cancelReveal = void 0;
988
+ resolve(element);
989
+ };
990
+ const observer = new MutationObserver(() => {
991
+ const element = this.#findRendered(field);
992
+ if (element) finish(element);
993
+ });
994
+ const timer = setTimeout(() => finish(null), REVEAL_TIMEOUT);
995
+ this.#cancelReveal = () => finish(null);
996
+ observer.observe(formElement, {
997
+ childList: true,
998
+ subtree: true,
999
+ attributes: true,
1000
+ attributeFilter: [
1001
+ "x-field-id",
1002
+ "inert",
1003
+ "hidden"
1004
+ ]
1005
+ });
1006
+ this.adapter.notifyRevealField(field);
1007
+ });
1008
+ }
962
1009
  #scrollToField(field, scrollOpts = {
963
1010
  behavior: "smooth",
964
1011
  block: "start"
965
1012
  }) {
966
- const fieldDOM = this.#getFieldExist(field) ? this.#getFieldDOM(field) : null;
967
- if (fieldDOM) scrollIntoView$1(fieldDOM, scrollOpts);
1013
+ this.#cancelReveal?.();
1014
+ const scroll = (element) => {
1015
+ if (element) scrollIntoView$1(element, scrollOpts);
1016
+ return element;
1017
+ };
1018
+ const found = this.#findRendered(field);
1019
+ return found ? Promise.resolve(scroll(found)) : this.#reveal(field).then(scroll);
1020
+ }
1021
+ async #focusField(field, scrollOpts) {
1022
+ const element = await this.#scrollToField(field, scrollOpts);
1023
+ (element && FocusTrapHandle.getFocusableElements(element).find((candidate) => !candidate.closest(`.${prefix}-field-label, .${prefix}-field-label-with-tooltip`)))?.focus({ preventScroll: true });
968
1024
  }
969
1025
  #scrollToError(config) {
970
1026
  const { field, index, scrollOpts = {
@@ -1004,6 +1060,7 @@ var FormFoundation = class extends BaseFoundation {
1004
1060
  }
1005
1061
  destroy() {
1006
1062
  this.#destroyed = true;
1063
+ this.#cancelReveal?.();
1007
1064
  this.#cancelDebouncedValidation();
1008
1065
  this.#unregistered = [];
1009
1066
  }
@@ -1100,7 +1157,10 @@ var FormFoundation = class extends BaseFoundation {
1100
1157
  setValues: (values, opts) => this.#setValues(values, opts ?? { isOverride: false }),
1101
1158
  submitForm: () => this.submit(),
1102
1159
  getFieldExist: (field) => this.#getFieldExist(field),
1103
- scrollToField: (field, scrollOpts) => this.#scrollToField(field, scrollOpts),
1160
+ scrollToField: async (field, scrollOpts) => {
1161
+ await this.#scrollToField(field, scrollOpts);
1162
+ },
1163
+ focusField: (field, scrollOpts) => this.#focusField(field, scrollOpts),
1104
1164
  scrollToError: (opts) => this.#scrollToError(opts)
1105
1165
  };
1106
1166
  };
@@ -78,6 +78,8 @@ export interface BaseFormAdapter<P = Record<string, any>, Values extends object
78
78
  notifyValueChange: (values: any, changedValues: any) => void;
79
79
  notifyErrorChange: (errors: any, changedError: any) => void;
80
80
  notifyReset: () => void;
81
+ /** scrollToField or focusField found nothing rendered at `field`: the caller expands what hides it */
82
+ notifyRevealField: (field: string) => void;
81
83
  /** The rendered form element; null before mount and after unmount */
82
84
  getFormElement: () => Element | null;
83
85
  }
@@ -150,7 +152,13 @@ export interface BaseFormApi<T extends object = any, O = T> {
150
152
  getInitValues: () => any;
151
153
  getValues: () => T;
152
154
  setValues: (fieldsValue: Partial<T>, config?: SetValuesConfig) => void;
153
- scrollToField: <K extends FieldPath<T>>(field: K, scrollConfig?: Options) => void;
155
+ /**
156
+ * Scrolls to the field, ArrayField container or SchemaField node at `field`. When nothing is rendered there, calls
157
+ * the Form's onRevealField and waits up to a second for it to mount.
158
+ */
159
+ scrollToField: <K extends FieldPath<T>>(field: K, scrollConfig?: Options) => Promise<void>;
160
+ /** scrollToField, then focuses the first focusable element inside, a tooltip trigger in a label excepted */
161
+ focusField: <K extends FieldPath<T>>(field: K, scrollConfig?: Options) => Promise<void>;
154
162
  scrollToError: <K extends FieldPath<T>>(config?: ScrollToErrorOptions<K>) => void;
155
163
  }
156
164
  export interface CallOpts {
@@ -1,3 +1,4 @@
1
+ import { Locale } from "../../locale/interface.js";
1
2
  import { StandardSchema } from "./interface.js";
2
3
  export type JsonSchema = Record<string, any>;
3
4
  export type SchemaFieldKind = 'input' | 'textarea' | 'number' | 'switch' | 'select' | 'object' | 'array' | 'unsupported';
@@ -12,6 +13,14 @@ export interface SchemaFieldNode {
12
13
  /** `title`, else the property name; undefined for an array item */
13
14
  title?: string;
14
15
  description?: string;
16
+ /** Path segments from the schema root: 0 for the root, also when SchemaField starts below it */
17
+ depth: number;
18
+ /** Set on an array item: its row index and the array's title (undefined for an untitled array item) and key */
19
+ item?: {
20
+ index: number;
21
+ arrayTitle?: string;
22
+ arrayKey: string | number;
23
+ };
15
24
  required: boolean;
16
25
  format?: string;
17
26
  default?: unknown;
@@ -38,5 +47,72 @@ export interface SchemaFieldNode {
38
47
  export declare function resolveJsonSchema(jsonSchema?: JsonSchema, schema?: StandardSchema): JsonSchema | undefined;
39
48
  /** The node at `field` ('' = root), walking properties and items; throws when the path is not in the schema */
40
49
  export declare function describeSchemaField(root: JsonSchema, field?: string): SchemaFieldNode;
41
- /** Value of a new array row: object -> {} with required nested objects and arrays prefilled (arrays with minItems blank rows), other -> undefined */
42
- export declare function blankRow(root: JsonSchema, arrayNode: SchemaFieldNode): unknown;
50
+ /**
51
+ * The semiRenderers name of a single-value array row, undefined for any other node. Such a row shows no label or
52
+ * description: the array's Section shows them once, and the row is named "<array title> item N".
53
+ */
54
+ export declare function singleItemName({ item, kind }: SchemaFieldNode, messages: Pick<Locale['Form'], 'item' | 'arrayItem'>): string;
55
+ /** What an array node allows with `count` rows; a fixed array (minItems === maxItems) neither adds nor removes */
56
+ export declare function arrayState(node: SchemaFieldNode, count: number): {
57
+ count: number;
58
+ minItems: number;
59
+ maxItems: number;
60
+ fixed: boolean;
61
+ canAdd: boolean;
62
+ canRemove: boolean;
63
+ };
64
+ /** The props semiRenderers gives the Form control of a leaf node, by kind */
65
+ export declare function controlProps(node: SchemaFieldNode): {
66
+ maxLength: number;
67
+ maxCount?: undefined;
68
+ max?: undefined;
69
+ min?: undefined;
70
+ precision?: undefined;
71
+ multiple?: undefined;
72
+ optionList?: undefined;
73
+ } | {
74
+ maxCount: number;
75
+ maxLength?: undefined;
76
+ max?: undefined;
77
+ min?: undefined;
78
+ precision?: undefined;
79
+ multiple?: undefined;
80
+ optionList?: undefined;
81
+ } | {
82
+ max: number;
83
+ min: number;
84
+ precision: number;
85
+ maxLength?: undefined;
86
+ maxCount?: undefined;
87
+ multiple?: undefined;
88
+ optionList?: undefined;
89
+ } | {
90
+ multiple: boolean;
91
+ optionList: {
92
+ label: string;
93
+ value: string | number | boolean;
94
+ }[];
95
+ maxLength?: undefined;
96
+ maxCount?: undefined;
97
+ max?: undefined;
98
+ min?: undefined;
99
+ precision?: undefined;
100
+ } | {
101
+ maxLength?: undefined;
102
+ maxCount?: undefined;
103
+ max?: undefined;
104
+ min?: undefined;
105
+ precision?: undefined;
106
+ multiple?: undefined;
107
+ optionList?: undefined;
108
+ };
109
+ export declare const schemaFieldWarnings: {
110
+ noSchema: string;
111
+ noRenderer: (field: string, kind: string) => string;
112
+ unsupported: (field: string) => string;
113
+ };
114
+ /**
115
+ * Value of a new row of the array at `arrayField`: object -> {} with required nested objects and arrays prefilled
116
+ * (arrays with minItems blank rows), other -> undefined
117
+ */
118
+ export declare function blankRow(root: JsonSchema, arrayField: string): unknown;
@@ -1,3 +1,4 @@
1
+ import { formatLocale } from "../../locale/format.js";
1
2
  import { toPath } from "es-toolkit/compat";
2
3
  // src/components/form/json-schema.ts
3
4
  const SINGLE_LINE_MAX_LENGTH = 160;
@@ -77,13 +78,15 @@ const bound = (values, pick) => {
77
78
  };
78
79
  const childPath = (field, name) => IDENTIFIER.test(name) ? field ? `${field}.${name}` : name : `${field}[${JSON.stringify(name)}]`;
79
80
  const isRequired = (parent, name) => Array.isArray(parent.required) && parent.required.includes(name);
80
- function buildNode(root, { schema, seen }, field, key, required, fallbackTitle) {
81
+ function buildNode(root, { schema, seen }, field, key, required, fallbackTitle, item) {
81
82
  const base = {
82
83
  field,
83
84
  pattern: field.replaceAll(/\[\d+\]/g, "[]"),
84
85
  key,
85
86
  title: schema.title ?? fallbackTitle,
86
87
  description: schema.description,
88
+ depth: field === "" ? 0 : toPath(field).length,
89
+ item,
87
90
  required,
88
91
  format: schema.format,
89
92
  default: schema.default,
@@ -155,8 +158,15 @@ function buildNode(root, { schema, seen }, field, key, required, fallbackTitle)
155
158
  }
156
159
  /** Explicit wins; otherwise the Form schema's Standard JSON Schema input (target draft-2020-12); undefined when neither exists */
157
160
  function resolveJsonSchema(jsonSchema, schema) {
158
- return jsonSchema ?? schema?.["~standard"].jsonSchema?.input({ target: "draft-2020-12" });
161
+ if (jsonSchema || !schema) return jsonSchema;
162
+ let converted = convertedSchemas.get(schema);
163
+ if (!converted) {
164
+ converted = schema["~standard"].jsonSchema?.input({ target: "draft-2020-12" });
165
+ if (converted) convertedSchemas.set(schema, converted);
166
+ }
167
+ return converted;
159
168
  }
169
+ const convertedSchemas = /* @__PURE__ */ new WeakMap();
160
170
  /** The node at `field` ('' = root), walking properties and items; throws when the path is not in the schema */
161
171
  function describeSchemaField(root, field = "") {
162
172
  const fail = () => /* @__PURE__ */ new Error(`Schema has no field "${field}"`);
@@ -164,16 +174,23 @@ function describeSchemaField(root, field = "") {
164
174
  let key = "";
165
175
  let required = false;
166
176
  let fallbackTitle;
177
+ let item;
167
178
  for (const segment of field === "" ? [] : toPath(field)) {
168
179
  const parent = current.schema;
169
180
  let child;
170
181
  if (parent.type === "array") {
171
182
  if (!/^\d+$/.test(segment)) throw fail();
172
183
  child = parent.items;
173
- key = Number(segment);
184
+ item = {
185
+ index: Number(segment),
186
+ arrayTitle: parent.title ?? fallbackTitle,
187
+ arrayKey: key
188
+ };
189
+ key = item.index;
174
190
  required = false;
175
191
  fallbackTitle = void 0;
176
192
  } else {
193
+ item = void 0;
177
194
  if (parent.type !== "object" || !isObject(parent.properties) || !Object.hasOwn(parent.properties, segment)) throw fail();
178
195
  child = parent.properties[segment];
179
196
  key = segment;
@@ -183,8 +200,55 @@ function describeSchemaField(root, field = "") {
183
200
  if (!isObject(child)) throw fail();
184
201
  current = resolve(root, child, current.seen);
185
202
  }
186
- return buildNode(root, current, field, key, required, fallbackTitle);
203
+ return buildNode(root, current, field, key, required, fallbackTitle, item);
204
+ }
205
+ /**
206
+ * The semiRenderers name of a single-value array row, undefined for any other node. Such a row shows no label or
207
+ * description: the array's Section shows them once, and the row is named "<array title> item N".
208
+ */
209
+ function singleItemName({ item, kind }, messages) {
210
+ if (!item || kind === "object" || kind === "array") return void 0;
211
+ const index = item.index + 1;
212
+ return item.arrayTitle === void 0 ? formatLocale(messages.item, { index }) : formatLocale(messages.arrayItem, {
213
+ title: item.arrayTitle,
214
+ index
215
+ });
216
+ }
217
+ /** What an array node allows with `count` rows; a fixed array (minItems === maxItems) neither adds nor removes */
218
+ function arrayState(node, count) {
219
+ const { minItems, maxItems } = node;
220
+ const fixed = Boolean(node.fixed);
221
+ return {
222
+ count,
223
+ minItems,
224
+ maxItems,
225
+ fixed,
226
+ canAdd: !fixed && (maxItems === void 0 || count < maxItems),
227
+ canRemove: !fixed && count > (minItems ?? 0)
228
+ };
187
229
  }
230
+ /** The props semiRenderers gives the Form control of a leaf node, by kind */
231
+ function controlProps(node) {
232
+ switch (node.kind) {
233
+ case "input": return { maxLength: node.maxLength };
234
+ case "textarea": return { maxCount: node.maxLength };
235
+ case "number": return {
236
+ max: node.maximum,
237
+ min: node.minimum,
238
+ precision: node.integer ? 0 : void 0
239
+ };
240
+ case "select": return {
241
+ multiple: node.multiple,
242
+ optionList: node.options
243
+ };
244
+ default: return {};
245
+ }
246
+ }
247
+ const schemaFieldWarnings = {
248
+ noSchema: "[Semi Form] SchemaField has no JSON Schema: pass jsonSchema, or give the Form a schema that exports one",
249
+ noRenderer: (field, kind) => `[Semi Form] SchemaField renders nothing for "${field}": renderers has no ${kind} entry`,
250
+ unsupported: (field) => `[Semi Form] SchemaField renders nothing for "${field}": its schema maps to no control`
251
+ };
188
252
  function blankValue(root, { schema, seen }) {
189
253
  if (schema.type === "object") {
190
254
  const row = {};
@@ -200,9 +264,12 @@ function blankValue(root, { schema, seen }) {
200
264
  return Array.from({ length: schema.minItems ?? 0 }, () => items && blankValue(root, items));
201
265
  }
202
266
  }
203
- /** Value of a new array row: object -> {} with required nested objects and arrays prefilled (arrays with minItems blank rows), other -> undefined */
204
- function blankRow(root, arrayNode) {
205
- const { items } = arrayNode.schema;
267
+ /**
268
+ * Value of a new row of the array at `arrayField`: object -> {} with required nested objects and arrays prefilled
269
+ * (arrays with minItems blank rows), other -> undefined
270
+ */
271
+ function blankRow(root, arrayField) {
272
+ const { items } = describeSchemaField(root, arrayField).schema;
206
273
  return isObject(items) ? blankValue(root, resolve(root, items, [])) : void 0;
207
274
  }
208
- export { blankRow, describeSchemaField, resolveJsonSchema };
275
+ export { arrayState, blankRow, controlProps, describeSchemaField, resolveJsonSchema, schemaFieldWarnings, singleItemName };
@@ -14,7 +14,7 @@ const typeCheckers = {
14
14
  string: (value) => typeof value === "string",
15
15
  number: (value) => typeof value === "number" && !Number.isNaN(value),
16
16
  integer: (value) => Number.isInteger(value),
17
- float: (value) => typeof value === "number" && Number.isFinite(value) && !Number.isInteger(value),
17
+ float: (value) => Number.isFinite(value) && !Number.isInteger(value),
18
18
  boolean: (value) => typeof value === "boolean",
19
19
  array: (value) => Array.isArray(value),
20
20
  object: (value) => typeof value === "object" && value !== null && !Array.isArray(value),