Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Formate la valeur affichée dans le champ NumberInput #659

Merged
merged 8 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"react-hook-form": "^7.51.2",
"react-i18next": "^14.0.5",
"react-modal": "^3.16.1",
"react-number-format": "^5.4.1",
"react-select": "^5.8.0",
"react-toastify": "^10.0.5",
"react-tooltip": "^5.26.3",
Expand Down
35 changes: 16 additions & 19 deletions src/components/form/question/NumberInput.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Trans from '@/components/translation/Trans'
import { useLocale } from '@/hooks/useLocale'
import { HTMLAttributes } from 'react'
import { DebounceInput } from 'react-debounce-input'
import { NumericFormat } from 'react-number-format'
import { twMerge } from 'tailwind-merge'

type Props = {
Expand All @@ -12,51 +12,48 @@ type Props = {
min?: number
id?: string
className?: string
defaultValue?: string | number | null | undefined
}

function unformatNumber(number: string) {
// Supprimer les séparateurs de milliers
return Number(number.replace(/[^0-9.-]+/g, ''))
}

export default function NumberInput({
unit,
value = '',
isMissing,
setValue,
min = 0,
className,
id,
...props
}: HTMLAttributes<HTMLInputElement> & Props) {
const locale = useLocale()

const handleValueChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const inputValue = event.target.value

if (inputValue === '') {
setValue(undefined)
} else {
setValue(Number(inputValue))
setValue(unformatNumber(inputValue))
}
}

return (
<div
className={twMerge(`flex items-center justify-start gap-1`, className)}>
<DebounceInput
debounceTimeout={300}
className={`focus:ring-primary max-w-[8rem] rounded-xl border-2 border-primary-200 bg-white p-2 text-right transition-colors focus:border-primary-700 focus:ring-2 md:max-w-full`}
type="number"
inputMode="numeric"
min={min}
<NumericFormat
value={isMissing ? '' : value}
placeholder={
isMissing
? value?.toLocaleString(locale, {
maximumFractionDigits: 1,
}) ?? '0'
: '0'
}
placeholder={isMissing ? '0' : ''}
className={`focus:ring-primary max-w-[8rem] rounded-xl border-2 border-primary-200 bg-white p-2 text-right transition-colors focus:border-primary-700 focus:ring-2 md:max-w-full`}
customInput={DebounceInput}
thousandSeparator={' '}
allowNegative={false}
onChange={handleValueChange}
onInput={handleValueChange}
id={id}
{...props}
/>

{unit ? (
<span className="whitespace-nowrap">
&nbsp;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8735,6 +8735,11 @@ react-move@^2.7.0:
prop-types "^15.6.2"
react-lifecycles-compat "^3.0.4"

react-number-format@^5.4.1:
version "5.4.1"
resolved "https://registry.yarnpkg.com/react-number-format/-/react-number-format-5.4.1.tgz#ca191af06c4618a823874efa486df50a1abbfc18"
integrity sha512-NICOjo/70dcAiwVmH6zMWoZrTQDlBrEXV/f7S0t/ewlpzp4z00pasg5G1yBX6NHLafwOF3QZ+VvK/XApwSKxdA==

react-select@^5.8.0:
version "5.8.0"
resolved "https://registry.yarnpkg.com/react-select/-/react-select-5.8.0.tgz#bd5c467a4df223f079dd720be9498076a3f085b5"
Expand Down
Loading