@@ -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' ;
@@ -14,6 +14,7 @@ import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownM
14
14
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem' ;
15
15
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys' ;
16
16
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside' ;
17
+ import { toSpliced } from '~/utils/array/toSpliced' ;
17
18
import { isDefined } from '~/utils/isDefined' ;
18
19
19
20
const StyledDropdownMenu = styled ( DropdownMenu ) `
@@ -35,7 +36,7 @@ export const LinksFieldInput = ({
35
36
36
37
const containerRef = useRef < HTMLDivElement > ( null ) ;
37
38
38
- const links = useMemo (
39
+ const links = useMemo < { url : string ; label : string } [ ] > (
39
40
( ) =>
40
41
[
41
42
fieldValue . primaryLinkUrl
@@ -53,51 +54,47 @@ 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 ) ;
80
75
setInputValue ( '' ) ;
81
76
82
- if ( ! links . length ) {
83
- onSubmit ?.( ( ) =>
84
- persistLinksField ( {
85
- primaryLinkUrl : inputValue ,
86
- primaryLinkLabel : '' ,
87
- secondaryLinks : [ ] ,
88
- } ) ,
89
- ) ;
77
+ const nextLinks = [ ...links , { label : '' , url : inputValue } ] ;
78
+ const [ nextPrimaryLink , ...nextSecondaryLinks ] = nextLinks ;
90
79
91
- return ;
92
- }
80
+ onSubmit ?.( ( ) =>
81
+ persistLinksField ( {
82
+ primaryLinkUrl : nextPrimaryLink . url ?? '' ,
83
+ primaryLinkLabel : nextPrimaryLink . label ?? '' ,
84
+ secondaryLinks : nextSecondaryLinks ,
85
+ } ) ,
86
+ ) ;
87
+ } ;
93
88
89
+ const handleDeleteLink = ( index : number ) => {
94
90
onSubmit ?.( ( ) =>
95
91
persistLinksField ( {
96
92
...fieldValue ,
97
- secondaryLinks : [
98
- ...( fieldValue . secondaryLinks ?? [ ] ) ,
99
- { label : '' , url : inputValue } ,
100
- ] ,
93
+ secondaryLinks : toSpliced (
94
+ fieldValue . secondaryLinks ?? [ ] ,
95
+ index - 1 ,
96
+ 1 ,
97
+ ) ,
101
98
} ) ,
102
99
) ;
103
100
} ;
@@ -108,9 +105,13 @@ export const LinksFieldInput = ({
108
105
< >
109
106
< DropdownMenuItemsContainer >
110
107
{ links . map ( ( { label, url } , index ) => (
111
- < MenuItem
108
+ < LinksFieldMenuItem
112
109
key = { index }
113
- text = { < LinkDisplay value = { { label, url } } /> }
110
+ dropdownId = { `${ hotkeyScope } -links-${ index } ` }
111
+ isPrimary = { index === 0 }
112
+ label = { label }
113
+ onDelete = { ( ) => handleDeleteLink ( index ) }
114
+ url = { url }
114
115
/>
115
116
) ) }
116
117
</ DropdownMenuItemsContainer >
@@ -124,9 +125,9 @@ export const LinksFieldInput = ({
124
125
value = { inputValue }
125
126
hotkeyScope = { hotkeyScope }
126
127
onChange = { ( event ) => setInputValue ( event . target . value ) }
127
- onEnter = { handleSubmit }
128
+ onEnter = { handleAddLink }
128
129
rightComponent = {
129
- < LightIconButton Icon = { IconPlus } onClick = { handleSubmit } />
130
+ < LightIconButton Icon = { IconPlus } onClick = { handleAddLink } />
130
131
}
131
132
/>
132
133
) : (
0 commit comments