@@ -7,6 +7,8 @@ import IconButton from "../../../components/IconButton";
77import Chip from "../../../components/Chip" ;
88import { useAppDispatch } from "../../../redux/hooks" ;
99import { updateTransaction } from "../../../redux/transactions/transactionsActions" ;
10+ import Select from "../../../components/Select" ;
11+ import { TransactionCategory } from "./constants" ;
1012
1113type ItemProps = {
1214 transactionId : string ;
@@ -25,7 +27,7 @@ const Item: FC<ItemProps> = ({
2527 useChip,
2628 chipColor,
2729} ) => {
28- const { register, handleSubmit, watch } = useForm ( ) ;
30+ const { register, handleSubmit, watch, setValue } = useForm ( ) ;
2931 const [ isEditing , setIsEditing ] = useState ( false ) ;
3032
3133 const dispatch = useAppDispatch ( ) ;
@@ -34,6 +36,11 @@ const Item: FC<ItemProps> = ({
3436 setIsEditing ( true ) ;
3537 } ;
3638
39+ const handleCancel = ( ) => {
40+ setIsEditing ( false ) ;
41+ setValue ( "value" , value ) ;
42+ } ;
43+
3744 const onSubmit = ( ) => {
3845 setIsEditing ( false ) ;
3946
@@ -72,28 +79,57 @@ const Item: FC<ItemProps> = ({
7279 className = { classes [ "value-container" ] }
7380 onSubmit = { handleSubmit ( onSubmit ) }
7481 >
75- < Input
76- type = "text"
77- formProps = { register ( "value" ) }
78- inputProps = { { defaultValue : value } }
79- isDirty = { ! ! watch ( "value" ) }
80- />
82+ { label === "Category" ? (
83+ < Select
84+ options = { Object . values ( TransactionCategory ) }
85+ formProps = { register ( "value" ) }
86+ inputProps = { { defaultValue : value } }
87+ />
88+ ) : (
89+ < Input
90+ type = "text"
91+ formProps = { register ( "value" ) }
92+ inputProps = { { defaultValue : value } }
93+ isDirty = { ! ! watch ( "value" ) }
94+ />
95+ ) }
8196 < IconButton
8297 icon = { < i className = "fa fa-check" aria-hidden = "true" > </ i > }
8398 submit
8499 />
100+ < IconButton
101+ icon = { < i className = "fa fa-times" aria-hidden = "true" > </ i > }
102+ onClick = { handleCancel }
103+ />
85104 </ form >
86105 ) : (
87106 < div className = { classes [ "value-container" ] } >
88107 { useChip ? (
89- < Chip label = { value } type = { chipColor } />
108+ < Chip
109+ onClick = { handleEdit }
110+ label = {
111+ label === "Category" ? (
112+ < >
113+ { value }
114+ < IconButton
115+ icon = { < i className = "fa fa-pencil" aria-hidden = "true" > </ i > }
116+ />
117+ </ >
118+ ) : (
119+ value
120+ )
121+ }
122+ type = { chipColor }
123+ />
90124 ) : (
91125 < span className = { classes [ "value" ] } > { value } </ span >
92126 ) }
93- < IconButton
94- icon = { < i className = "fa fa-pencil" aria-hidden = "true" > </ i > }
95- onClick = { handleEdit }
96- />
127+ { label !== "Category" && (
128+ < IconButton
129+ icon = { < i className = "fa fa-pencil" aria-hidden = "true" > </ i > }
130+ onClick = { handleEdit }
131+ />
132+ ) }
97133 </ div >
98134 ) }
99135 </ div >
0 commit comments