Skip to content

Commit 729107a

Browse files
committed
Set state for options in Display. Fixed state/rendering error in SearchTools.
1 parent ab2601f commit 729107a

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ export class CodeSnippetDisplay extends React.Component<
203203
this.handleDragMove = this.handleDragMove.bind(this);
204204
this._evtMouseUp = this._evtMouseUp.bind(this);
205205
this.handleRenameSnippet = this.handleRenameSnippet.bind(this);
206+
this.setSearchOptions = this.setSearchOptions.bind(this);
206207
}
207208

208209
// Handle code snippet insert into a notebook or document
@@ -1620,6 +1621,12 @@ export class CodeSnippetDisplay extends React.Component<
16201621
return body;
16211622
}
16221623

1624+
setSearchOptions(selectedOptions: string[]): void {
1625+
this.setState({
1626+
searchOptions: selectedOptions
1627+
});
1628+
}
1629+
16231630
render(): React.ReactElement {
16241631
return (
16251632
<div>

src/CodeSnippetSearchTools.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface ISearchSnippetState {
2424

2525
interface ISearchSnippetProps {
2626
selectedSearchOptions: string[];
27+
setSearchOptions: (options: string[]) => void;
2728
}
2829

2930
class OptionsHandler extends Widget {
@@ -54,26 +55,35 @@ export class SearchMultiOption extends React.Component<
5455
}
5556

5657
// duplicate code between this and parent clickhandler, potentially combine
57-
// STATUS: state updates are a bit weird, when unchecked, adding to state. When checked removing.
58+
// STATUS: state updates are a bit weird, when unchecked, adding to state. When checked removing. Delay in update.
5859
handleClick(selectedOption: string, selected: boolean): void {
5960
if (selected) {
6061
// select option
61-
this.setState(state => ({
62-
currSelected: [...state.currSelected, selectedOption]
63-
}));
62+
this.setState(
63+
state => ({
64+
currSelected: [...state.currSelected, selectedOption]
65+
}),
66+
() => {
67+
//callback
68+
this.props.handleOptionClick(this.state.currSelected);
69+
}
70+
);
6471
} else {
6572
// unselect option
6673
const array = [...this.state.currSelected];
6774
const index = array.indexOf(selectedOption);
6875
if (index !== -1) {
6976
array.splice(index, 1);
70-
this.setState({
71-
currSelected: array
72-
});
77+
this.setState(
78+
{
79+
currSelected: array
80+
},
81+
() => {
82+
this.props.handleOptionClick(this.state.currSelected);
83+
}
84+
);
7385
}
7486
}
75-
this.props.handleOptionClick(this.state.currSelected);
76-
console.log(this.state);
7787
}
7888

7989
render(): JSX.Element {
@@ -150,6 +160,7 @@ export class SearchTools extends React.Component<
150160
this.setState({
151161
currSelected: selectedOptions
152162
});
163+
this.props.setSearchOptions(selectedOptions);
153164
}
154165

155166
handleClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {

0 commit comments

Comments
 (0)