@pie-lib/config-ui 10.10.4-next.289 → 10.10.4-next.306

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/config-ui",
3
- "version": "10.10.4-next.289+d87c857d",
3
+ "version": "10.10.4-next.306+d4c12e9b",
4
4
  "main": "lib/index.js",
5
5
  "module": "src/index.js",
6
6
  "publishConfig": {
@@ -10,9 +10,9 @@
10
10
  "dependencies": {
11
11
  "@material-ui/core": "^3.8.3",
12
12
  "@material-ui/icons": "^3.0.2",
13
- "@pie-lib/editable-html": "^7.17.3",
13
+ "@pie-lib/editable-html": "^7.17.4-next.306+d4c12e9b",
14
14
  "@pie-lib/icons": "^2.4.25",
15
- "@pie-lib/render-ui": "^4.12.0",
15
+ "@pie-lib/render-ui": "^4.12.1-next.306+d4c12e9b",
16
16
  "classnames": "^2.2.6",
17
17
  "debug": "^4.1.1",
18
18
  "lodash": "^4.17.11",
@@ -26,5 +26,5 @@
26
26
  "peerDependencies": {
27
27
  "react": "^16.8.1"
28
28
  },
29
- "gitHead": "d87c857d4acfff770a3d9353bc549b6544451d5d"
29
+ "gitHead": "d4c12e9bebddc8c13f218c3fc301d02ea8e98b8c"
30
30
  }
@@ -46,6 +46,7 @@ export class NumberTextFieldCustom extends React.Component {
46
46
  static propTypes = {
47
47
  classes: PropTypes.object.isRequired,
48
48
  className: PropTypes.string,
49
+ customValues: PropTypes.array,
49
50
  disabled: PropTypes.bool,
50
51
  error: PropTypes.bool,
51
52
  inputClassName: PropTypes.string,
@@ -62,6 +63,7 @@ export class NumberTextFieldCustom extends React.Component {
62
63
 
63
64
  static defaultProps = {
64
65
  step: 1,
66
+ customValues: [],
65
67
  textAlign: 'center',
66
68
  variant: 'standard',
67
69
  onlyIntegersAllowed: false
@@ -70,10 +72,18 @@ export class NumberTextFieldCustom extends React.Component {
70
72
  constructor(props) {
71
73
  super(props);
72
74
 
73
- const value = this.clamp(props.value);
75
+ let value = this.clamp(props.value);
76
+ let currentIndex = (props.customValues || []).findIndex(val => val === value);
77
+
78
+ if ((props.customValues || []).length > 0 && currentIndex === -1) {
79
+ const closestValue = this.getClosestValue(value);
80
+ value = closestValue.value;
81
+ currentIndex = closestValue.index;
82
+ }
74
83
 
75
84
  this.state = {
76
- value
85
+ value,
86
+ currentIndex
77
87
  };
78
88
 
79
89
  if (value !== props.value) {
@@ -90,7 +100,11 @@ export class NumberTextFieldCustom extends React.Component {
90
100
  }
91
101
 
92
102
  clamp(value) {
93
- const { min, max } = this.props;
103
+ const { min, max, customValues } = this.props;
104
+
105
+ if ((customValues || []).length > 0) {
106
+ return value;
107
+ }
94
108
 
95
109
  if (!isFinite(value)) {
96
110
  return fallbackNumber(min, max);
@@ -107,14 +121,37 @@ export class NumberTextFieldCustom extends React.Component {
107
121
  return value;
108
122
  }
109
123
 
124
+ getClosestValue = number => {
125
+ const { customValues } = this.props;
126
+
127
+ return customValues.reduce(
128
+ (closest, value, index) =>
129
+ Math.abs(value - number) < Math.abs(closest.value - number) ? { value, index } : closest,
130
+ { value: customValues[0], index: 0 }
131
+ );
132
+ };
133
+
110
134
  onBlur = event => {
111
- const { onlyIntegersAllowed } = this.props;
135
+ const { customValues, onlyIntegersAllowed } = this.props;
112
136
  const { value } = event.target;
113
137
  const rawNumber = onlyIntegersAllowed ? parseInt(value) : parseFloat(value);
114
- const number = this.clamp(rawNumber);
138
+ let number = this.clamp(rawNumber);
139
+ let updatedIndex = (customValues || []).findIndex(val => val === number);
140
+
141
+ if (customValues.length > 0 && updatedIndex === -1) {
142
+ const closestValue = this.getClosestValue(number);
143
+ number = closestValue.value;
144
+ updatedIndex = closestValue.index;
145
+ }
115
146
 
116
147
  if (number !== this.state.value) {
117
- this.setState({ value: number.toString() }, () => this.props.onChange(event, number));
148
+ this.setState(
149
+ {
150
+ value: number.toString(),
151
+ currentIndex: updatedIndex
152
+ },
153
+ () => this.props.onChange(event, number)
154
+ );
118
155
  }
119
156
  };
120
157
 
@@ -127,17 +164,34 @@ export class NumberTextFieldCustom extends React.Component {
127
164
  changeValue(event, sign = 1, shouldUpdate = false) {
128
165
  event.preventDefault();
129
166
 
130
- const { step, onlyIntegersAllowed, onChange } = this.props;
131
- const { value } = this.state;
132
- const rawNumber = onlyIntegersAllowed ? parseInt(value) : parseFloat(value);
133
- const updatedValue = (rawNumber * 10000 + step * sign * 10000) / 10000;
134
- const number = this.clamp(updatedValue);
167
+ const { customValues, step, onlyIntegersAllowed, onChange } = this.props;
168
+ const { currentIndex, value } = this.state;
169
+ const updatedIndex = currentIndex + sign * 1;
170
+ let number;
135
171
 
136
- this.setState({ value: number.toString() }, () => {
137
- if (shouldUpdate) {
138
- onChange(event, number);
172
+ if (customValues.length > 0) {
173
+ if (updatedIndex < 0 || updatedIndex >= customValues.length) {
174
+ return;
139
175
  }
140
- });
176
+
177
+ number = customValues[updatedIndex];
178
+ } else {
179
+ const rawNumber = onlyIntegersAllowed ? parseInt(value) : parseFloat(value);
180
+ const updatedValue = (rawNumber * 10000 + step * sign * 10000) / 10000;
181
+ number = this.clamp(updatedValue);
182
+ }
183
+
184
+ this.setState(
185
+ {
186
+ value: number.toString(),
187
+ currentIndex: updatedIndex
188
+ },
189
+ () => {
190
+ if (shouldUpdate) {
191
+ onChange(event, number);
192
+ }
193
+ }
194
+ );
141
195
  }
142
196
 
143
197
  render() {