@@ -4,8 +4,8 @@ import { Key } from 'ts-key-enum';
4
4
import { IconPlus } from 'twenty-ui' ;
5
5
6
6
import { useLinksField } from '@/object-record/record-field/meta-types/hooks/useLinksField' ;
7
+ import { LinksFieldMenuItem } from '@/object-record/record-field/meta-types/input/components/LinksFieldMenuItem' ;
7
8
import { FieldInputEvent } from '@/object-record/record-field/types/FieldInputEvent' ;
8
- import { LinkDisplay } from '@/ui/field/display/components/LinkDisplay' ;
9
9
import { LightIconButton } from '@/ui/input/button/components/LightIconButton' ;
10
10
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu' ;
11
11
import { DropdownMenuInput } from '@/ui/layout/dropdown/components/DropdownMenuInput' ;
@@ -35,13 +35,14 @@ export const LinksFieldInput = ({
35
35
36
36
const containerRef = useRef < HTMLDivElement > ( null ) ;
37
37
38
- const links = useMemo (
38
+ const links = useMemo < { url : string ; label : string ; isPrimary ?: boolean } [ ] > (
39
39
( ) =>
40
40
[
41
41
fieldValue . primaryLinkUrl
42
42
? {
43
43
url : fieldValue . primaryLinkUrl ,
44
44
label : fieldValue . primaryLinkLabel ,
45
+ isPrimary : true ,
45
46
}
46
47
: null ,
47
48
...( fieldValue . secondaryLinks ?? [ ] ) ,
@@ -53,27 +54,21 @@ export const LinksFieldInput = ({
53
54
] ,
54
55
) ;
55
56
57
+ const handleDropdownClose = ( ) => {
58
+ onCancel ?.( ) ;
59
+ } ;
60
+
56
61
useListenClickOutside ( {
57
62
refs : [ containerRef ] ,
58
- callback : ( event ) => {
59
- event . stopImmediatePropagation ( ) ;
60
-
61
- const isTargetInput =
62
- event . target instanceof HTMLInputElement &&
63
- event . target . tagName === 'INPUT' ;
64
-
65
- if ( ! isTargetInput ) {
66
- onCancel ?.( ) ;
67
- }
68
- } ,
63
+ callback : handleDropdownClose ,
69
64
} ) ;
70
65
66
+ useScopedHotkeys ( Key . Escape , handleDropdownClose , hotkeyScope ) ;
67
+
71
68
const [ isInputDisplayed , setIsInputDisplayed ] = useState ( false ) ;
72
69
const [ inputValue , setInputValue ] = useState ( '' ) ;
73
70
74
- useScopedHotkeys ( Key . Escape , onCancel ?? ( ( ) => { } ) , hotkeyScope ) ;
75
-
76
- const handleSubmit = ( ) => {
71
+ const handleAddLink = ( ) => {
77
72
if ( ! inputValue ) return ;
78
73
79
74
setIsInputDisplayed ( false ) ;
@@ -102,15 +97,31 @@ export const LinksFieldInput = ({
102
97
) ;
103
98
} ;
104
99
100
+ const handleDeleteLink = ( index : number ) => {
101
+ const nextSecondaryLinks = [ ...( fieldValue . secondaryLinks ?? [ ] ) ] ;
102
+ nextSecondaryLinks . splice ( index - 1 , 1 ) ;
103
+
104
+ onSubmit ?.( ( ) =>
105
+ persistLinksField ( {
106
+ ...fieldValue ,
107
+ secondaryLinks : nextSecondaryLinks ,
108
+ } ) ,
109
+ ) ;
110
+ } ;
111
+
105
112
return (
106
113
< StyledDropdownMenu ref = { containerRef } width = { 200 } >
107
114
{ ! ! links . length && (
108
115
< >
109
116
< DropdownMenuItemsContainer >
110
- { links . map ( ( { label, url } , index ) => (
111
- < MenuItem
117
+ { links . map ( ( { isPrimary , label, url } , index ) => (
118
+ < LinksFieldMenuItem
112
119
key = { index }
113
- text = { < LinkDisplay value = { { label, url } } /> }
120
+ dropdownId = { `${ hotkeyScope } -links-${ index } ` }
121
+ isPrimary = { isPrimary }
122
+ label = { label }
123
+ onDelete = { ( ) => handleDeleteLink ( index ) }
124
+ url = { url }
114
125
/>
115
126
) ) }
116
127
</ DropdownMenuItemsContainer >
@@ -124,9 +135,9 @@ export const LinksFieldInput = ({
124
135
value = { inputValue }
125
136
hotkeyScope = { hotkeyScope }
126
137
onChange = { ( event ) => setInputValue ( event . target . value ) }
127
- onEnter = { handleSubmit }
138
+ onEnter = { handleAddLink }
128
139
rightComponent = {
129
- < LightIconButton Icon = { IconPlus } onClick = { handleSubmit } />
140
+ < LightIconButton Icon = { IconPlus } onClick = { handleAddLink } />
130
141
}
131
142
/>
132
143
) : (
0 commit comments