@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.es.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import Ids from 'ids';
2
- import { isArray, isFunction, isNumber, bind, assign, debounce, get, isUndefined, set, forEach, isObject, uniqueBy, isString } from 'min-dash';
2
+ import { isArray, isFunction, isNumber, bind, assign, debounce, get, isUndefined, set, has, forEach, isObject, uniqueBy, isString } from 'min-dash';
3
3
  import { FormFieldRegistry as FormFieldRegistry$1, clone, Default, FormContext, FormRenderContext, FormComponent, FormFields, createFormContainer, createInjector, schemaVersion } from '@bpmn-io/form-js-viewer';
4
4
  export { schemaVersion } from '@bpmn-io/form-js-viewer';
5
5
  import { createContext, render } from 'preact';
@@ -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);
@@ -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,16 +1830,95 @@ 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()
1825
1834
  }), jsx(TextInputEntry, {
1826
1835
  id: `value-value-${index}`,
1827
1836
  label: "Value",
1828
1837
  onChange: onChange('value'),
1829
- value: get(field, ['values', index, 'value'])
1838
+ value: getValue(),
1839
+ validate: validate(getValue())
1830
1840
  })]
1831
1841
  });
1832
1842
  }
1833
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())
1878
+ }), jsx(TextInputEntry, {
1879
+ id: `value-value-${index}`,
1880
+ label: "Value",
1881
+ onChange: onChange('value'),
1882
+ value: getValue()
1883
+ })]
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
+ }, {});
1920
+ }
1921
+
1834
1922
  function GeneralGroup(field, editField) {
1835
1923
  const {
1836
1924
  type
@@ -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,11 +2107,90 @@ 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
2112
+ })
2113
+ }, `${id}-${index}`);
2114
+ })
2115
+ });
2116
+ }
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
2004
2167
  })
2005
2168
  }, `${id}-${index}`);
2006
2169
  })
2007
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
+ }, {});
2008
2194
  }
2009
2195
 
2010
2196
  const labelsByType = {
@@ -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