Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/adding on select to trendlines #703

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/interactive/src/TrendLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class TrendLine extends React.Component<TrendLineProps, TrendLineState> {
onDragComplete={this.handleDragLineComplete}
edgeInteractiveCursor="react-financial-charts-move-cursor"
lineInteractiveCursor="react-financial-charts-move-cursor"
onSelect={this.handleSelect}
/>
);
})}
Expand Down Expand Up @@ -280,4 +281,20 @@ export class TrendLine extends React.Component<TrendLineProps, TrendLineState> {
},
});
};

private readonly handleSelect = (e: React.MouseEvent, index: number | undefined, moreProps: any) => {
const { trends } = this.props;
const newTrends = trends.map((d, dIdx) => ({ ...d, selected: dIdx === index }));
this.setState(
{
trends: newTrends,
},
() => {
const { onSelect } = this.props;
if (onSelect !== undefined) {
onSelect(e, newTrends, moreProps);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this should return newTrends, if you looks at the original onSelect function definition, the param is called interactives... I believe it is better to return just the selected trends and maybe include their indexes as thats what you're likely to be interested in if you require a function like onSelect, also this way you do not need to loop through all the trends again to determine what was selected.

}
},
);
};
}
15 changes: 10 additions & 5 deletions packages/interactive/src/wrapper/EachTrendLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface EachTrendLineProps {
readonly onEdge1Drag: any; // func
readonly onEdge2Drag: any; // func
readonly onDragComplete?: (e: React.MouseEvent, moreProps: any) => void;
readonly onSelect: (e: React.MouseEvent, interactives: any[], moreProps: any) => void;
readonly onSelect: (e: React.MouseEvent, index: number | undefined, moreProps: any) => void;
readonly r: number;
readonly strokeOpacity: number;
readonly defaultClassName?: string;
Expand Down Expand Up @@ -219,16 +219,20 @@ export class EachTrendLine extends React.Component<EachTrendLineProps, EachTrend
onDragComplete(e, moreProps);
};

private readonly handleEdge2DragStart = () => {
private readonly handleEdge2DragStart = (e: React.MouseEvent, moreProps: any) => {
const { index, onSelect } = this.props;
this.setState({
anchor: "edge1",
});
onSelect(e, index, moreProps);
};

private readonly handleEdge1DragStart = () => {
private readonly handleEdge1DragStart = (e: React.MouseEvent, moreProps: any) => {
const { index, onSelect } = this.props;
this.setState({
anchor: "edge2",
});
onSelect(e, index, moreProps);
};

private readonly handleLineDrag = (e: React.MouseEvent, moreProps: any) => {
Expand Down Expand Up @@ -265,15 +269,16 @@ export class EachTrendLine extends React.Component<EachTrendLineProps, EachTrend
});
};

private readonly handleLineDragStart = () => {
const { x1Value, y1Value, x2Value, y2Value } = this.props;
private readonly handleLineDragStart = (e: React.MouseEvent, moreProps: any) => {
const { x1Value, y1Value, x2Value, y2Value, onSelect, index } = this.props;

this.dragStart = {
x1Value,
y1Value,
x2Value,
y2Value,
};
onSelect(e, index, moreProps);
};
}

Expand Down