@bpmn-io/form-js-editor 0.6.1 → 0.7.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.
package/dist/index.cjs CHANGED
@@ -1165,7 +1165,7 @@ function TextInput(props) {
1165
1165
  const prevValue = usePrevious(value);
1166
1166
  const [cachedValue, setCachedValue] = hooks$1.useState(null);
1167
1167
  const [error, setError] = hooks$1.useState(null);
1168
- hooks$1.useEffect(() => setError(validate(value)), [value]);
1168
+ hooks$1.useEffect(() => setError(validate(value)), [validate, value]);
1169
1169
  const onInput = useDebounce(event => {
1170
1170
  const value = event.target.value;
1171
1171
  const error = validate(value);
@@ -1815,9 +1815,18 @@ function ValueEntry(props) {
1815
1815
  const {
1816
1816
  editField,
1817
1817
  field,
1818
- index
1818
+ index,
1819
+ validate
1819
1820
  } = props;
1820
1821
 
1822
+ const getLabel = () => {
1823
+ return minDash.get(field, ['values', index, 'label']);
1824
+ };
1825
+
1826
+ const getValue = () => {
1827
+ return minDash.get(field, ['values', index, 'value']);
1828
+ };
1829
+
1821
1830
  const onChange = key => {
1822
1831
  const values = minDash.get(field, ['values']);
1823
1832
  return value => {
@@ -1830,16 +1839,95 @@ function ValueEntry(props) {
1830
1839
  id: `value-label-${index}`,
1831
1840
  label: "Label",
1832
1841
  onChange: onChange('label'),
1833
- value: minDash.get(field, ['values', index, 'label'])
1842
+ value: getLabel()
1834
1843
  }), jsxRuntime.jsx(TextInputEntry, {
1835
1844
  id: `value-value-${index}`,
1836
1845
  label: "Value",
1837
1846
  onChange: onChange('value'),
1838
- value: minDash.get(field, ['values', index, 'value'])
1847
+ value: getValue(),
1848
+ validate: validate(getValue())
1839
1849
  })]
1840
1850
  });
1841
1851
  }
1842
1852
 
1853
+ function CustomValueEntry(props) {
1854
+ const {
1855
+ editField,
1856
+ field,
1857
+ index,
1858
+ validate
1859
+ } = props;
1860
+
1861
+ const getKey = () => {
1862
+ return Object.keys(minDash.get(field, ['properties']))[index];
1863
+ };
1864
+
1865
+ const getValue = () => {
1866
+ return minDash.get(field, ['properties', getKey()]);
1867
+ };
1868
+
1869
+ const onChange = key => {
1870
+ const properties = minDash.get(field, ['properties']);
1871
+ return value => {
1872
+ if (key === 'value') {
1873
+ editField(field, 'properties', updateValue(properties, getKey(), value));
1874
+ } else if (key === 'key') {
1875
+ editField(field, 'properties', updateKey(properties, getKey(), value));
1876
+ }
1877
+ };
1878
+ };
1879
+
1880
+ return jsxRuntime.jsxs(jsxRuntime.Fragment, {
1881
+ children: [jsxRuntime.jsx(TextInputEntry, {
1882
+ id: `value-key-${index}`,
1883
+ label: "Key",
1884
+ onChange: onChange('key'),
1885
+ value: getKey(),
1886
+ validate: validate(getKey())
1887
+ }), jsxRuntime.jsx(TextInputEntry, {
1888
+ id: `value-value-${index}`,
1889
+ label: "Value",
1890
+ onChange: onChange('value'),
1891
+ value: getValue()
1892
+ })]
1893
+ });
1894
+ } // helpers //////////
1895
+
1896
+ /**
1897
+ * Returns copy of object with updated value.
1898
+ *
1899
+ * @param {Object} properties
1900
+ * @param {string} key
1901
+ * @param {string} value
1902
+ *
1903
+ * @returns {Object}
1904
+ */
1905
+
1906
+ function updateValue(properties, key, value) {
1907
+ return { ...properties,
1908
+ [key]: value
1909
+ };
1910
+ }
1911
+ /**
1912
+ * Returns copy of object with updated key.
1913
+ *
1914
+ * @param {Object} properties
1915
+ * @param {string} oldKey
1916
+ * @param {string} newKey
1917
+ *
1918
+ * @returns {Object}
1919
+ */
1920
+
1921
+
1922
+ function updateKey(properties, oldKey, newKey) {
1923
+ return Object.entries(properties).reduce((newProperties, entry) => {
1924
+ const [key, value] = entry;
1925
+ return { ...newProperties,
1926
+ [key === oldKey ? newKey : key]: value
1927
+ };
1928
+ }, {});
1929
+ }
1930
+
1843
1931
  function GeneralGroup(field, editField) {
1844
1932
  const {
1845
1933
  type
@@ -1982,13 +2070,32 @@ function ValuesGroup(field, editField) {
1982
2070
  } = field;
1983
2071
 
1984
2072
  const addEntry = () => {
2073
+ const index = values.length + 1;
1985
2074
  const entry = {
1986
- label: 'Value',
1987
- value: 'value'
2075
+ label: `Value ${index}`,
2076
+ value: `value${index}`
1988
2077
  };
1989
2078
  editField(field, ['values'], arrayAdd$1(values, values.length, entry));
1990
2079
  };
1991
2080
 
2081
+ const validateFactory = key => {
2082
+ return value => {
2083
+ if (value === key) {
2084
+ return;
2085
+ }
2086
+
2087
+ if (minDash.isUndefined(value) || !value.length) {
2088
+ return 'Must not be empty.';
2089
+ }
2090
+
2091
+ const isValueAssigned = values.find(entry => entry.value === value);
2092
+
2093
+ if (isValueAssigned) {
2094
+ return 'Must be unique.';
2095
+ }
2096
+ };
2097
+ };
2098
+
1992
2099
  const hasEntries = values.length > 0;
1993
2100
  return jsxRuntime.jsx(Group, {
1994
2101
  label: "Values",
@@ -2009,11 +2116,90 @@ function ValuesGroup(field, editField) {
2009
2116
  children: jsxRuntime.jsx(ValueEntry, {
2010
2117
  editField: editField,
2011
2118
  field: field,
2012
- index: index
2119
+ index: index,
2120
+ validate: validateFactory
2121
+ })
2122
+ }, `${id}-${index}`);
2123
+ })
2124
+ });
2125
+ }
2126
+
2127
+ function CustomValuesGroup(field, editField) {
2128
+ const {
2129
+ id,
2130
+ properties = {}
2131
+ } = field;
2132
+
2133
+ const addEntry = () => {
2134
+ const index = Object.keys(properties).length + 1;
2135
+ const key = `key${index}`,
2136
+ value = 'value';
2137
+ editField(field, ['properties'], { ...properties,
2138
+ [key]: value
2139
+ });
2140
+ };
2141
+
2142
+ const validateFactory = key => {
2143
+ return value => {
2144
+ if (value === key) {
2145
+ return;
2146
+ }
2147
+
2148
+ if (minDash.isUndefined(value) || !value.length) {
2149
+ return 'Must not be empty.';
2150
+ }
2151
+
2152
+ if (minDash.has(properties, value)) {
2153
+ return 'Must be unique.';
2154
+ }
2155
+ };
2156
+ };
2157
+
2158
+ const hasEntries = Object.keys(properties).length > 0;
2159
+ return jsxRuntime.jsx(Group, {
2160
+ label: "Custom Properties",
2161
+ addEntry: addEntry,
2162
+ hasEntries: hasEntries,
2163
+ children: Object.keys(properties).map((key, index) => {
2164
+ const removeEntry = () => {
2165
+ editField(field, ['properties'], removeKey(properties, key));
2166
+ };
2167
+
2168
+ return jsxRuntime.jsx(CollapsibleEntry, {
2169
+ label: key,
2170
+ removeEntry: removeEntry,
2171
+ children: jsxRuntime.jsx(CustomValueEntry, {
2172
+ editField: editField,
2173
+ field: field,
2174
+ index: index,
2175
+ validate: validateFactory
2013
2176
  })
2014
2177
  }, `${id}-${index}`);
2015
2178
  })
2016
2179
  });
2180
+ } // helpers //////////
2181
+
2182
+ /**
2183
+ * Returns copy of object without key.
2184
+ *
2185
+ * @param {Object} properties
2186
+ * @param {string} oldKey
2187
+ *
2188
+ * @returns {Object}
2189
+ */
2190
+
2191
+ function removeKey(properties, oldKey) {
2192
+ return Object.entries(properties).reduce((newProperties, entry) => {
2193
+ const [key, value] = entry;
2194
+
2195
+ if (key === oldKey) {
2196
+ return newProperties;
2197
+ }
2198
+
2199
+ return { ...newProperties,
2200
+ [key]: value
2201
+ };
2202
+ }, {});
2017
2203
  }
2018
2204
 
2019
2205
  const labelsByType = {
@@ -2042,6 +2228,10 @@ function getGroups(field, editField) {
2042
2228
  groups.push(ValidationGroup(field, editField));
2043
2229
  }
2044
2230
 
2231
+ if (type !== 'default') {
2232
+ groups.push(CustomValuesGroup(field, editField));
2233
+ }
2234
+
2045
2235
  return groups;
2046
2236
  }
2047
2237