import { Classes } from '@blueprintjs/core'; import type { NucleiPreferences } from '@zakodium/nmrium-core'; import { useCallback, useMemo } from 'react'; import { useFormContext } from 'react-hook-form'; import { FaPlus, FaRegTrashAlt } from 'react-icons/fa'; import { Button } from 'react-science/ui'; import type { CellProps } from 'react-table'; import { GroupPane } from '../../../elements/GroupPane.js'; import { Input2Controller } from '../../../elements/Input2Controller.js'; import { NumberInput2Controller } from '../../../elements/NumberInput2Controller.js'; import type { Column } from '../../../elements/ReactTable/ReactTable.js'; import ReactTable from '../../../elements/ReactTable/ReactTable.js'; import type { WorkspaceWithSource } from '../../../reducer/preferences/preferencesReducer.js'; import { Section } from '../general_settings.js'; function getKeyPath( index: number, key: T, ): `nuclei.${number}.${T}` { return `nuclei.${index}.${key}`; } export default function NucleiTabContent() { const { control, setValue, watch } = useFormContext(); const fields: NucleiPreferences[] = watch('nuclei') || []; const addHandler = useCallback( (data: readonly any[], index: number) => { let columns: NucleiPreferences[] = []; const emptyField: NucleiPreferences = { nucleus: '', ppmFormat: '0.00', hzFormat: '0.00', }; if (data && Array.isArray(data)) { columns = [...data.slice(0, index), emptyField, ...data.slice(index)]; } else { columns.push(emptyField); } setValue('nuclei', columns); }, [setValue], ); const deleteHandler = useCallback( (data: any, index: number) => { const _fields = data.filter( (_: any, columnIndex: any) => columnIndex !== index, ); setValue('nuclei', _fields); }, [setValue], ); const COLUMNS = useMemo>>( () => [ { Header: '#', style: { width: '25px', textAlign: 'center' }, accessor: (_, index) => index + 1, }, { Header: 'Nucleus', style: { padding: 0 }, Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'nucleus'); return ( ); }, }, { Header: 'δ (ppm)', style: { padding: 0 }, Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'ppmFormat'); return ( ); }, }, { Header: 'Coupling (Hz)', style: { padding: 0 }, Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'hzFormat'); return ( ); }, }, { Header: 'Axis from', style: { padding: 0 }, Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'axisFrom'); return ( ); }, }, { Header: 'Axis to', style: { padding: 0 }, Cell: ({ row }: CellProps) => { const name = getKeyPath(row.index, 'axisTo'); return ( ); }, }, { Header: '', style: { width: '60px' }, id: 'op-buttons', Cell: ({ data, row }: CellProps) => { return (
); }, }, ], [addHandler, control, deleteHandler], ); return (
{ return ( addHandler(fields, 0)} /> ); }} >
); } interface FieldsBlockHeaderProps { onAdd: () => void; text: string; } function FieldsBlockHeader(props: FieldsBlockHeaderProps) { const { onAdd, text } = props; return (

{text}

); }