refactor: update NumberInput usage to utilize defaultValue and improve field handling in forms
This commit is contained in:
parent
5df7e8cef8
commit
70e24b5abe
4 changed files with 59 additions and 47 deletions
|
|
@ -152,6 +152,7 @@ export const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>(
|
||||||
className="h-1/2 rounded-l-none rounded-br-none border-input border-b-[0.5px] border-l-0 px-2 focus-visible:relative"
|
className="h-1/2 rounded-l-none rounded-br-none border-input border-b-[0.5px] border-l-0 px-2 focus-visible:relative"
|
||||||
disabled={value === max}
|
disabled={value === max}
|
||||||
onClick={handleIncrement}
|
onClick={handleIncrement}
|
||||||
|
type="button"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
>
|
>
|
||||||
<ChevronUp size={15} />
|
<ChevronUp size={15} />
|
||||||
|
|
@ -161,6 +162,7 @@ export const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>(
|
||||||
className="h-1/2 rounded-l-none rounded-tr-none border-input border-t-[0.5px] border-l-0 px-2 focus-visible:relative"
|
className="h-1/2 rounded-l-none rounded-tr-none border-input border-t-[0.5px] border-l-0 px-2 focus-visible:relative"
|
||||||
disabled={value === min}
|
disabled={value === min}
|
||||||
onClick={handleDecrement}
|
onClick={handleDecrement}
|
||||||
|
type="button"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
>
|
>
|
||||||
<ChevronDown size={15} />
|
<ChevronDown size={15} />
|
||||||
|
|
|
||||||
|
|
@ -620,6 +620,7 @@ const StabileSection = () => {
|
||||||
<span className="font-semibold">Budget (da preferenze servizio):</span>
|
<span className="font-semibold">Budget (da preferenze servizio):</span>
|
||||||
<NumberInput
|
<NumberInput
|
||||||
allowNegative={false}
|
allowNegative={false}
|
||||||
|
defaultValue={value}
|
||||||
min={0}
|
min={0}
|
||||||
onValueChange={(val) => {
|
onValueChange={(val) => {
|
||||||
if (val !== undefined) {
|
if (val !== undefined) {
|
||||||
|
|
@ -628,7 +629,6 @@ const StabileSection = () => {
|
||||||
}}
|
}}
|
||||||
stepper={50}
|
stepper={50}
|
||||||
suffix=" €"
|
suffix=" €"
|
||||||
value={value}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
|
|
|
||||||
|
|
@ -1004,30 +1004,34 @@ export const AnnuncioEditForm = ({ data }: { data: AnnunciWithMedia }) => {
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="prezzo"
|
name="prezzo"
|
||||||
render={({ field }) => (
|
render={({ field }) => {
|
||||||
<FormItem>
|
const { value, onChange, ...rest } = field;
|
||||||
<div className="flex flex-wrap items-center gap-x-2">
|
|
||||||
<FormLabel htmlFor="prezzo">Prezzo</FormLabel>{" "}
|
|
||||||
<FormMessage />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<FormControl>
|
return (
|
||||||
<NumberInput
|
<FormItem>
|
||||||
suffix=" €"
|
<div className="flex flex-wrap items-center gap-x-2">
|
||||||
{...field}
|
<FormLabel htmlFor="prezzo">Prezzo</FormLabel>{" "}
|
||||||
decimalScale={2}
|
<FormMessage />
|
||||||
decimalSeparator=","
|
</div>
|
||||||
fixedDecimalScale
|
|
||||||
id="prezzo"
|
<FormControl>
|
||||||
min={0}
|
<NumberInput
|
||||||
onValueChange={(v) => {
|
decimalScale={2}
|
||||||
field.onChange((v || 0) * 100);
|
{...rest}
|
||||||
}}
|
decimalSeparator=","
|
||||||
value={Number(field.value) / 100}
|
defaultValue={Number(value) / 100}
|
||||||
/>
|
fixedDecimalScale
|
||||||
</FormControl>
|
id="prezzo"
|
||||||
</FormItem>
|
min={0}
|
||||||
)}
|
onValueChange={(v) => {
|
||||||
|
onChange((Number(v) || 0) * 100);
|
||||||
|
}}
|
||||||
|
suffix=" €"
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
children2={
|
children2={
|
||||||
|
|
|
||||||
|
|
@ -409,30 +409,36 @@ export const FormPrezzo = ({
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="prezzo_cent"
|
name="prezzo_cent"
|
||||||
render={({ field }) => (
|
render={({ field }) => {
|
||||||
<FormItem>
|
const { value, onChange, ...rest } = field;
|
||||||
<div className="flex flex-wrap items-center gap-x-2">
|
|
||||||
<FormLabel htmlFor="prezzo">Prezzo</FormLabel>
|
return (
|
||||||
<FormMessage />
|
<FormItem>
|
||||||
</div>
|
<div className="flex flex-wrap items-center gap-x-2">
|
||||||
<FormControl>
|
<FormLabel htmlFor="prezzo">Prezzo</FormLabel>{" "}
|
||||||
<NumberInput
|
<FormMessage />
|
||||||
suffix=" €"
|
</div>
|
||||||
{...field}
|
|
||||||
decimalScale={2}
|
<FormControl>
|
||||||
decimalSeparator=","
|
<NumberInput
|
||||||
fixedDecimalScale
|
decimalScale={2}
|
||||||
id="prezzo"
|
{...rest}
|
||||||
min={0}
|
decimalSeparator=","
|
||||||
onValueChange={(v) => {
|
defaultValue={Number(value) / 100}
|
||||||
field.onChange((v || 0) * 100);
|
fixedDecimalScale
|
||||||
}}
|
id="prezzo"
|
||||||
value={Number(field.value) / 100}
|
min={0}
|
||||||
/>
|
onValueChange={(v) => {
|
||||||
</FormControl>
|
onChange((Number(v) || 0) * 100);
|
||||||
</FormItem>
|
}}
|
||||||
)}
|
suffix=" €"
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="sconto"
|
name="sconto"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue