import { Classes } from '@blueprintjs/core'; import styled from '@emotion/styled'; import { useField } from '@tanstack/react-form'; import { useSelector } from '@tanstack/react-store'; import { useCallback, useMemo, useState } from 'react'; import { FaRegTrashAlt } from 'react-icons/fa'; import { Button, FieldGroupSVGTextStyleFields, TableDragRowHandler, createTableColumnHelper, withForm, } from 'react-science/ui'; import type { z } from 'zod'; import { useChartData } from '../../../../context/ChartContext.js'; import { getSpectraObjectPaths } from '../../../../utility/getSpectraObjectPaths.js'; import { CellActions, CellActionsButton } from '../ui/cell_actions.tsx'; import { CellCheckbox } from '../ui/cell_checkbox.tsx'; import { CellInput } from '../ui/cell_input.tsx'; import { TableSettings } from '../ui/table.js'; import { TableSection } from '../ui/table_section.js'; import type { spectraLabelFieldTabValidationWithUUID } from '../validation/spectra_label_tab_validation.ts'; import { defaultGeneralSettingsFormValues } from '../validation.js'; export const SpectraLabelTab = withForm({ defaultValues: defaultGeneralSettingsFormValues, render: ({ form }) => { const { Section, AppField } = form; return ( <>
{({ Checkbox }) => }
); }, }); type Field = z.input; function getEmptyField(): Field { return { format: '', jpath: '', visible: true, uuid: crypto.randomUUID(), }; } const TableFields = withForm({ defaultValues: defaultGeneralSettingsFormValues, render: function Fields({ form }) { const { Field } = form; const { removeValue, setValue, pushValue, name, store } = useField({ form, name: 'spectraLabel.fields', mode: 'array', }); const [autoFocus, setAutoFocus] = useState(''); function onAddField() { const value = getEmptyField(); pushValue(value, { dontRunListeners: true }); setAutoFocus(value.uuid); } const { data } = useChartData(); const { datalist } = useMemo(() => getSpectraObjectPaths(data), [data]); const onDeleteAt = useCallback( (index: number) => { removeValue(index); }, [removeValue], ); const columns = useMemo(() => { const columnHelper = createTableColumnHelper(); return [ columnHelper.display({ id: 'dnd', header: '', meta: { tdStyle: { textAlign: 'center' }, }, cell: () => , }), columnHelper.accessor('jpath', { header: 'Field', cell: ({ row: { index, original } }) => ( {(field) => ( setAutoFocus('')} /> )} ), }), columnHelper.accessor('format', { header: 'Format', cell: ({ row: { index } }) => ( {(field) => } ), }), columnHelper.accessor('visible', { header: 'Visible', meta: { tdStyle: { textAlign: 'center' }, }, cell: ({ row: { index } }) => ( {(field) => } ), }), columnHelper.display({ id: 'actions', header: '', meta: { thStyle: { width: '60px', }, }, cell: ({ row: { index } }) => { return ( onDeleteAt(index)} > ); }, }), ]; }, [Field, autoFocus, datalist, name, onDeleteAt]); const onRowOrderChanged = useCallback( (value: Field[]) => { setValue(value); }, [setValue], ); const fieldsData = useSelector(store, (s) => s.value); return ( Add Field } > ); }, }); function getRowId(row: Field) { return row.uuid; } const TableDragRowHandlerStyled = styled(TableDragRowHandler)` margin: 0 2px; `;