@bpmn-io/properties-panel 1.1.1 → 1.3.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.
@@ -455,6 +455,10 @@ textarea.bio-properties-panel-input,
455
455
  width: 100%;
456
456
  }
457
457
 
458
+ textarea.bio-properties-panel-input.auto-resize {
459
+ min-height: 28px;
460
+ }
461
+
458
462
  .bio-properties-panel-input:focus,
459
463
  .bio-properties-panel-input:focus-within {
460
464
  outline: none;
package/dist/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useContext, useRef, useEffect, useMemo, useState, useCallback } from '../preact/hooks';
1
+ import { useContext, useRef, useEffect, useMemo, useState, useCallback, useLayoutEffect } from '../preact/hooks';
2
2
  import { isFunction, isArray, get, assign, set, sortBy, find, isNumber, debounce } from 'min-dash';
3
3
  import classnames from 'classnames';
4
4
  import { forwardRef } from '../preact/compat';
@@ -2033,6 +2033,7 @@ function NumberField(props) {
2033
2033
  * @param {Function} props.onFocus
2034
2034
  * @param {Function} props.onBlur
2035
2035
  * @param {String} props.step
2036
+ * @param {Function} props.validate
2036
2037
  */
2037
2038
  function NumberFieldEntry(props) {
2038
2039
  const {
@@ -2048,25 +2049,55 @@ function NumberFieldEntry(props) {
2048
2049
  setValue,
2049
2050
  step,
2050
2051
  onFocus,
2051
- onBlur
2052
+ onBlur,
2053
+ validate
2052
2054
  } = props;
2053
- const value = getValue(element);
2055
+ const [cachedInvalidValue, setCachedInvalidValue] = useState(null);
2056
+ const globalError = useError(id);
2057
+ const [localError, setLocalError] = useState(null);
2058
+ let value = getValue(element);
2059
+ const previousValue = usePrevious(value);
2060
+ useEffect(() => {
2061
+ if (isFunction(validate)) {
2062
+ const newValidationError = validate(value) || null;
2063
+ setLocalError(newValidationError);
2064
+ }
2065
+ }, [value]);
2066
+ const onInput = newValue => {
2067
+ let newValidationError = null;
2068
+ if (isFunction(validate)) {
2069
+ newValidationError = validate(newValue) || null;
2070
+ }
2071
+ if (newValidationError) {
2072
+ setCachedInvalidValue(newValue);
2073
+ } else {
2074
+ setValue(newValue);
2075
+ }
2076
+ setLocalError(newValidationError);
2077
+ };
2078
+ if (previousValue === value && localError) {
2079
+ value = cachedInvalidValue;
2080
+ }
2081
+ const error = globalError || localError;
2054
2082
  return jsxs("div", {
2055
- class: "bio-properties-panel-entry",
2083
+ class: classnames('bio-properties-panel-entry', error ? 'has-error' : ''),
2056
2084
  "data-entry-id": id,
2057
2085
  children: [jsx(NumberField, {
2058
2086
  debounce: debounce,
2059
2087
  disabled: disabled,
2060
2088
  id: id,
2061
2089
  label: label,
2062
- onInput: setValue,
2063
2090
  onFocus: onFocus,
2064
2091
  onBlur: onBlur,
2092
+ onInput: onInput,
2065
2093
  max: max,
2066
2094
  min: min,
2067
2095
  step: step,
2068
2096
  value: value
2069
- }, element), jsx(Description, {
2097
+ }, element), error && jsx("div", {
2098
+ class: "bio-properties-panel-error",
2099
+ children: error
2100
+ }), jsx(Description, {
2070
2101
  forId: id,
2071
2102
  element: element,
2072
2103
  value: description
@@ -2255,6 +2286,10 @@ function prefixId$3(id) {
2255
2286
  return `bio-properties-panel-${id}`;
2256
2287
  }
2257
2288
 
2289
+ function resizeToContents(element) {
2290
+ element.style.height = 'auto';
2291
+ element.style.height = Math.min(element.scrollHeight + 2, 150) + 'px';
2292
+ }
2258
2293
  function TextArea(props) {
2259
2294
  const {
2260
2295
  id,
@@ -2266,7 +2301,8 @@ function TextArea(props) {
2266
2301
  disabled,
2267
2302
  monospace,
2268
2303
  onFocus,
2269
- onBlur
2304
+ onBlur,
2305
+ autoResize
2270
2306
  } = props;
2271
2307
  const [localValue, setLocalValue] = useState(value);
2272
2308
  const ref = useShowEntryEvent(id);
@@ -2277,8 +2313,12 @@ function TextArea(props) {
2277
2313
  }, [onInput, debounce]);
2278
2314
  const handleInput = e => {
2279
2315
  handleInputCallback(e);
2316
+ autoResize && resizeToContents(e.target);
2280
2317
  setLocalValue(e.target.value);
2281
2318
  };
2319
+ useLayoutEffect(() => {
2320
+ autoResize && resizeToContents(ref.current);
2321
+ }, []);
2282
2322
  useEffect(() => {
2283
2323
  if (value === localValue) {
2284
2324
  return;
@@ -2296,7 +2336,7 @@ function TextArea(props) {
2296
2336
  id: prefixId$2(id),
2297
2337
  name: id,
2298
2338
  spellCheck: "false",
2299
- class: classnames('bio-properties-panel-input', monospace ? 'bio-properties-panel-input-monospace' : ''),
2339
+ class: classnames('bio-properties-panel-input', monospace ? 'bio-properties-panel-input-monospace' : '', autoResize ? 'auto-resize' : ''),
2300
2340
  onInput: handleInput,
2301
2341
  onFocus: onFocus,
2302
2342
  onBlur: onBlur,
@@ -2336,7 +2376,8 @@ function TextAreaEntry(props) {
2336
2376
  monospace,
2337
2377
  disabled,
2338
2378
  onFocus,
2339
- onBlur
2379
+ onBlur,
2380
+ autoResize
2340
2381
  } = props;
2341
2382
  const value = getValue(element);
2342
2383
  const error = useError(id);
@@ -2353,7 +2394,8 @@ function TextAreaEntry(props) {
2353
2394
  rows: rows,
2354
2395
  debounce: debounce,
2355
2396
  monospace: monospace,
2356
- disabled: disabled
2397
+ disabled: disabled,
2398
+ autoResize: autoResize
2357
2399
  }, element), error && jsx("div", {
2358
2400
  class: "bio-properties-panel-error",
2359
2401
  children: error