@rjsf/utils 6.0.0-beta.22 → 6.0.0-beta.23

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.
package/dist/index.cjs CHANGED
@@ -41,6 +41,7 @@ __export(index_exports, {
41
41
  DEFINITIONS_KEY: () => DEFINITIONS_KEY,
42
42
  DEPENDENCIES_KEY: () => DEPENDENCIES_KEY,
43
43
  DISCRIMINATOR_PATH: () => DISCRIMINATOR_PATH,
44
+ DateElement: () => DateElement,
44
45
  ENUM_KEY: () => ENUM_KEY,
45
46
  ERRORS_KEY: () => ERRORS_KEY,
46
47
  ErrorSchemaBuilder: () => ErrorSchemaBuilder,
@@ -153,7 +154,9 @@ __export(index_exports, {
153
154
  toFieldPathId: () => toFieldPathId,
154
155
  toPathSchema: () => toPathSchema,
155
156
  unwrapErrorHandler: () => unwrapErrorHandler,
157
+ useAltDateWidgetProps: () => useAltDateWidgetProps,
156
158
  useDeepCompareMemo: () => useDeepCompareMemo,
159
+ useFileWidgetProps: () => useFileWidgetProps,
157
160
  utcToLocal: () => utcToLocal,
158
161
  validationDataMerge: () => validationDataMerge,
159
162
  withIdRefPrefix: () => withIdRefPrefix
@@ -3414,17 +3417,209 @@ function unwrapErrorHandler(errorHandler) {
3414
3417
  }, {});
3415
3418
  }
3416
3419
 
3417
- // src/useDeepCompareMemo.ts
3420
+ // src/useAltDateWidgetProps.tsx
3418
3421
  var import_react2 = require("react");
3422
+ var import_jsx_runtime2 = require("react/jsx-runtime");
3423
+ function readyForChange(state) {
3424
+ return Object.values(state).every((value) => value !== -1);
3425
+ }
3426
+ function DateElement(props) {
3427
+ const {
3428
+ className = "form-control",
3429
+ type,
3430
+ range,
3431
+ value,
3432
+ select,
3433
+ rootId,
3434
+ name,
3435
+ disabled,
3436
+ readonly,
3437
+ autofocus,
3438
+ registry,
3439
+ onBlur,
3440
+ onFocus
3441
+ } = props;
3442
+ const id = `${rootId}_${type}`;
3443
+ const { SelectWidget } = registry.widgets;
3444
+ const onChange = (0, import_react2.useCallback)((value2) => select(type, value2), [select, type]);
3445
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
3446
+ SelectWidget,
3447
+ {
3448
+ schema: { type: "integer" },
3449
+ id,
3450
+ name,
3451
+ className,
3452
+ options: { enumOptions: dateRangeOptions(range[0], range[1]) },
3453
+ placeholder: type,
3454
+ value,
3455
+ disabled,
3456
+ readonly,
3457
+ autofocus,
3458
+ onChange,
3459
+ onBlur,
3460
+ onFocus,
3461
+ registry,
3462
+ label: "",
3463
+ "aria-describedby": ariaDescribedByIds(rootId)
3464
+ }
3465
+ );
3466
+ }
3467
+ function useAltDateWidgetProps(props) {
3468
+ const { time = false, disabled = false, readonly = false, options, onChange, value } = props;
3469
+ const [state, setState] = (0, import_react2.useState)(parseDateString(value, time));
3470
+ (0, import_react2.useEffect)(() => {
3471
+ setState(parseDateString(value, time));
3472
+ }, [time, value]);
3473
+ const handleChange = (0, import_react2.useCallback)(
3474
+ (property, value2) => {
3475
+ const nextState = {
3476
+ ...state,
3477
+ [property]: typeof value2 === "undefined" ? -1 : value2
3478
+ };
3479
+ if (readyForChange(nextState)) {
3480
+ onChange(toDateString(nextState, time));
3481
+ } else {
3482
+ setState(nextState);
3483
+ }
3484
+ },
3485
+ [state, onChange, time]
3486
+ );
3487
+ const handleClear = (0, import_react2.useCallback)(
3488
+ (event) => {
3489
+ event.preventDefault();
3490
+ if (disabled || readonly) {
3491
+ return;
3492
+ }
3493
+ onChange(void 0);
3494
+ },
3495
+ [disabled, readonly, onChange]
3496
+ );
3497
+ const handleSetNow = (0, import_react2.useCallback)(
3498
+ (event) => {
3499
+ event.preventDefault();
3500
+ if (disabled || readonly) {
3501
+ return;
3502
+ }
3503
+ const nextState = parseDateString((/* @__PURE__ */ new Date()).toJSON(), time);
3504
+ onChange(toDateString(nextState, time));
3505
+ },
3506
+ [disabled, readonly, time, onChange]
3507
+ );
3508
+ const elements = (0, import_react2.useMemo)(
3509
+ () => getDateElementProps(
3510
+ state,
3511
+ time,
3512
+ options.yearsRange,
3513
+ options.format
3514
+ ),
3515
+ [state, time, options.yearsRange, options.format]
3516
+ );
3517
+ return { elements, handleChange, handleClear, handleSetNow };
3518
+ }
3519
+
3520
+ // src/useDeepCompareMemo.ts
3521
+ var import_react3 = require("react");
3419
3522
  var import_isEqual3 = __toESM(require("lodash/isEqual"), 1);
3420
3523
  function useDeepCompareMemo(newValue) {
3421
- const valueRef = (0, import_react2.useRef)(newValue);
3524
+ const valueRef = (0, import_react3.useRef)(newValue);
3422
3525
  if (!(0, import_isEqual3.default)(newValue, valueRef.current)) {
3423
3526
  valueRef.current = newValue;
3424
3527
  }
3425
3528
  return valueRef.current;
3426
3529
  }
3427
3530
 
3531
+ // src/useFileWidgetProps.ts
3532
+ var import_react4 = require("react");
3533
+ function addNameToDataURL(dataURL, name) {
3534
+ return dataURL.replace(";base64", `;name=${encodeURIComponent(name)};base64`);
3535
+ }
3536
+ function processFile(file) {
3537
+ const { name, size, type } = file;
3538
+ return new Promise((resolve, reject) => {
3539
+ const reader = new window.FileReader();
3540
+ reader.onerror = reject;
3541
+ reader.onload = (event) => {
3542
+ if (typeof event.target?.result === "string") {
3543
+ resolve({
3544
+ dataURL: addNameToDataURL(event.target.result, name),
3545
+ name,
3546
+ size,
3547
+ type
3548
+ });
3549
+ } else {
3550
+ resolve({
3551
+ dataURL: null,
3552
+ name,
3553
+ size,
3554
+ type
3555
+ });
3556
+ }
3557
+ };
3558
+ reader.readAsDataURL(file);
3559
+ });
3560
+ }
3561
+ function processFiles(files) {
3562
+ return Promise.all(Array.from(files).map(processFile));
3563
+ }
3564
+ function extractFileInfo(dataURLs) {
3565
+ return dataURLs.reduce((acc, dataURL) => {
3566
+ if (!dataURL) {
3567
+ return acc;
3568
+ }
3569
+ try {
3570
+ const { blob, name } = dataURItoBlob(dataURL);
3571
+ return [
3572
+ ...acc,
3573
+ {
3574
+ dataURL,
3575
+ name,
3576
+ size: blob.size,
3577
+ type: blob.type
3578
+ }
3579
+ ];
3580
+ } catch {
3581
+ return acc;
3582
+ }
3583
+ }, []);
3584
+ }
3585
+ function useFileWidgetProps(value, onChange, multiple = false) {
3586
+ const values = (0, import_react4.useMemo)(() => {
3587
+ if (multiple && value) {
3588
+ return Array.isArray(value) ? value : [value];
3589
+ }
3590
+ return [];
3591
+ }, [value, multiple]);
3592
+ const filesInfo = (0, import_react4.useMemo)(
3593
+ () => Array.isArray(value) ? extractFileInfo(value) : extractFileInfo([value || ""]),
3594
+ [value]
3595
+ );
3596
+ const handleChange = (0, import_react4.useCallback)(
3597
+ (files) => {
3598
+ processFiles(files).then((filesInfoEvent) => {
3599
+ const newValue = filesInfoEvent.map((fileInfo) => fileInfo.dataURL || null);
3600
+ if (multiple) {
3601
+ onChange(values.concat(...newValue));
3602
+ } else {
3603
+ onChange(newValue[0]);
3604
+ }
3605
+ });
3606
+ },
3607
+ [values, multiple, onChange]
3608
+ );
3609
+ const handleRemove = (0, import_react4.useCallback)(
3610
+ (index) => {
3611
+ if (multiple) {
3612
+ const newValue = values.filter((_, i) => i !== index);
3613
+ onChange(newValue);
3614
+ } else {
3615
+ onChange(void 0);
3616
+ }
3617
+ },
3618
+ [values, multiple, onChange]
3619
+ );
3620
+ return { filesInfo, handleChange, handleRemove };
3621
+ }
3622
+
3428
3623
  // src/utcToLocal.ts
3429
3624
  function utcToLocal(jsonDate) {
3430
3625
  if (!jsonDate) {
@@ -3536,6 +3731,8 @@ var TranslatableString = /* @__PURE__ */ ((TranslatableString2) => {
3536
3731
  TranslatableString2["OptionalObjectAdd"] = "Add data for optional field";
3537
3732
  TranslatableString2["OptionalObjectRemove"] = "Remove data for optional field";
3538
3733
  TranslatableString2["OptionalObjectEmptyMsg"] = "No data for optional field";
3734
+ TranslatableString2["Type"] = "Type";
3735
+ TranslatableString2["Value"] = "Value";
3539
3736
  TranslatableString2["UnknownFieldType"] = "Unknown field type %1";
3540
3737
  TranslatableString2["OptionPrefix"] = "Option %1";
3541
3738
  TranslatableString2["TitleOptionPrefix"] = "%1 option %2";