-
-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add record use modal ui (#618)
- Loading branch information
Showing
3 changed files
with
182 additions
and
65 deletions.
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
apps/nextjs-app/src/features/app/blocks/view/AddRecordModal.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,160 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { FieldKeyType } from '@teable/core'; | ||
import { createRecords } from '@teable/openapi'; | ||
import { RecordEditor } from '@teable/sdk/components/expand-record/RecordEditor'; | ||
import { useFields, useTableId, useViewId } from '@teable/sdk/hooks'; | ||
import type { IFieldInstance, Record } from '@teable/sdk/model'; | ||
import { createRecordInstance, recordInstanceFieldMap } from '@teable/sdk/model'; | ||
import { Spin } from '@teable/ui-lib/base'; | ||
import { Button, Dialog, DialogContent, DialogTrigger } from '@teable/ui-lib/shadcn'; | ||
import { isEqual } from 'lodash'; | ||
import { useTranslation } from 'next-i18next'; | ||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { useCounter } from 'react-use'; | ||
|
||
interface IAddRecordModalProps { | ||
children?: React.ReactNode; | ||
callback?: (recordId: string) => void; | ||
} | ||
|
||
export const AddRecordModal = (props: IAddRecordModalProps) => { | ||
const { children, callback } = props; | ||
const tableId = useTableId(); | ||
const viewId = useViewId(); | ||
const showFields = useFields(); | ||
const [open, setOpen] = useState(false); | ||
const [version, updateVersion] = useCounter(0); | ||
const { t } = useTranslation('common'); | ||
const allFields = useFields({ withHidden: true, withDenied: true }); | ||
const [record, setRecord] = useState<Record | undefined>(undefined); | ||
|
||
const { mutate: createRecord, isLoading } = useMutation({ | ||
mutationFn: (fields: { [fieldId: string]: unknown }) => | ||
createRecords(tableId!, { | ||
records: [{ fields }], | ||
fieldKeyType: FieldKeyType.Id, | ||
}), | ||
onSuccess: (data) => { | ||
setOpen(false); | ||
callback?.(data.data.records[0].id); | ||
}, | ||
}); | ||
|
||
const newRecord = useCallback( | ||
(version: number = 0) => { | ||
setRecord((preRecord) => { | ||
const record = createRecordInstance({ | ||
id: '', | ||
fields: version > 0 && preRecord?.fields ? preRecord.fields : {}, | ||
}); | ||
record.updateCell = (fieldId: string, newValue: unknown) => { | ||
record.fields[fieldId] = newValue; | ||
updateVersion.inc(); | ||
return Promise.resolve(); | ||
}; | ||
return record; | ||
}); | ||
}, | ||
[updateVersion] | ||
); | ||
|
||
useEffect(() => { | ||
if (!open) { | ||
updateVersion.reset(); | ||
newRecord(); | ||
} | ||
}, [newRecord, open, updateVersion]); | ||
|
||
useEffect(() => { | ||
// init record | ||
newRecord(); | ||
}, [newRecord]); | ||
|
||
useEffect(() => { | ||
if (version > 0) { | ||
newRecord(version); | ||
} | ||
}, [version, newRecord]); | ||
|
||
useEffect(() => { | ||
if (!allFields.length) { | ||
return; | ||
} | ||
setRecord((record) => | ||
record | ||
? recordInstanceFieldMap( | ||
record, | ||
allFields.reduce( | ||
(acc, field) => { | ||
acc[field.id] = field; | ||
return acc; | ||
}, | ||
{} as { [fieldId: string]: IFieldInstance } | ||
) | ||
) | ||
: record | ||
); | ||
}, [allFields, record]); | ||
|
||
const showFieldsId = useMemo(() => new Set(showFields.map((field) => field.id)), [showFields]); | ||
|
||
const fields = useMemo( | ||
() => (viewId ? allFields.filter((field) => showFieldsId.has(field.id)) : []), | ||
[allFields, showFieldsId, viewId] | ||
); | ||
|
||
const hiddenFields = useMemo( | ||
() => (viewId ? allFields.filter((field) => !showFieldsId.has(field.id)) : []), | ||
[allFields, showFieldsId, viewId] | ||
); | ||
|
||
const onChange = (newValue: unknown, fieldId: string) => { | ||
if (isEqual(record?.getCellValue(fieldId), newValue)) { | ||
return; | ||
} | ||
record?.updateCell(fieldId, newValue); | ||
}; | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogTrigger asChild>{children}</DialogTrigger> | ||
<DialogContent | ||
closeable={false} | ||
className="flex h-full max-w-3xl flex-col p-0 pt-6" | ||
style={{ width: 'calc(100% - 40px)', height: 'calc(100% - 100px)' }} | ||
onMouseDown={(e) => e.stopPropagation()} | ||
onInteractOutside={(e) => e.preventDefault()} | ||
onKeyDown={(e) => e.stopPropagation()} | ||
> | ||
<div className="flex-1 overflow-y-auto p-10 pt-4"> | ||
<RecordEditor | ||
record={record} | ||
fields={fields} | ||
hiddenFields={hiddenFields} | ||
onChange={onChange} | ||
/> | ||
</div> | ||
<div className="flex justify-end gap-4 border-t px-10 py-3"> | ||
<Button variant={'outline'} size={'sm'} onClick={() => setOpen(false)}> | ||
{t('actions.cancel')} | ||
</Button> | ||
<Button | ||
className="relative overflow-hidden" | ||
size={'sm'} | ||
disabled={!tableId || isLoading} | ||
onClick={() => { | ||
createRecord(record?.fields ?? {}); | ||
}} | ||
> | ||
{isLoading && ( | ||
<div className="absolute flex size-full items-center justify-center"> | ||
<Spin className="mr-2" /> | ||
</div> | ||
)} | ||
{t('actions.create')} | ||
</Button> | ||
</div> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
}; |
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
50 changes: 12 additions & 38 deletions
50
apps/nextjs-app/src/features/app/blocks/view/tool-bar/GridToolBar.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