Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit e446e1e

Browse files
James CollierMaybeJustJames
authored andcommitted
Atom style callbacks pass indexes rather than an extension of Atom
The [TTFD](https://ttfd.vib.be) application uses this to display selected atoms from a tracer metabolite. When the molecule itself is used to store "selected" state the same instance must be used for display. This prevents generalised React components where different Molecule instances (of the same molecule) are used for display.
1 parent d2649bb commit e446e1e

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

src/index.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export type Atom = {
4242
x: number;
4343
y: number;
4444
element: string;
45-
selected?: boolean;
4645
};
4746

4847
export type MoleculeData = {
@@ -392,11 +391,8 @@ type MoleculeProps = {
392391
labelTranslateY?: number;
393392
atomClicked?: (_index: number) => void;
394393
atomLabel?: (_atom: Atom, _index: number) => string;
395-
atomStyle?: (_element: string, _selected: boolean) => React.CSSProperties;
396-
atomLabelStyle?: (
397-
_element: string,
398-
_selected: boolean,
399-
) => React.CSSProperties;
394+
atomStyle?: (_element: string, _index: number) => React.CSSProperties;
395+
atomLabelStyle?: (_element: string, _index: number) => React.CSSProperties;
400396
};
401397

402398
export const Molecule: React.FC<MoleculeProps> = (props: MoleculeProps) => {
@@ -416,20 +412,20 @@ export const Molecule: React.FC<MoleculeProps> = (props: MoleculeProps) => {
416412
-(Math.min(min_y, 0) - ATOM_RADIUS) + (props.translateY || 0);
417413

418414
const defaultAtomLabel = (atom: Atom, _index: number): React.ReactElement => (
419-
<>{atom.element}</>
415+
<>{atom.element.toString()}</>
420416
);
421417

422418
const defaultAtomStyle = (
423419
element: string,
424-
_selected: boolean,
420+
_index: number,
425421
): React.CSSProperties => ({
426422
fill: element === "C" ? "rgba(1, 1, 1, 0)" : "white",
427423
stroke: element === "C" ? "rgba(1, 1, 1, 0)" : "white",
428424
});
429425

430426
const defaultAtomLabelStyle = (
431427
element: string,
432-
_selected: boolean,
428+
_index: number,
433429
): React.CSSProperties => ({
434430
fill: element === "C" ? "rgba(0,0,0,0)" : "black",
435431
});
@@ -473,14 +469,14 @@ export const Molecule: React.FC<MoleculeProps> = (props: MoleculeProps) => {
473469
cy={atom.y}
474470
r={ATOM_RADIUS}
475471
strokeWidth="0.02"
476-
style={atomStyle(atom.element, !!atom.selected)}
472+
style={atomStyle(atom.element, i)}
477473
/>
478474
<text
479475
key={`label-${i}`}
480476
x={atom.x + (props.labelTranslateX || 0)}
481477
y={atom.y + (props.labelTranslateY || 0.25)}
482478
textAnchor="middle"
483-
style={atomLabelStyle(atom.element, !!atom.selected)}
479+
style={atomLabelStyle(atom.element, i)}
484480
>
485481
{atomLabel(atom, i)}
486482
</text>

0 commit comments

Comments
 (0)