1
- import { useState } from 'react' ;
1
+ import { useState , useRef } from 'react' ;
2
2
3
3
import {
4
4
Dialog ,
@@ -12,6 +12,7 @@ import { IDialogSelectorProps } from './types';
12
12
import { RadioGroup , RadioGroupItem } from '../ui/radio-group' ;
13
13
import { Label } from '../ui/label' ;
14
14
import { Button } from '../ui/button' ;
15
+ import { Input } from '../ui/input' ;
15
16
16
17
const DialogSelector = ( props : IDialogSelectorProps ) => {
17
18
const {
@@ -21,10 +22,23 @@ const DialogSelector = (props: IDialogSelectorProps) => {
21
22
description,
22
23
options,
23
24
value,
25
+ quantity : quantityProp ,
24
26
onSave,
25
27
isSubmitting,
26
28
} = props ;
27
29
const [ selectedItem , setSelectedItem ] = useState < string > ( value ) ;
30
+ const selectedItemRef = useRef < string > ( value ) ; // to prevent dated prop values bug on the previous line
31
+ const [ quantity , setQuantity ] = useState < number > ( quantityProp ) ;
32
+ const quantityRef = useRef < number > ( quantityProp ) ;
33
+
34
+ if ( quantityRef . current !== quantityProp ) {
35
+ setQuantity ( quantityProp ) ;
36
+ quantityRef . current = quantityProp ;
37
+ }
38
+ if ( selectedItemRef . current !== value ) {
39
+ setSelectedItem ( value ) ;
40
+ selectedItemRef . current = value ;
41
+ }
28
42
29
43
return (
30
44
< Dialog open = { open } onOpenChange = { onClose } >
@@ -33,15 +47,57 @@ const DialogSelector = (props: IDialogSelectorProps) => {
33
47
< DialogTitle className = "text-base font-medium" > { title } </ DialogTitle >
34
48
{ description && < DialogDescription > { description } </ DialogDescription > }
35
49
</ DialogHeader >
50
+ < div className = "flex flex-col" >
51
+ < label htmlFor = "quantity" className = "text-muted-foreground" >
52
+ Quantidade
53
+ </ label >
54
+ < div className = "flex gap-2 items-center justify-center py-2" >
55
+ < Button
56
+ onClick = { ( event ) => {
57
+ event . preventDefault ( ) ;
58
+ setQuantity ( ( prev ) => {
59
+ const newQuantity = Math . max ( prev - 1 , 0 ) ;
60
+ return newQuantity ;
61
+ } ) ;
62
+ } }
63
+ className = "bg-blue-700 text-white hover:bg-blue-600 active:bg-blue-500 px-8"
64
+ >
65
+ -
66
+ </ Button >
67
+ < Input
68
+ type = "number"
69
+ name = "quantity"
70
+ value = { quantity }
71
+ onChange = { ( event ) => setQuantity ( + event . target . value ) }
72
+ placeholder = "Quantidade"
73
+ min = { 0 }
74
+ />
75
+ < Button
76
+ onClick = { ( event ) => {
77
+ event . preventDefault ( ) ;
78
+ setQuantity ( ( prev ) => prev + 1 ) ;
79
+ } }
80
+ className = "bg-blue-700 text-white hover:bg-blue-600 active:bg-blue-500 px-8"
81
+ >
82
+ +
83
+ </ Button >
84
+ </ div >
85
+ </ div >
36
86
< div className = "px-2 py-4 max-h-[50vh] overflow-y-auto" >
37
87
< RadioGroup
38
88
value = { selectedItem }
39
89
onValueChange = { ( v ) => setSelectedItem ( v ) }
40
90
>
41
91
{ options . map ( ( option , idx ) => (
42
92
< div key = { idx } className = "flex items-center space-x-2 py-2" >
43
- < RadioGroupItem value = { option . value } id = "r1" />
44
- < Label htmlFor = "r1" > { option . label } </ Label >
93
+ < RadioGroupItem
94
+ value = { option . value }
95
+ id = { option . value }
96
+ className = "cursor-pointer"
97
+ />
98
+ < Label htmlFor = { option . value } className = "cursor-pointer" >
99
+ { option . label }
100
+ </ Label >
45
101
</ div >
46
102
) ) }
47
103
</ RadioGroup >
@@ -50,7 +106,9 @@ const DialogSelector = (props: IDialogSelectorProps) => {
50
106
< Button
51
107
className = "w-full bg-blue-700 text-white hover:bg-blue-600 active:bg-blue-500"
52
108
size = "sm"
53
- onClick = { ( ) => ( onSave ? onSave ( selectedItem ) : undefined ) }
109
+ onClick = { ( ) =>
110
+ onSave ? onSave ( selectedItem , quantity ) : undefined
111
+ }
54
112
loading = { isSubmitting }
55
113
>
56
114
Salvar
0 commit comments