-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c498bc1
commit 9770ec9
Showing
12 changed files
with
229 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 114 additions & 79 deletions
193
...nt/src/modules/object-record/record-field/meta-types/input/components/LinksFieldInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,134 @@ | ||
import { useMemo, useRef, useState } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { Key } from 'ts-key-enum'; | ||
import { IconPlus } from 'twenty-ui'; | ||
|
||
import { useLinksField } from '@/object-record/record-field/meta-types/hooks/useLinksField'; | ||
import { FieldInputOverlay } from '@/ui/field/input/components/FieldInputOverlay'; | ||
import { TextInput } from '@/ui/field/input/components/TextInput'; | ||
import { LinkDisplay } from '@/ui/field/display/components/LinkDisplay'; | ||
import { LightIconButton } from '@/ui/input/button/components/LightIconButton'; | ||
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu'; | ||
import { DropdownMenuInput } from '@/ui/layout/dropdown/components/DropdownMenuInput'; | ||
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer'; | ||
import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator'; | ||
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem'; | ||
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys'; | ||
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
import { FieldInputEvent } from './DateTimeFieldInput'; | ||
const StyledDropdownMenu = styled(DropdownMenu)` | ||
left: -1px; | ||
position: absolute; | ||
top: -1px; | ||
`; | ||
|
||
export type LinksFieldInputProps = { | ||
onClickOutside?: FieldInputEvent; | ||
onEnter?: FieldInputEvent; | ||
onEscape?: FieldInputEvent; | ||
onTab?: FieldInputEvent; | ||
onShiftTab?: FieldInputEvent; | ||
onCancel?: () => void; | ||
}; | ||
|
||
export const LinksFieldInput = ({ | ||
onEnter, | ||
onEscape, | ||
onClickOutside, | ||
onTab, | ||
onShiftTab, | ||
}: LinksFieldInputProps) => { | ||
const { draftValue, setDraftValue, hotkeyScope, persistLinksField } = | ||
useLinksField(); | ||
|
||
const handleEnter = (url: string) => { | ||
onEnter?.(() => | ||
persistLinksField({ | ||
primaryLinkUrl: url, | ||
primaryLinkLabel: '', | ||
secondaryLinks: [], | ||
}), | ||
); | ||
}; | ||
export const LinksFieldInput = ({ onCancel }: LinksFieldInputProps) => { | ||
const { persistLinksField, hotkeyScope, fieldValue } = useLinksField(); | ||
|
||
const handleEscape = (url: string) => { | ||
onEscape?.(() => | ||
persistLinksField({ | ||
primaryLinkUrl: url, | ||
primaryLinkLabel: '', | ||
secondaryLinks: [], | ||
}), | ||
); | ||
}; | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
const handleClickOutside = (event: MouseEvent | TouchEvent, url: string) => { | ||
onClickOutside?.(() => | ||
persistLinksField({ | ||
primaryLinkUrl: url, | ||
primaryLinkLabel: '', | ||
secondaryLinks: [], | ||
}), | ||
); | ||
}; | ||
const links = useMemo( | ||
() => | ||
[ | ||
fieldValue.primaryLinkUrl | ||
? { | ||
url: fieldValue.primaryLinkUrl, | ||
label: fieldValue.primaryLinkLabel, | ||
} | ||
: null, | ||
...(fieldValue.secondaryLinks ?? []), | ||
].filter(isDefined), | ||
[ | ||
fieldValue.primaryLinkLabel, | ||
fieldValue.primaryLinkUrl, | ||
fieldValue.secondaryLinks, | ||
], | ||
); | ||
|
||
const handleTab = (url: string) => { | ||
onTab?.(() => | ||
persistLinksField({ | ||
primaryLinkUrl: url, | ||
primaryLinkLabel: '', | ||
secondaryLinks: [], | ||
}), | ||
); | ||
}; | ||
useListenClickOutside({ | ||
refs: [containerRef], | ||
callback: (event) => { | ||
event.stopImmediatePropagation(); | ||
|
||
const isTargetInput = | ||
event.target instanceof HTMLInputElement && | ||
event.target.tagName === 'INPUT'; | ||
|
||
if (!isTargetInput) { | ||
onCancel?.(); | ||
} | ||
}, | ||
}); | ||
|
||
const handleShiftTab = (url: string) => { | ||
onShiftTab?.(() => | ||
const [isInputDisplayed, setIsInputDisplayed] = useState(false); | ||
const [inputValue, setInputValue] = useState(''); | ||
|
||
useScopedHotkeys(Key.Escape, onCancel ?? (() => {}), hotkeyScope); | ||
|
||
const handleSubmit = () => { | ||
if (!inputValue) return; | ||
|
||
setIsInputDisplayed(false); | ||
setInputValue(''); | ||
|
||
if (!links.length) { | ||
persistLinksField({ | ||
primaryLinkUrl: url, | ||
primaryLinkUrl: inputValue, | ||
primaryLinkLabel: '', | ||
secondaryLinks: [], | ||
}), | ||
); | ||
}; | ||
}); | ||
|
||
return; | ||
} | ||
|
||
const handleChange = (url: string) => { | ||
setDraftValue({ | ||
primaryLinkUrl: url, | ||
primaryLinkLabel: '', | ||
secondaryLinks: [], | ||
persistLinksField({ | ||
...fieldValue, | ||
secondaryLinks: [ | ||
...(fieldValue.secondaryLinks ?? []), | ||
{ label: '', url: inputValue }, | ||
], | ||
}); | ||
}; | ||
|
||
return ( | ||
<FieldInputOverlay> | ||
<TextInput | ||
value={draftValue?.primaryLinkUrl ?? ''} | ||
autoFocus | ||
placeholder="Links" | ||
hotkeyScope={hotkeyScope} | ||
onClickOutside={handleClickOutside} | ||
onEnter={handleEnter} | ||
onEscape={handleEscape} | ||
onTab={handleTab} | ||
onShiftTab={handleShiftTab} | ||
onChange={handleChange} | ||
/> | ||
</FieldInputOverlay> | ||
<StyledDropdownMenu ref={containerRef} width={200}> | ||
{!!links.length && ( | ||
<> | ||
<DropdownMenuItemsContainer> | ||
{links.map(({ label, url }, index) => ( | ||
<MenuItem | ||
key={index} | ||
text={<LinkDisplay value={{ label, url }} />} | ||
/> | ||
))} | ||
</DropdownMenuItemsContainer> | ||
<DropdownMenuSeparator /> | ||
</> | ||
)} | ||
{isInputDisplayed ? ( | ||
<DropdownMenuInput | ||
autoFocus | ||
placeholder="URL" | ||
value={inputValue} | ||
hotkeyScope={hotkeyScope} | ||
onChange={(event) => setInputValue(event.target.value)} | ||
onEnter={handleSubmit} | ||
rightComponent={ | ||
<LightIconButton Icon={IconPlus} onClick={handleSubmit} /> | ||
} | ||
/> | ||
) : ( | ||
<DropdownMenuItemsContainer> | ||
<MenuItem | ||
onClick={() => setIsInputDisplayed(true)} | ||
LeftIcon={IconPlus} | ||
text="Add link" | ||
/> | ||
</DropdownMenuItemsContainer> | ||
)} | ||
</StyledDropdownMenu> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
packages/twenty-front/src/modules/ui/field/display/components/LinksDisplay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import { FieldLinksValue } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { LinkDisplay } from '@/ui/field/display/components/LinkDisplay'; | ||
import { getUrlHostName } from '~/utils/url/getUrlHostName'; | ||
|
||
type LinksDisplayProps = { | ||
value?: FieldLinksValue; | ||
}; | ||
|
||
export const LinksDisplay = ({ value }: LinksDisplayProps) => { | ||
const url = value?.primaryLinkUrl || ''; | ||
const label = value?.primaryLinkLabel || getUrlHostName(url); | ||
const label = value?.primaryLinkLabel || ''; | ||
|
||
return <LinkDisplay value={{ url, label }} />; | ||
}; |
Oops, something went wrong.