@inseefr/lunatic 0.3.3-experimental → 0.3.4-experimental

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": "@inseefr/lunatic",
3
- "version": "0.3.3-experimental",
3
+ "version": "0.3.4-experimental",
4
4
  "workersVersion": "0.2.1-experimental",
5
5
  "description": "Library of questionnaire components",
6
6
  "repository": {
@@ -173,12 +173,22 @@ const InputDeclarationsWrapper = ({
173
173
  onChange={(e) => {
174
174
  const v = e.target.value;
175
175
  const valueToFire = v === '' ? null : v;
176
+ const valueToFireForArrows =
177
+ Number.parseFloat(v).toFixed(decimals);
176
178
  if (
179
+ decimals &&
180
+ v !== '' &&
181
+ !new RegExp(`^[0-9]+(.[0-9]{1,${decimals}})?$`).test(
182
+ valueToFireForArrows
183
+ )
184
+ ) {
185
+ e.preventDefault();
186
+ } else if (
177
187
  (([null, ''].includes(v) && value.length > 0) ||
178
188
  ([null, ''].includes(value) && v.length > 0)) &&
179
189
  componentType !== 'Datepicker'
180
190
  ) {
181
- setValue(v);
191
+ setValue(valueToFire);
182
192
  handleChange({
183
193
  [U.getResponseName(response)]: valueToFire,
184
194
  });
@@ -188,11 +198,16 @@ const InputDeclarationsWrapper = ({
188
198
  'Event' &&
189
199
  roleType !== 'datepicker') ||
190
200
  // FF hack: impossible to handle arrow events
191
- (Math.abs(v - value) !== 0 && isInputNumber)
201
+ (Math.abs(v - value).toFixed(decimals) !==
202
+ Number.parseFloat(`${Math.pow(10, -decimals)}`).toFixed(
203
+ decimals
204
+ ) &&
205
+ !Number.parseInt(v, 10) &&
206
+ isInputNumber)
192
207
  ) {
193
- setValue(valueToFire);
208
+ setValue(valueToFireForArrows);
194
209
  handleChange({
195
- [U.getResponseName(response)]: valueToFire,
210
+ [U.getResponseName(response)]: valueToFireForArrows,
196
211
  });
197
212
  } else {
198
213
  if (isInputNumber) {
@@ -212,6 +227,11 @@ const InputDeclarationsWrapper = ({
212
227
  }}
213
228
  onBlur={handleChangeOnBlur}
214
229
  onFocus={handleFocusIn}
230
+ onKeyPress={(event) => {
231
+ if (decimals === 0 && !/[0-9]/.test(event.key)) {
232
+ event.preventDefault();
233
+ }
234
+ }}
215
235
  />
216
236
  {isInputNumber && unit && unitPosition === 'AFTER' && (
217
237
  <span className="unit">{unit}</span>
@@ -6,11 +6,12 @@ import { areEqual } from '../../utils/lib';
6
6
  import { getTypeControls } from '../component-wrapper/controls/validators';
7
7
  import './input.scss';
8
8
 
9
- const InputNumber = ({ numberAsTextfield, ...props }) => (
9
+ const InputNumber = ({ numberAsTextfield, decimals, ...props }) => (
10
10
  <InputDeclarationsWrapper
11
11
  type={numberAsTextfield ? 'text' : 'number'}
12
12
  roleType="input"
13
13
  {...props}
14
+ decimals={decimals || 0}
14
15
  isInputNumber
15
16
  numberAsTextfield
16
17
  validators={[getTypeControls]}