-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented EntityFieldGroup (WIP)
- Loading branch information
1 parent
5721d60
commit 9656b02
Showing
19 changed files
with
515 additions
and
63 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
146 changes: 146 additions & 0 deletions
146
.../fields/EntityFieldGroup/components/EntityFieldGroupDocument/EntityFieldGroupDocument.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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { ctw } from '@/common'; | ||
import { Button } from '@/components/atoms'; | ||
import { Input } from '@/components/atoms/Input'; | ||
import { createTestId } from '@/components/organisms/Renderer/utils/create-test-id'; | ||
import get from 'lodash/get'; | ||
import { Upload, XCircle } from 'lucide-react'; | ||
import { useCallback, useMemo, useRef } from 'react'; | ||
import { useField } from '../../../../hooks/external'; | ||
import { useMountEvent } from '../../../../hooks/internal/useMountEvent'; | ||
import { useUnmountEvent } from '../../../../hooks/internal/useUnmountEvent'; | ||
import { FieldDescription } from '../../../../layouts/FieldDescription'; | ||
import { FieldErrors } from '../../../../layouts/FieldErrors'; | ||
import { FieldLayout } from '../../../../layouts/FieldLayout'; | ||
import { FieldPriorityReason } from '../../../../layouts/FieldPriorityReason'; | ||
import { IFormElement, TDynamicFormElement } from '../../../../types'; | ||
import { IDocumentFieldParams } from '../../../DocumentField'; | ||
import { createOrUpdateFileIdOrFileInDocuments } from '../../../DocumentField/hooks/useDocumentUpload/helpers/create-or-update-fileid-or-file-in-documents'; | ||
import { getFileOrFileIdFromDocumentsList } from '../../../DocumentField/hooks/useDocumentUpload/helpers/get-file-or-fileid-from-documents-list'; | ||
import { removeDocumentFromListByTemplateId } from '../../../DocumentField/hooks/useDocumentUpload/helpers/remove-document-from-list-by-template-id'; | ||
import { useStack } from '../../../FieldList'; | ||
|
||
export const EntityFieldGroupDocument: TDynamicFormElement< | ||
'documentfield', | ||
IDocumentFieldParams | ||
> = ({ element }) => { | ||
useMountEvent(element); | ||
useUnmountEvent(element); | ||
|
||
const { params } = element; | ||
const { placeholder = 'Choose file', acceptFileFormats = undefined } = params || {}; | ||
|
||
const { stack } = useStack(); | ||
const { | ||
value: documentsList, | ||
disabled, | ||
onChange, | ||
onBlur, | ||
onFocus, | ||
} = useField<Array<IDocumentFieldParams['template']> | undefined>(element, stack); | ||
const value = useMemo( | ||
() => | ||
getFileOrFileIdFromDocumentsList( | ||
documentsList, | ||
element as IFormElement<'documentfield', IDocumentFieldParams>, | ||
), | ||
[documentsList, element], | ||
); | ||
|
||
const file = useMemo(() => { | ||
if (value instanceof File) { | ||
return value; | ||
} | ||
|
||
if (typeof value === 'string') { | ||
return new File([], value); | ||
} | ||
|
||
return undefined; | ||
}, [value]); | ||
|
||
const inputRef = useRef<HTMLInputElement>(null); | ||
const focusInputOnContainerClick = useCallback(() => { | ||
inputRef.current?.click(); | ||
}, [inputRef]); | ||
|
||
const clearFileAndInput = useCallback(() => { | ||
if (!element.params?.template?.id) { | ||
console.warn('Template id is migging in element', element); | ||
|
||
return; | ||
} | ||
|
||
const updatedDocuments = removeDocumentFromListByTemplateId( | ||
documentsList, | ||
element.params?.template?.id as string, | ||
); | ||
|
||
onChange(updatedDocuments); | ||
|
||
if (inputRef.current) { | ||
inputRef.current.value = ''; | ||
} | ||
}, [documentsList, element, onChange]); | ||
|
||
const handleChange = useCallback( | ||
(e: React.ChangeEvent<HTMLInputElement>) => { | ||
const documents = get(documentsList || [], element.valueDestination); | ||
const updatedDocuments = createOrUpdateFileIdOrFileInDocuments( | ||
documents, | ||
element, | ||
e.target.files?.[0] as File, | ||
); | ||
onChange(updatedDocuments); | ||
}, | ||
[onChange], | ||
); | ||
|
||
return ( | ||
<FieldLayout element={element}> | ||
<div | ||
className={ctw( | ||
'relative flex h-[56px] flex-row items-center gap-3 rounded-[16px] border bg-white px-4', | ||
{ 'pointer-events-none opacity-50': disabled }, | ||
)} | ||
onClick={focusInputOnContainerClick} | ||
data-testid={createTestId(element, stack)} | ||
> | ||
<div className="flex gap-3 text-[#007AFF]"> | ||
<Upload /> | ||
<span className="select-none whitespace-nowrap text-base font-bold">{placeholder}</span> | ||
</div> | ||
<span className="truncate text-sm">{file ? file.name : 'No File Choosen'}</span> | ||
{file && ( | ||
<Button | ||
variant="ghost" | ||
size="icon" | ||
className="h-[28px] w-[28px] rounded-full" | ||
onClick={e => { | ||
e.stopPropagation(); | ||
clearFileAndInput(); | ||
}} | ||
> | ||
<div className="rounded-full bg-white"> | ||
<XCircle /> | ||
</div> | ||
</Button> | ||
)} | ||
<Input | ||
data-testid={`${createTestId(element, stack)}-hidden-input`} | ||
type="file" | ||
placeholder={placeholder} | ||
accept={acceptFileFormats} | ||
disabled={disabled} | ||
onChange={handleChange} | ||
onBlur={onBlur} | ||
onFocus={onFocus} | ||
ref={inputRef} | ||
className="hidden" | ||
/> | ||
</div> | ||
<FieldDescription element={element} /> | ||
<FieldPriorityReason element={element} /> | ||
<FieldErrors element={element} /> | ||
</FieldLayout> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
...sms/Form/DynamicForm/fields/EntityFieldGroup/components/EntityFieldGroupDocument/index.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './EntityFieldGroupDocument'; |
Oops, something went wrong.