|
| 1 | +import { Classes, NumericInput } from '@blueprintjs/core'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | +import { useStore } from '@tanstack/react-form'; |
| 4 | +import { useCallback, useMemo } from 'react'; |
| 5 | +import { FaPlus, FaRegTrashAlt } from 'react-icons/fa'; |
| 6 | +import { Button, withForm } from 'react-science/ui'; |
| 7 | +import type { CellProps } from 'react-table'; |
| 8 | + |
| 9 | +import { GroupPane } from '../../../../elements/GroupPane.tsx'; |
| 10 | +import { Input2 } from '../../../../elements/Input2.tsx'; |
| 11 | +import type { Column } from '../../../../elements/ReactTable/ReactTable.tsx'; |
| 12 | +import ReactTable from '../../../../elements/ReactTable/ReactTable.tsx'; |
| 13 | +import { Section } from '../../general_settings.tsx'; |
| 14 | +import { defaultGeneralSettingsFormValues } from '../validation.ts'; |
| 15 | + |
| 16 | +interface NucleiFormElement { |
| 17 | + nucleus: string; |
| 18 | + ppmFormat: string; |
| 19 | + hzFormat: string; |
| 20 | +} |
| 21 | + |
| 22 | +const emptyNucleiFormElement: NucleiFormElement = { |
| 23 | + nucleus: '', |
| 24 | + ppmFormat: '0.00', |
| 25 | + hzFormat: '0.00', |
| 26 | +}; |
| 27 | + |
| 28 | +export const NucleiTab = withForm({ |
| 29 | + defaultValues: defaultGeneralSettingsFormValues, |
| 30 | + render: function Render({ form }) { |
| 31 | + const fields = useStore(form.store, (state) => state.values.nuclei); |
| 32 | + |
| 33 | + const handleAdd = useCallback( |
| 34 | + (data: readonly NucleiFormElement[], index: number) => { |
| 35 | + let columns: NucleiFormElement[] = []; |
| 36 | + |
| 37 | + if (data) { |
| 38 | + columns = [ |
| 39 | + ...data.slice(0, index), |
| 40 | + emptyNucleiFormElement, |
| 41 | + ...data.slice(index), |
| 42 | + ]; |
| 43 | + } else { |
| 44 | + columns.push(emptyNucleiFormElement); |
| 45 | + } |
| 46 | + |
| 47 | + form.setFieldValue('nuclei', columns); |
| 48 | + }, |
| 49 | + [form], |
| 50 | + ); |
| 51 | + |
| 52 | + const handleDelete = useCallback( |
| 53 | + (data: readonly NucleiFormElement[], formElement: NucleiFormElement) => { |
| 54 | + const fields = data.filter((element) => { |
| 55 | + return element.nucleus !== formElement.nucleus; |
| 56 | + }); |
| 57 | + |
| 58 | + form.setFieldValue('nuclei', fields); |
| 59 | + }, |
| 60 | + [form], |
| 61 | + ); |
| 62 | + |
| 63 | + const COLUMNS = useMemo<Array<Column<NucleiFormElement>>>( |
| 64 | + () => [ |
| 65 | + { |
| 66 | + Header: 'Nucleus', |
| 67 | + style: { padding: 0 }, |
| 68 | + Cell: ({ row: { index } }: CellProps<NucleiFormElement>) => { |
| 69 | + return ( |
| 70 | + <form.Field key={index} name={`nuclei[${index}].nucleus`}> |
| 71 | + {(subField) => ( |
| 72 | + <Input2 |
| 73 | + style={{ |
| 74 | + backgroundColor: 'transparent', |
| 75 | + boxShadow: 'none', |
| 76 | + }} |
| 77 | + value={subField.state.value} |
| 78 | + onChange={subField.handleChange} |
| 79 | + /> |
| 80 | + )} |
| 81 | + </form.Field> |
| 82 | + ); |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + Header: 'δ (ppm)', |
| 87 | + style: { padding: 0 }, |
| 88 | + Cell: ({ row: { index } }: CellProps<NucleiFormElement>) => { |
| 89 | + return ( |
| 90 | + <form.Field key={index} name={`nuclei[${index}].ppmFormat`}> |
| 91 | + {(subField) => ( |
| 92 | + <Input2 |
| 93 | + style={{ |
| 94 | + backgroundColor: 'transparent', |
| 95 | + boxShadow: 'none', |
| 96 | + }} |
| 97 | + value={subField.state.value} |
| 98 | + onChange={subField.handleChange} |
| 99 | + /> |
| 100 | + )} |
| 101 | + </form.Field> |
| 102 | + ); |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + Header: 'Coupling (Hz)', |
| 107 | + style: { padding: 0 }, |
| 108 | + Cell: ({ row: { index } }: CellProps<NucleiFormElement>) => { |
| 109 | + return ( |
| 110 | + <form.Field key={index} name={`nuclei[${index}].hzFormat`}> |
| 111 | + {(subField) => ( |
| 112 | + <Input2 |
| 113 | + style={{ |
| 114 | + backgroundColor: 'transparent', |
| 115 | + boxShadow: 'none', |
| 116 | + }} |
| 117 | + value={subField.state.value} |
| 118 | + onChange={subField.handleChange} |
| 119 | + /> |
| 120 | + )} |
| 121 | + </form.Field> |
| 122 | + ); |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + Header: 'Axis from', |
| 127 | + style: { padding: 0 }, |
| 128 | + Cell: ({ row: { index } }: CellProps<NucleiFormElement>) => { |
| 129 | + return ( |
| 130 | + <form.Field key={index} name={`nuclei[${index}].axisFrom`}> |
| 131 | + {(subField) => ( |
| 132 | + <NumericInput |
| 133 | + style={{ |
| 134 | + backgroundColor: 'transparent', |
| 135 | + boxShadow: 'none', |
| 136 | + }} |
| 137 | + value={Number(subField.state.value) || undefined} |
| 138 | + fill |
| 139 | + onValueChange={(valueAsNumber) => |
| 140 | + subField.handleChange(valueAsNumber) |
| 141 | + } |
| 142 | + /> |
| 143 | + )} |
| 144 | + </form.Field> |
| 145 | + ); |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + Header: 'Axis to', |
| 150 | + style: { padding: 0 }, |
| 151 | + Cell: ({ row: { index } }: CellProps<NucleiFormElement>) => { |
| 152 | + return ( |
| 153 | + <form.Field key={index} name={`nuclei[${index}].axisTo`}> |
| 154 | + {(subField) => ( |
| 155 | + <NumericInput |
| 156 | + style={{ |
| 157 | + backgroundColor: 'transparent', |
| 158 | + boxShadow: 'none', |
| 159 | + }} |
| 160 | + value={Number(subField.state.value) || undefined} |
| 161 | + fill |
| 162 | + onValueChange={(valueAsNumber) => |
| 163 | + subField.handleChange(valueAsNumber) |
| 164 | + } |
| 165 | + /> |
| 166 | + )} |
| 167 | + </form.Field> |
| 168 | + ); |
| 169 | + }, |
| 170 | + }, |
| 171 | + { |
| 172 | + Header: '', |
| 173 | + style: { |
| 174 | + width: 60, |
| 175 | + }, |
| 176 | + id: 'op-buttons', |
| 177 | + Cell: ({ |
| 178 | + row: { original, index: rowIndex }, |
| 179 | + data, |
| 180 | + }: CellProps<NucleiFormElement>) => { |
| 181 | + return ( |
| 182 | + <Buttons> |
| 183 | + <Button |
| 184 | + size="small" |
| 185 | + variant="outlined" |
| 186 | + intent="success" |
| 187 | + onClick={() => handleAdd(data, rowIndex + 1)} |
| 188 | + > |
| 189 | + <FaPlus className={Classes.ICON} /> |
| 190 | + </Button> |
| 191 | + <Button |
| 192 | + size="small" |
| 193 | + variant="outlined" |
| 194 | + intent="danger" |
| 195 | + onClick={() => handleDelete(data, original)} |
| 196 | + > |
| 197 | + <FaRegTrashAlt className={Classes.ICON} /> |
| 198 | + </Button> |
| 199 | + </Buttons> |
| 200 | + ); |
| 201 | + }, |
| 202 | + }, |
| 203 | + ], |
| 204 | + [form, handleAdd, handleDelete], |
| 205 | + ); |
| 206 | + |
| 207 | + return ( |
| 208 | + <GroupPane |
| 209 | + text="Number formatting for crosshair and info line and axis domain" |
| 210 | + renderHeader={(text) => { |
| 211 | + return ( |
| 212 | + <FieldsBlockHeader text={text} onAdd={() => handleAdd(fields, 0)} /> |
| 213 | + ); |
| 214 | + }} |
| 215 | + > |
| 216 | + <ReactTable |
| 217 | + style={{ |
| 218 | + 'thead tr th': { zIndex: 1 }, |
| 219 | + td: { padding: 0 }, |
| 220 | + }} |
| 221 | + rowStyle={{ |
| 222 | + hover: { backgroundColor: '#f7f7f7' }, |
| 223 | + active: { backgroundColor: '#f5f5f5' }, |
| 224 | + }} |
| 225 | + data={fields} |
| 226 | + columns={COLUMNS} |
| 227 | + emptyDataRowText="No Nucleus" |
| 228 | + /> |
| 229 | + </GroupPane> |
| 230 | + ); |
| 231 | + }, |
| 232 | +}); |
| 233 | + |
| 234 | +interface FieldsBlockHeaderProps { |
| 235 | + onAdd: () => void; |
| 236 | + text: string; |
| 237 | +} |
| 238 | + |
| 239 | +function FieldsBlockHeader(props: FieldsBlockHeaderProps) { |
| 240 | + const { onAdd, text } = props; |
| 241 | + return ( |
| 242 | + <Section> |
| 243 | + <p style={{ flex: 1 }}>{text}</p> |
| 244 | + |
| 245 | + <Button |
| 246 | + size="small" |
| 247 | + variant="outlined" |
| 248 | + intent="success" |
| 249 | + tooltipProps={{ content: '', disabled: true }} |
| 250 | + onClick={onAdd} |
| 251 | + > |
| 252 | + Add nuclei preferences |
| 253 | + </Button> |
| 254 | + </Section> |
| 255 | + ); |
| 256 | +} |
| 257 | + |
| 258 | +const Buttons = styled.div` |
| 259 | + display: flex; |
| 260 | + justify-content: space-evenly; |
| 261 | +`; |
0 commit comments