Skip to content

Commit

Permalink
fix(floatingNumberForm): fix some issues with floating quantities in …
Browse files Browse the repository at this point in the history
…form
  • Loading branch information
Clm-Roig committed Jul 4, 2022
1 parent 93933bb commit c6ad7f9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/components/CompletionChip/CompletionChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const CompletionChip: FC<Props> = ({
<Chip
clickable={true}
color={isSelected ? 'info' : 'primary'}
label={quantity.toString() + ' ' + unit}
label={quantity + ' ' + unit}
sx={sxProps}
{...chipProps}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/TrackerCard/TrackerCardContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const TrackerCardContent: FC<Props> = ({
.map((rc) => {
const todayCompletion = aggCompletions.find((c) => rc.unit === c.unit);
if (todayCompletion) {
const remain = rc.quantity - todayCompletion.quantity;
const remain = Number((rc.quantity - todayCompletion.quantity).toFixed(2));
return {
...rc,
quantity: remain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Controller, useFieldArray, useForm } from 'react-hook-form';

import { useAutoAnimate } from '../../../hooks/useAutoAnimate';
import Completion from '../../../models/Completion';
import { validateFacultativePositiveFloat } from '../../../utils/validateNumber';
import CompletionQuantityTextField from '../completions/CompletionQuantityTextField';
import CompletionUnitTextField from '../completions/CompletionUnitTextField';
import {
Expand Down Expand Up @@ -85,43 +86,27 @@ const ValidateCompletionsForm: FC<Props> = ({ completions, formId, onSubmit }) =
control={control}
name={`completions.${index}.quantity` as const}
rules={{
min: 1,
pattern: /^\d+$/,
required: true
}}
render={({ field: { name, onChange, value }, fieldState: { error } }) => {
let errorText = '';
if (error) {
switch (error.type) {
case 'min':
errorText = 'La quantité doit être supérieure ou égale à 1.';
break;

case 'pattern':
errorText = 'La quantité doit être un nombre.';
break;

case 'required':
errorText = 'La quantité est requise';
break;
}
}
return (
<CompletionQuantityTextField
onDecrement={() => setValue(name, computeDecrementedStringQuantity(value))}
onIncrement={() => setValue(name, computeIncrementedStringQuantity(value))}
textFieldProps={{
error: !!error,
helperText: error && errorText,
onChange: onChange,
required: true,
size: 'small',
sx: { mb: 1 },
value: value || ''
}}
/>
);
min: { value: 1, message: 'La quantité doit être supérieure ou égale à 1.' },
required: { value: true, message: 'La quantité est requise.' },
validate: (value) =>
validateFacultativePositiveFloat(value) ||
'La quantité doit être un nombre positif.'
}}
render={({ field: { name, onChange, value }, fieldState: { error } }) => (
<CompletionQuantityTextField
onDecrement={() => setValue(name, computeDecrementedStringQuantity(value))}
onIncrement={() => setValue(name, computeIncrementedStringQuantity(value))}
textFieldProps={{
error: !!error,
helperText: error && error.message,
onChange: onChange,
required: true,
size: 'small',
sx: { mb: 1 },
value: value || ''
}}
/>
)}
/>
</Grid>
<Grid item xs={1}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import styled from '@emotion/styled';
import { Button, TextFieldProps } from '@mui/material';
import { Button, TextField, TextFieldProps } from '@mui/material';
import React, { FC } from 'react';

import NumberTextField from '../formElements/NumberTextField/NumberTextField';

const StyledButton = styled(Button)`
min-width: auto;
`;
const StyledTextField = styled(NumberTextField)`
const StyledTextField = styled(TextField)`
fieldset {
border-width: 0 1px 1px 0;
border-top-right-radius: 0;
Expand All @@ -26,6 +24,7 @@ const CompletionQuantityTextField: FC<Props> = ({ onDecrement, onIncrement, text
<StyledTextField
inputProps={{
min: 1,
inputMode: 'numeric',
style: {
textAlign: 'center'
}
Expand Down

This file was deleted.

0 comments on commit c6ad7f9

Please sign in to comment.