@bpmn-io/form-js-editor 0.6.1 → 0.7.2

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.es.js CHANGED
@@ -1,7 +1,7 @@
1
- import Ids from 'ids';
2
- import { isArray, isFunction, isNumber, bind, assign, debounce, get, isUndefined, set, forEach, isObject, uniqueBy, isString } from 'min-dash';
3
1
  import { FormFieldRegistry as FormFieldRegistry$1, clone, Default, FormContext, FormRenderContext, FormComponent, FormFields, createFormContainer, createInjector, schemaVersion } from '@bpmn-io/form-js-viewer';
4
2
  export { schemaVersion } from '@bpmn-io/form-js-viewer';
3
+ import Ids from 'ids';
4
+ import { isArray, isFunction, isNumber, bind, assign, debounce, get, isUndefined, set, has, forEach, isObject, uniqueBy, isString } from 'min-dash';
5
5
  import { createContext, render } from 'preact';
6
6
  import { useContext, useRef, useEffect, useMemo, useState, useCallback } from 'preact/hooks';
7
7
  import React from 'preact/compat';
@@ -1156,7 +1156,7 @@ function TextInput(props) {
1156
1156
  const prevValue = usePrevious(value);
1157
1157
  const [cachedValue, setCachedValue] = useState(null);
1158
1158
  const [error, setError] = useState(null);
1159
- useEffect(() => setError(validate(value)), [value]);
1159
+ useEffect(() => setError(validate(value)), [validate, value]);
1160
1160
  const onInput = useDebounce(event => {
1161
1161
  const value = event.target.value;
1162
1162
  const error = validate(value);
@@ -1670,7 +1670,7 @@ function DisabledEntry(props) {
1670
1670
  } = props;
1671
1671
 
1672
1672
  const onChange = value => {
1673
- editField(field, 'disabled', value);
1673
+ editField(field, ['disabled'], value);
1674
1674
  };
1675
1675
 
1676
1676
  return jsx(CheckboxInputEntry, {
@@ -1806,9 +1806,18 @@ function ValueEntry(props) {
1806
1806
  const {
1807
1807
  editField,
1808
1808
  field,
1809
- index
1809
+ index,
1810
+ validate
1810
1811
  } = props;
1811
1812
 
1813
+ const getLabel = () => {
1814
+ return get(field, ['values', index, 'label']);
1815
+ };
1816
+
1817
+ const getValue = () => {
1818
+ return get(field, ['values', index, 'value']);
1819
+ };
1820
+
1812
1821
  const onChange = key => {
1813
1822
  const values = get(field, ['values']);
1814
1823
  return value => {
@@ -1821,14 +1830,93 @@ function ValueEntry(props) {
1821
1830
  id: `value-label-${index}`,
1822
1831
  label: "Label",
1823
1832
  onChange: onChange('label'),
1824
- value: get(field, ['values', index, 'label'])
1833
+ value: getLabel()
1834
+ }), jsx(TextInputEntry, {
1835
+ id: `value-value-${index}`,
1836
+ label: "Value",
1837
+ onChange: onChange('value'),
1838
+ value: getValue(),
1839
+ validate: validate(getValue())
1840
+ })]
1841
+ });
1842
+ }
1843
+
1844
+ function CustomValueEntry(props) {
1845
+ const {
1846
+ editField,
1847
+ field,
1848
+ index,
1849
+ validate
1850
+ } = props;
1851
+
1852
+ const getKey = () => {
1853
+ return Object.keys(get(field, ['properties']))[index];
1854
+ };
1855
+
1856
+ const getValue = () => {
1857
+ return get(field, ['properties', getKey()]);
1858
+ };
1859
+
1860
+ const onChange = key => {
1861
+ const properties = get(field, ['properties']);
1862
+ return value => {
1863
+ if (key === 'value') {
1864
+ editField(field, 'properties', updateValue(properties, getKey(), value));
1865
+ } else if (key === 'key') {
1866
+ editField(field, 'properties', updateKey(properties, getKey(), value));
1867
+ }
1868
+ };
1869
+ };
1870
+
1871
+ return jsxs(Fragment, {
1872
+ children: [jsx(TextInputEntry, {
1873
+ id: `value-key-${index}`,
1874
+ label: "Key",
1875
+ onChange: onChange('key'),
1876
+ value: getKey(),
1877
+ validate: validate(getKey())
1825
1878
  }), jsx(TextInputEntry, {
1826
1879
  id: `value-value-${index}`,
1827
1880
  label: "Value",
1828
1881
  onChange: onChange('value'),
1829
- value: get(field, ['values', index, 'value'])
1882
+ value: getValue()
1830
1883
  })]
1831
1884
  });
1885
+ } // helpers //////////
1886
+
1887
+ /**
1888
+ * Returns copy of object with updated value.
1889
+ *
1890
+ * @param {Object} properties
1891
+ * @param {string} key
1892
+ * @param {string} value
1893
+ *
1894
+ * @returns {Object}
1895
+ */
1896
+
1897
+ function updateValue(properties, key, value) {
1898
+ return { ...properties,
1899
+ [key]: value
1900
+ };
1901
+ }
1902
+ /**
1903
+ * Returns copy of object with updated key.
1904
+ *
1905
+ * @param {Object} properties
1906
+ * @param {string} oldKey
1907
+ * @param {string} newKey
1908
+ *
1909
+ * @returns {Object}
1910
+ */
1911
+
1912
+
1913
+ function updateKey(properties, oldKey, newKey) {
1914
+ return Object.entries(properties).reduce((newProperties, entry) => {
1915
+ const [key, value] = entry;
1916
+ return { ...newProperties,
1917
+ [key === oldKey ? newKey : key]: value
1918
+ };
1919
+ }, {});
1832
1920
  }
1833
1921
 
1834
1922
  function GeneralGroup(field, editField) {
@@ -1939,10 +2027,10 @@ function ValidationGroup(field, editField) {
1939
2027
  onChange: onChange('maxLength'),
1940
2028
  value: get(field, ['validate', 'maxLength'])
1941
2029
  }), jsx(TextInputEntry, {
1942
- id: "regularExpressionPattern",
2030
+ id: "pattern",
1943
2031
  label: "Regular Expression Pattern",
1944
- onChange: onChange('regularExpressionPattern'),
1945
- value: get(field, ['validate', 'regularExpressionPattern'])
2032
+ onChange: onChange('pattern'),
2033
+ value: get(field, ['validate', 'pattern'])
1946
2034
  }));
1947
2035
  }
1948
2036
 
@@ -1973,13 +2061,32 @@ function ValuesGroup(field, editField) {
1973
2061
  } = field;
1974
2062
 
1975
2063
  const addEntry = () => {
2064
+ const index = values.length + 1;
1976
2065
  const entry = {
1977
- label: 'Value',
1978
- value: 'value'
2066
+ label: `Value ${index}`,
2067
+ value: `value${index}`
1979
2068
  };
1980
2069
  editField(field, ['values'], arrayAdd$1(values, values.length, entry));
1981
2070
  };
1982
2071
 
2072
+ const validateFactory = key => {
2073
+ return value => {
2074
+ if (value === key) {
2075
+ return;
2076
+ }
2077
+
2078
+ if (isUndefined(value) || !value.length) {
2079
+ return 'Must not be empty.';
2080
+ }
2081
+
2082
+ const isValueAssigned = values.find(entry => entry.value === value);
2083
+
2084
+ if (isValueAssigned) {
2085
+ return 'Must be unique.';
2086
+ }
2087
+ };
2088
+ };
2089
+
1983
2090
  const hasEntries = values.length > 0;
1984
2091
  return jsx(Group, {
1985
2092
  label: "Values",
@@ -2000,13 +2107,92 @@ function ValuesGroup(field, editField) {
2000
2107
  children: jsx(ValueEntry, {
2001
2108
  editField: editField,
2002
2109
  field: field,
2003
- index: index
2110
+ index: index,
2111
+ validate: validateFactory
2004
2112
  })
2005
2113
  }, `${id}-${index}`);
2006
2114
  })
2007
2115
  });
2008
2116
  }
2009
2117
 
2118
+ function CustomValuesGroup(field, editField) {
2119
+ const {
2120
+ id,
2121
+ properties = {}
2122
+ } = field;
2123
+
2124
+ const addEntry = () => {
2125
+ const index = Object.keys(properties).length + 1;
2126
+ const key = `key${index}`,
2127
+ value = 'value';
2128
+ editField(field, ['properties'], { ...properties,
2129
+ [key]: value
2130
+ });
2131
+ };
2132
+
2133
+ const validateFactory = key => {
2134
+ return value => {
2135
+ if (value === key) {
2136
+ return;
2137
+ }
2138
+
2139
+ if (isUndefined(value) || !value.length) {
2140
+ return 'Must not be empty.';
2141
+ }
2142
+
2143
+ if (has(properties, value)) {
2144
+ return 'Must be unique.';
2145
+ }
2146
+ };
2147
+ };
2148
+
2149
+ const hasEntries = Object.keys(properties).length > 0;
2150
+ return jsx(Group, {
2151
+ label: "Custom Properties",
2152
+ addEntry: addEntry,
2153
+ hasEntries: hasEntries,
2154
+ children: Object.keys(properties).map((key, index) => {
2155
+ const removeEntry = () => {
2156
+ editField(field, ['properties'], removeKey(properties, key));
2157
+ };
2158
+
2159
+ return jsx(CollapsibleEntry, {
2160
+ label: key,
2161
+ removeEntry: removeEntry,
2162
+ children: jsx(CustomValueEntry, {
2163
+ editField: editField,
2164
+ field: field,
2165
+ index: index,
2166
+ validate: validateFactory
2167
+ })
2168
+ }, `${id}-${index}`);
2169
+ })
2170
+ });
2171
+ } // helpers //////////
2172
+
2173
+ /**
2174
+ * Returns copy of object without key.
2175
+ *
2176
+ * @param {Object} properties
2177
+ * @param {string} oldKey
2178
+ *
2179
+ * @returns {Object}
2180
+ */
2181
+
2182
+ function removeKey(properties, oldKey) {
2183
+ return Object.entries(properties).reduce((newProperties, entry) => {
2184
+ const [key, value] = entry;
2185
+
2186
+ if (key === oldKey) {
2187
+ return newProperties;
2188
+ }
2189
+
2190
+ return { ...newProperties,
2191
+ [key]: value
2192
+ };
2193
+ }, {});
2194
+ }
2195
+
2010
2196
  const labelsByType = {
2011
2197
  button: 'BUTTON',
2012
2198
  checkbox: 'CHECKBOX',
@@ -2033,6 +2219,10 @@ function getGroups(field, editField) {
2033
2219
  groups.push(ValidationGroup(field, editField));
2034
2220
  }
2035
2221
 
2222
+ if (type !== 'default') {
2223
+ groups.push(CustomValuesGroup(field, editField));
2224
+ }
2225
+
2036
2226
  return groups;
2037
2227
  }
2038
2228
 
@@ -4466,6 +4656,10 @@ const ids = new Ids([32, 36, 1]);
4466
4656
  * properties: FormEditorProperties,
4467
4657
  * schema: Schema
4468
4658
  * } } State
4659
+ *
4660
+ * @typedef { (type:string, priority:number, handler:Function) => void } OnEventWithPriority
4661
+ * @typedef { (type:string, handler:Function) => void } OnEventWithOutPriority
4662
+ * @typedef { OnEventWithPriority & OnEventWithOutPriority } OnEventType
4469
4663
  */
4470
4664
 
4471
4665
  /**
@@ -4478,10 +4672,16 @@ class FormEditor {
4478
4672
  * @param {FormEditorOptions} options
4479
4673
  */
4480
4674
  constructor(options = {}) {
4675
+ /**
4676
+ * @public
4677
+ * @type {OnEventType}
4678
+ */
4679
+ this.on = this._onEvent;
4481
4680
  /**
4482
4681
  * @public
4483
4682
  * @type {String}
4484
4683
  */
4684
+
4485
4685
  this._id = ids.next();
4486
4686
  /**
4487
4687
  * @private
@@ -4654,16 +4854,6 @@ class FormEditor {
4654
4854
  properties
4655
4855
  });
4656
4856
  }
4657
- /**
4658
- * @param {string} type
4659
- * @param {number} priority
4660
- * @param {Function} handler
4661
- */
4662
-
4663
-
4664
- on(type, priority, handler) {
4665
- this.get('eventBus').on(type, priority, handler);
4666
- }
4667
4857
  /**
4668
4858
  * @param {string} type
4669
4859
  * @param {Function} handler
@@ -4736,6 +4926,14 @@ class FormEditor {
4736
4926
  _getModules() {
4737
4927
  return [ModelingModule, EditorActionsModule, KeyboardModule, SelectionModule];
4738
4928
  }
4929
+ /**
4930
+ * @internal
4931
+ */
4932
+
4933
+
4934
+ _onEvent(type, priority, handler) {
4935
+ this.get('eventBus').on(type, priority, handler);
4936
+ }
4739
4937
 
4740
4938
  } // helpers //////////
4741
4939