import { Classes } from '@blueprintjs/core'; import { useField } from '@tanstack/react-form'; import { useCallback, useMemo } from 'react'; import { FaPlus, FaRegTrashAlt } from 'react-icons/fa'; import { Button, createTableColumnHelper, withForm } from 'react-science/ui'; import type { z } from 'zod'; import { CellActions, CellActionsButton, CellInput, CellNumericInput, TableSettings, } from '../ui/table.tsx'; import { TableSection } from '../ui/table_section.tsx'; import type { nucleiValidation } from '../validation/nuclei_tab_validation.ts'; import { defaultGeneralSettingsFormValues } from '../validation.js'; type NucleiFormElement = z.input[number]; function emptyNucleiFormElement(): NucleiFormElement { return { nucleus: '', ppmFormat: '0.00', hzFormat: '0.00', uuid: crypto.randomUUID(), }; } export const NucleiTab = withForm({ defaultValues: defaultGeneralSettingsFormValues, render: function Render({ form }) { const nucleiField = useField({ form, name: 'nuclei', mode: 'array' }); const { Field } = form; const { insertValue, removeValue, pushValue } = nucleiField; const insertNucleus = useCallback( (index: number) => { insertValue(index, emptyNucleiFormElement()); }, [insertValue], ); const pushNucleus = useCallback(() => { pushValue(emptyNucleiFormElement()); }, [pushValue]); const deleteNucleus = useCallback( (index: number) => { removeValue(index); }, [removeValue], ); const COLUMNS = useMemo(() => { const columnHelper = createTableColumnHelper(); return [ columnHelper.accessor('nucleus', { header: 'Nucleus', cell: ({ row: { index } }) => ( {(subField) => ( )} ), }), columnHelper.accessor('ppmFormat', { header: 'δ (ppm)', cell: ({ row: { index } }) => ( {(subField) => ( )} ), }), columnHelper.accessor('hzFormat', { header: 'Coupling (Hz)', cell: ({ row: { index } }) => ( {(subField) => ( )} ), }), columnHelper.display({ id: 'axisFrom', header: 'Axis from', cell: ({ row: { index } }) => ( {(subField) => ( subField.handleChange(event.currentTarget.value) } onBlur={subField.handleBlur} fill /> )} ), }), columnHelper.display({ id: 'axisTo', header: 'Axis to', cell: ({ row: { index } }) => ( {(subField) => ( subField.handleChange(event.currentTarget.value) } onBlur={subField.handleBlur} fill /> )} ), }), columnHelper.display({ id: 'actions', header: '', meta: { thStyle: { width: '60px', }, }, cell: ({ row: { index } }) => ( insertNucleus(index + 1)} > deleteNucleus(index)} > ), }), ]; }, [Field, deleteNucleus, insertNucleus]); return ( Add nuclei preferences } > ); }, }); function getRowId(row: NucleiFormElement) { return row.uuid; }