@try-at-software/input-elements 1.2.2 → 1.2.4

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.
@@ -5,6 +5,7 @@ export interface IInvalidationOptions {
5
5
  export interface ISingleValueInputElementProps<TValue> {
6
6
  value: TValue;
7
7
  label: FormText;
8
+ isRequired: boolean;
8
9
  renderRequiredIndicator: boolean;
9
10
  errorMessage: FormText;
10
11
  onChange: (newValue: TValue) => void;
@@ -1,7 +1,11 @@
1
+ import { FormText } from '../../Components';
1
2
  import { IBaseInputElementProps } from '../IBaseInputElementProps';
2
3
  export interface INumberInputProps extends IBaseInputElementProps {
3
4
  handleDecimalValues?: boolean;
4
5
  min?: number;
5
6
  max?: number;
6
7
  step?: number;
8
+ getMinErrorMessage?: (min: number, max: number | undefined) => FormText;
9
+ getMaxErrorMessage?: (min: number | undefined, max: number) => FormText;
10
+ decimalErrorMessage?: FormText;
7
11
  }
@@ -12,6 +12,9 @@ interface INumberInputState {
12
12
  export declare class NumberInput extends React.Component<ISingleValueInputElementProps<number> & IOperativeProps<INumberInputProps> & IDynamicProps<IBaseInputElementDynamicProps>, INumberInputState> {
13
13
  state: INumberInputState;
14
14
  render(): JSX.Element;
15
+ private handleChange;
16
+ private handleIncrement;
17
+ private handleDecrement;
15
18
  private handleUserInput;
16
19
  private ensureValueConsistency;
17
20
  private getMaxValue;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NumberInput = void 0;
4
- const react_1 = require("@fluentui/react");
5
4
  const React = require("react");
5
+ const react_1 = require("@fluentui/react");
6
6
  const Components_1 = require("../../Components");
7
7
  const delimiter = '.';
8
8
  class NumberInput extends React.Component {
@@ -13,63 +13,72 @@ class NumberInput extends React.Component {
13
13
  intermediateValue: (_a = this.props.value) === null || _a === void 0 ? void 0 : _a.toString(),
14
14
  customWarning: ''
15
15
  };
16
+ this.handleChange = (event) => {
17
+ const newValue = event.target.value;
18
+ const value = NumberInput.normalizeData(newValue);
19
+ const { isRequired } = this.props;
20
+ console.log(isRequired, value);
21
+ if (!isRequired && !value) {
22
+ this.setState({ intermediateValue: '', customWarning: '' });
23
+ this.props.onChange(undefined);
24
+ return;
25
+ }
26
+ if (!value || isNaN(Number(value))) {
27
+ this.setState({ intermediateValue: value, customWarning: 'The provided value is not a valid number.' });
28
+ this.props.invalidateInput();
29
+ return;
30
+ }
31
+ const numericValue = NumberInput.getNumericValue(value);
32
+ this.handleUserInput(numericValue, value);
33
+ };
34
+ this.handleIncrement = (value) => {
35
+ let numericValue = NumberInput.getNumericValue(value);
36
+ numericValue += this.getStep();
37
+ this.handleUserInput(numericValue, numericValue.toString());
38
+ };
39
+ this.handleDecrement = (value) => {
40
+ let numericValue = NumberInput.getNumericValue(value);
41
+ numericValue -= this.getStep();
42
+ this.handleUserInput(numericValue, numericValue.toString());
43
+ };
16
44
  }
17
45
  render() {
18
46
  var _a;
19
47
  if (!this.props)
20
48
  return null;
21
- const { dynamicProps, operativeProps } = this.props;
49
+ const { dynamicProps, errorMessage, operativeProps } = this.props;
50
+ const { customWarning } = this.state;
22
51
  return (React.createElement(React.Fragment, null,
23
52
  React.createElement(Components_1.LabelRenderer, { label: this.props.label, required: !!this.props.renderRequiredIndicator }),
24
53
  React.createElement(react_1.SpinButton, { "data-automationid": "number-input", inputProps: {
25
54
  autoFocus: !!operativeProps.autoFocus,
26
55
  placeholder: operativeProps.placeholder,
27
56
  disabled: !!dynamicProps.isDisabled,
28
- onChange: (event) => {
29
- const newValue = event.target.value;
30
- const value = NumberInput.normalizeData(newValue);
31
- if (!value || isNaN(Number(value))) {
32
- this.setState({ intermediateValue: value, customWarning: 'The provided value is not a valid number.' });
33
- this.props.invalidateInput();
34
- return;
35
- }
36
- const numericValue = NumberInput.getNumericValue(value);
37
- this.handleUserInput(numericValue, value);
38
- }
39
- }, value: (_a = this.state.intermediateValue) !== null && _a !== void 0 ? _a : '', onIncrement: (value) => {
40
- let numericValue = NumberInput.getNumericValue(value);
41
- numericValue += this.getStep();
42
- this.handleUserInput(numericValue, numericValue.toString());
43
- }, onDecrement: (value) => {
44
- let numericValue = NumberInput.getNumericValue(value);
45
- numericValue -= this.getStep();
46
- this.handleUserInput(numericValue, numericValue.toString());
47
- } }),
48
- React.createElement(Components_1.ErrorRenderer, { error: this.props.errorMessage, messageBarType: react_1.MessageBarType.warning }),
49
- React.createElement(Components_1.ErrorRenderer, { error: this.state.customWarning, messageBarType: react_1.MessageBarType.warning })));
57
+ onChange: this.handleChange
58
+ }, value: (_a = this.state.intermediateValue) !== null && _a !== void 0 ? _a : '', onIncrement: this.handleIncrement, onDecrement: this.handleDecrement }),
59
+ React.createElement(Components_1.ErrorRenderer, { error: errorMessage, messageBarType: react_1.MessageBarType.warning }),
60
+ React.createElement(Components_1.ErrorRenderer, { error: customWarning, messageBarType: react_1.MessageBarType.warning })));
50
61
  }
51
62
  handleUserInput(value, intermediateValue) {
52
- const changeResult = this.ensureValueConsistency(value);
53
- this.setState({ customWarning: changeResult.warning, intermediateValue: intermediateValue });
54
- if (changeResult.newValue !== undefined)
55
- this.props.onChange(changeResult.newValue);
63
+ const consistencyWarning = this.ensureValueConsistency(value);
64
+ this.setState({ customWarning: consistencyWarning, intermediateValue: intermediateValue });
65
+ if (consistencyWarning === undefined)
66
+ this.props.onChange(value);
56
67
  else
57
68
  this.props.invalidateInput();
58
69
  }
59
70
  ensureValueConsistency(value) {
71
+ var _a, _b, _c, _d, _e;
72
+ const { operativeProps } = this.props;
60
73
  const maxValue = this.getMaxValue();
61
- if (value > maxValue)
62
- return { warning: `The maximum value is ${maxValue}`, newValue: undefined };
63
74
  const minValue = this.getMinValue();
75
+ if (value > maxValue)
76
+ return (_b = (_a = operativeProps.getMaxErrorMessage) === null || _a === void 0 ? void 0 : _a.call(operativeProps, minValue, maxValue)) !== null && _b !== void 0 ? _b : `The maximum value is ${maxValue}`;
64
77
  if (value < minValue)
65
- return { warning: `The minimum value is ${minValue}`, newValue: undefined };
66
- const { operativeProps } = this.props;
78
+ return (_d = (_c = operativeProps.getMinErrorMessage) === null || _c === void 0 ? void 0 : _c.call(operativeProps, minValue, maxValue)) !== null && _d !== void 0 ? _d : `The minimum value is ${minValue}`;
67
79
  if (!operativeProps.handleDecimalValues && value % 1 !== 0)
68
- return {
69
- warning: 'Decimal values are not allowed',
70
- newValue: undefined
71
- };
72
- return { warning: '', newValue: value };
80
+ return (_e = operativeProps.decimalErrorMessage) !== null && _e !== void 0 ? _e : 'Decimal values are not allowed';
81
+ return undefined;
73
82
  }
74
83
  getMaxValue() {
75
84
  var _a;
@@ -102,7 +102,8 @@ class SingleValueInputElement extends ExtendedInputElement_1.ExtendedInputElemen
102
102
  React.createElement(SingleValueInputElementWrapper_1.SingleValueInputElementWrapper, { internalComponent: this.componentToRender, renderErrors: (_b = this._configuration) === null || _b === void 0 ? void 0 : _b.renderErrors, loadingIndicator: (_c = this._configuration) === null || _c === void 0 ? void 0 : _c.loadingComponent, inputProps: {
103
103
  label: (_d = this._configuration) === null || _d === void 0 ? void 0 : _d.label,
104
104
  value: this.value,
105
- renderRequiredIndicator: ((_e = this._configuration) === null || _e === void 0 ? void 0 : _e.renderRequiredIndicator) && ((_f = this._configuration) === null || _f === void 0 ? void 0 : _f.isRequired),
105
+ isRequired: this._configuration.isRequired,
106
+ renderRequiredIndicator: ((_e = this._configuration) === null || _e === void 0 ? void 0 : _e.isRequired) && ((_f = this._configuration) === null || _f === void 0 ? void 0 : _f.renderRequiredIndicator),
106
107
  errorMessage: this.errorMessage,
107
108
  onChange: (newValue) => this.setValue(newValue),
108
109
  invalidateInput: this.invalidateInput
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@try-at-software/input-elements",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "A package providing different input elements that are extensible and easily configurable for your custom needs.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",