@tunghtml/strapi-plugin-composite-field 1.1.0 → 1.1.1

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.
@@ -3,6 +3,7 @@ import * as React from "react";
3
3
  import React__default from "react";
4
4
  import { Field, Flex, Button, Typography } from "@strapi/design-system";
5
5
  import { Play } from "@strapi/icons";
6
+ import { useCMEditViewDataManager } from "@strapi/strapi/admin";
6
7
  var __assign = function() {
7
8
  __assign = Object.assign || function __assign2(t) {
8
9
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -427,6 +428,7 @@ const CompositeInput = (props) => {
427
428
  label,
428
429
  intlLabel
429
430
  } = props;
431
+ const { modifiedData } = useCMEditViewDataManager();
430
432
  const { formatMessage } = useIntl();
431
433
  const [localValue, setLocalValue] = React__default.useState(value || "");
432
434
  React__default.useEffect(() => {
@@ -448,35 +450,10 @@ const CompositeInput = (props) => {
448
450
  }
449
451
  };
450
452
  const handleGenerate = React__default.useCallback(() => {
451
- const formData = {};
452
- fields.forEach((fieldPath) => {
453
- const input = document.querySelector(
454
- `input[name="${fieldPath}"], textarea[name="${fieldPath}"], select[name="${fieldPath}"]`
455
- );
456
- if (input && input.value) {
457
- formData[fieldPath] = input.value;
458
- } else {
459
- const enumSelect = document.querySelector(
460
- `[data-strapi-field-name="${fieldPath}"] select`
461
- );
462
- if (enumSelect && enumSelect.value) {
463
- formData[fieldPath] = enumSelect.value;
464
- }
465
- const enumButton = document.querySelector(
466
- `[data-strapi-field-name="${fieldPath}"] button[role="combobox"]`
467
- );
468
- if (enumButton) {
469
- const selectedText = enumButton.textContent?.trim();
470
- if (selectedText && selectedText !== "Select...") {
471
- formData[fieldPath] = selectedText;
472
- }
473
- }
474
- }
475
- });
476
453
  const parts = [];
477
454
  fields.forEach((fieldPath) => {
478
- const fieldValue = formData[fieldPath];
479
- if (fieldValue) {
455
+ const fieldValue = modifiedData?.[fieldPath];
456
+ if (fieldValue !== null && fieldValue !== void 0 && fieldValue !== "") {
480
457
  parts.push(fieldValue);
481
458
  }
482
459
  });
@@ -486,33 +463,14 @@ const CompositeInput = (props) => {
486
463
  if (onChange) {
487
464
  onChange({ target: { name, value: result, type: "text" } });
488
465
  }
489
- }, [fields, separator, onChange, name]);
466
+ }, [fields, separator, onChange, name, modifiedData]);
490
467
  React__default.useEffect(() => {
491
- if (!autoGenerate || fields.length === 0) return;
492
- const handleFieldChange = () => {
493
- const timeoutId = setTimeout(() => {
494
- handleGenerate();
495
- }, 300);
496
- return () => clearTimeout(timeoutId);
497
- };
498
- const listeners = [];
499
- fields.forEach((fieldPath) => {
500
- const elements = document.querySelectorAll(
501
- `input[name="${fieldPath}"], textarea[name="${fieldPath}"], select[name="${fieldPath}"], [data-strapi-field-name="${fieldPath}"] select, [data-strapi-field-name="${fieldPath}"] button`
502
- );
503
- elements.forEach((element) => {
504
- element.addEventListener("change", handleFieldChange);
505
- element.addEventListener("input", handleFieldChange);
506
- listeners.push({ element, handler: handleFieldChange });
507
- });
508
- });
509
- return () => {
510
- listeners.forEach(({ element, handler }) => {
511
- element.removeEventListener("change", handler);
512
- element.removeEventListener("input", handler);
513
- });
514
- };
515
- }, [autoGenerate, fields, handleGenerate]);
468
+ if (!autoGenerate || fields.length === 0 || !modifiedData) return;
469
+ const timeoutId = setTimeout(() => {
470
+ handleGenerate();
471
+ }, 300);
472
+ return () => clearTimeout(timeoutId);
473
+ }, [autoGenerate, fields, modifiedData, handleGenerate]);
516
474
  return /* @__PURE__ */ jsx(
517
475
  Field.Root,
518
476
  {
@@ -4,6 +4,7 @@ const jsxRuntime = require("react/jsx-runtime");
4
4
  const React = require("react");
5
5
  const designSystem = require("@strapi/design-system");
6
6
  const icons = require("@strapi/icons");
7
+ const admin = require("@strapi/strapi/admin");
7
8
  function _interopNamespace(e) {
8
9
  if (e && e.__esModule) return e;
9
10
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
@@ -446,6 +447,7 @@ const CompositeInput = (props) => {
446
447
  label,
447
448
  intlLabel
448
449
  } = props;
450
+ const { modifiedData } = admin.useCMEditViewDataManager();
449
451
  const { formatMessage } = useIntl();
450
452
  const [localValue, setLocalValue] = React__namespace.default.useState(value || "");
451
453
  React__namespace.default.useEffect(() => {
@@ -467,35 +469,10 @@ const CompositeInput = (props) => {
467
469
  }
468
470
  };
469
471
  const handleGenerate = React__namespace.default.useCallback(() => {
470
- const formData = {};
471
- fields.forEach((fieldPath) => {
472
- const input = document.querySelector(
473
- `input[name="${fieldPath}"], textarea[name="${fieldPath}"], select[name="${fieldPath}"]`
474
- );
475
- if (input && input.value) {
476
- formData[fieldPath] = input.value;
477
- } else {
478
- const enumSelect = document.querySelector(
479
- `[data-strapi-field-name="${fieldPath}"] select`
480
- );
481
- if (enumSelect && enumSelect.value) {
482
- formData[fieldPath] = enumSelect.value;
483
- }
484
- const enumButton = document.querySelector(
485
- `[data-strapi-field-name="${fieldPath}"] button[role="combobox"]`
486
- );
487
- if (enumButton) {
488
- const selectedText = enumButton.textContent?.trim();
489
- if (selectedText && selectedText !== "Select...") {
490
- formData[fieldPath] = selectedText;
491
- }
492
- }
493
- }
494
- });
495
472
  const parts = [];
496
473
  fields.forEach((fieldPath) => {
497
- const fieldValue = formData[fieldPath];
498
- if (fieldValue) {
474
+ const fieldValue = modifiedData?.[fieldPath];
475
+ if (fieldValue !== null && fieldValue !== void 0 && fieldValue !== "") {
499
476
  parts.push(fieldValue);
500
477
  }
501
478
  });
@@ -505,33 +482,14 @@ const CompositeInput = (props) => {
505
482
  if (onChange) {
506
483
  onChange({ target: { name, value: result, type: "text" } });
507
484
  }
508
- }, [fields, separator, onChange, name]);
485
+ }, [fields, separator, onChange, name, modifiedData]);
509
486
  React__namespace.default.useEffect(() => {
510
- if (!autoGenerate || fields.length === 0) return;
511
- const handleFieldChange = () => {
512
- const timeoutId = setTimeout(() => {
513
- handleGenerate();
514
- }, 300);
515
- return () => clearTimeout(timeoutId);
516
- };
517
- const listeners = [];
518
- fields.forEach((fieldPath) => {
519
- const elements = document.querySelectorAll(
520
- `input[name="${fieldPath}"], textarea[name="${fieldPath}"], select[name="${fieldPath}"], [data-strapi-field-name="${fieldPath}"] select, [data-strapi-field-name="${fieldPath}"] button`
521
- );
522
- elements.forEach((element) => {
523
- element.addEventListener("change", handleFieldChange);
524
- element.addEventListener("input", handleFieldChange);
525
- listeners.push({ element, handler: handleFieldChange });
526
- });
527
- });
528
- return () => {
529
- listeners.forEach(({ element, handler }) => {
530
- element.removeEventListener("change", handler);
531
- element.removeEventListener("input", handler);
532
- });
533
- };
534
- }, [autoGenerate, fields, handleGenerate]);
487
+ if (!autoGenerate || fields.length === 0 || !modifiedData) return;
488
+ const timeoutId = setTimeout(() => {
489
+ handleGenerate();
490
+ }, 300);
491
+ return () => clearTimeout(timeoutId);
492
+ }, [autoGenerate, fields, modifiedData, handleGenerate]);
535
493
  return /* @__PURE__ */ jsxRuntime.jsx(
536
494
  designSystem.Field.Root,
537
495
  {
@@ -37,7 +37,7 @@ const index = {
37
37
  defaultMessage: "Combine multiple fields into one display value"
38
38
  },
39
39
  components: {
40
- Input: async () => Promise.resolve().then(() => require("../_chunks/CompositeInput-cwqJYLtS.js"))
40
+ Input: async () => Promise.resolve().then(() => require("../_chunks/CompositeInput-B2zE74Yh.js"))
41
41
  },
42
42
  options: {
43
43
  base: [
@@ -34,7 +34,7 @@ const index = {
34
34
  defaultMessage: "Combine multiple fields into one display value"
35
35
  },
36
36
  components: {
37
- Input: async () => import("../_chunks/CompositeInput-Cs0WHHpL.mjs")
37
+ Input: async () => import("../_chunks/CompositeInput-B-IQTi47.mjs")
38
38
  },
39
39
  options: {
40
40
  base: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tunghtml/strapi-plugin-composite-field",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A Strapi v5 plugin to create composite display fields by combining multiple text, string, and enum fields with auto-generate support",
5
5
  "keywords": [
6
6
  "strapi",