fix: Team creation dialog is visible and closable (#1758)

* fix: Team creation dialog is visible and closable

* fix according to recs

* fix border-radius issue visible on dark theme

* rename to appropriate naming: hotkeyCloser

* no memoize
This commit is contained in:
Tom Avalexing
2023-10-04 16:10:23 +03:00
committed by GitHub
parent 59a7e7ead3
commit 8f41792918

View File

@ -10,9 +10,10 @@ import {
} from '@/people/components/PeoplePicker'; } from '@/people/components/PeoplePicker';
import { GET_PEOPLE } from '@/people/graphql/queries/getPeople'; import { GET_PEOPLE } from '@/people/graphql/queries/getPeople';
import { LightIconButton } from '@/ui/button/components/LightIconButton'; import { LightIconButton } from '@/ui/button/components/LightIconButton';
import { FieldDoubleText } from '@/ui/field/types/FieldDoubleText';
import { IconPlus } from '@/ui/icon'; import { IconPlus } from '@/ui/icon';
import { DoubleTextInput } from '@/ui/input/components/DoubleTextInput';
import { RelationPickerHotkeyScope } from '@/ui/input/relation-picker/types/RelationPickerHotkeyScope'; import { RelationPickerHotkeyScope } from '@/ui/input/relation-picker/types/RelationPickerHotkeyScope';
import { TextInputSettings } from '@/ui/input/text/components/TextInputSettings';
import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope'; import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';
import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope'; import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope';
import { import {
@ -21,23 +22,31 @@ import {
} from '~/generated/graphql'; } from '~/generated/graphql';
const StyledContainer = styled.div` const StyledContainer = styled.div`
position: relative; position: static;
`; `;
const StyledInputContainer = styled.div` const StyledInputContainer = styled.div`
background-color: ${({ theme }) => theme.background.tertiary}; background-color: transparent;
box-shadow: ${({ theme }) => theme.boxShadow.strong}; box-shadow: ${({ theme }) => theme.boxShadow.strong};
display: flex; display: flex;
gap: ${({ theme }) => theme.spacing(0.5)}; gap: ${({ theme }) => theme.spacing(0.5)};
width: ${({ theme }) => theme.spacing(62.5)}; width: ${({ theme }) => theme.spacing(62.5)};
& * input, & input,
* div { div {
background-color: ${({ theme }) => theme.background.primary}; background-color: ${({ theme }) => theme.background.primary};
width: 100%;
}
div {
border-radius: ${({ theme }) => theme.spacing(1)};
overflow: hidden;
}
input {
display: flex;
flex-grow: 1;
padding: ${({ theme }) => theme.spacing(2)};
} }
`; `;
const defaultUsername = { firstName: '', lastName: '' };
export const AddPersonToCompany = ({ export const AddPersonToCompany = ({
companyId, companyId,
peopleIds, peopleIds,
@ -47,14 +56,19 @@ export const AddPersonToCompany = ({
}) => { }) => {
const [isDropdownOpen, setIsDropdownOpen] = useState(false); const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const [isCreationDropdownOpen, setIsCreationDropdownOpen] = useState(false); const [isCreationDropdownOpen, setIsCreationDropdownOpen] = useState(false);
const [username, setUsername] = useState(defaultUsername);
const [updatePerson] = useUpdateOnePersonMutation(); const [updatePerson] = useUpdateOnePersonMutation();
const [insertOnePerson] = useInsertOnePersonMutation(); const [insertOnePerson] = useInsertOnePersonMutation();
const { refs, floatingStyles } = useFloating({ const { refs, floatingStyles } = useFloating({
open: isDropdownOpen,
placement: 'right-start', placement: 'right-start',
middleware: [flip(), offset({ mainAxis: -20, crossAxis: 25 })], middleware: [flip(), offset({ mainAxis: 30, crossAxis: 0 })],
}); });
const handleEscape = () => {
if (isCreationDropdownOpen) setIsCreationDropdownOpen(false);
if (isDropdownOpen) setIsDropdownOpen(false);
};
const { const {
setHotkeyScopeAndMemorizePreviousScope, setHotkeyScopeAndMemorizePreviousScope,
goBackToPreviousHotkeyScope, goBackToPreviousHotkeyScope,
@ -94,58 +108,49 @@ export const AddPersonToCompany = ({
} }
}; };
const handleUsernameChange = const handleInputKeyDown = async ({
(type: 'firstName' | 'lastName') => firstValue,
(name: string): void => secondValue,
setUsername((prevUserName) => ({ ...prevUserName, [type]: name })); }: FieldDoubleText) => {
if (!firstValue && !secondValue) return;
const handleInputKeyDown = async (
e: React.KeyboardEvent<HTMLInputElement>,
) => {
if (e.key !== 'Enter' || (!username.firstName && !username.lastName))
return;
const newPersonId = v4(); const newPersonId = v4();
await insertOnePerson({ await insertOnePerson({
variables: { variables: {
data: { data: {
company: { connect: { id: companyId } }, company: { connect: { id: companyId } },
id: newPersonId, id: newPersonId,
...username, firstName: firstValue,
lastName: secondValue,
}, },
}, },
refetchQueries: [getOperationName(GET_PEOPLE) ?? ''], refetchQueries: [getOperationName(GET_PEOPLE) ?? ''],
}); });
setIsCreationDropdownOpen(false); setIsCreationDropdownOpen(false);
setUsername(defaultUsername);
}; };
return ( return (
<RecoilScope> <RecoilScope>
<StyledContainer> <StyledContainer ref={refs.setReference}>
<div ref={refs.setReference}> <LightIconButton
<LightIconButton Icon={IconPlus}
Icon={IconPlus} onClick={handleOpenPicker}
onClick={handleOpenPicker} size="small"
size="small" accent="tertiary"
accent="tertiary" />
/>
</div>
{isDropdownOpen && ( {isDropdownOpen && (
<div ref={refs.setFloating} style={floatingStyles}> <div ref={refs.setFloating} style={floatingStyles}>
{isCreationDropdownOpen ? ( {isCreationDropdownOpen ? (
<StyledInputContainer> <StyledInputContainer>
<TextInputSettings <DoubleTextInput
onKeyDown={handleInputKeyDown} firstValue=""
value={username.firstName} secondValue=""
onChange={handleUsernameChange('firstName')} firstValuePlaceholder="First Name"
placeholder="First Name" secondValuePlaceholder="Last Name"
/> onClickOutside={handleEscape}
<TextInputSettings onEnter={handleInputKeyDown}
onKeyDown={handleInputKeyDown} onEscape={handleEscape}
value={username.lastName} hotkeyScope={RelationPickerHotkeyScope.RelationPicker}
onChange={handleUsernameChange('lastName')}
placeholder="Last Name"
/> />
</StyledInputContainer> </StyledInputContainer>
) : ( ) : (