Skip to content

Commit 185eddc

Browse files
committed
set a consistent key for output elements rather than using the array index
1 parent 89762a5 commit 185eddc

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

packages/browser-repl/src/components/shell-output-line.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type ShellOutputEntryValue = any;
3535
type Glyph = 'ChevronRight' | 'XWithCircle' | 'ChevronLeft';
3636

3737
export interface ShellOutputEntry {
38+
key?: number | string;
3839
format: 'input' | 'output' | 'error';
3940
type?: string | null;
4041
value: ShellOutputEntryValue;

packages/browser-repl/src/components/shell-output.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export class ShellOutput extends Component<ShellOutputProps> {
1616

1717
renderLine = (entry: ShellOutputEntry, index: number): JSX.Element => {
1818
return (
19-
<ShellOutputLine key={`shell-output-entry-${index}`} entry={entry} />
19+
<ShellOutputLine
20+
key={`shell-output-entry-${entry.key ?? index}`}
21+
entry={entry}
22+
/>
2023
);
2124
};
2225

packages/browser-repl/src/components/shell.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ const capLengthStart = (elements: unknown[], maxLength: number) => {
184184
elements.splice(maxLength);
185185
};
186186

187+
let lastKey = 0;
188+
187189
const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
188190
{
189191
runtime,
@@ -262,6 +264,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
262264
...(outputRef.current ?? []),
263265
...result.map(
264266
(entry): ShellOutputEntry => ({
267+
key: lastKey++,
265268
format: 'output',
266269
type: entry.type,
267270
value: entry.printable,

packages/browser-repl/src/sandbox.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,26 @@ class DemoServiceProvider {
151151

152152
const runtime = new IframeRuntime(new DemoServiceProvider() as any);
153153

154+
const lotsOfLines: ShellOutputEntry[] = [];
155+
for (let i = 0; i < 99; i++) {
156+
lotsOfLines.push({ key: `entry-${i}`, format: 'output', value: { i } });
157+
}
158+
154159
const IframeRuntimeExample: React.FunctionComponent = () => {
155160
const [darkMode, setDarkMode] = useState(true);
156161
const [redactInfo, setRedactInfo] = useState(false);
157-
const [maxOutputLength, setMaxOutputLength] = useState(1000);
158-
const [maxHistoryLength, setMaxHistoryLength] = useState(1000);
162+
const [maxOutputLength, setMaxOutputLength] = useState(100);
163+
const [maxHistoryLength, setMaxHistoryLength] = useState(100);
159164
const [initialEvaluate, setInitialEvaluate] = useState<string[]>([]);
160165

161166
const [initialText, setInitialText] = useState('');
162167
const [output, setOutput] = useState<ShellOutputEntry[]>([
163-
{ format: 'output', value: { foo: 1, bar: true, buz: function () {} } },
168+
...lotsOfLines,
169+
{
170+
key: 'test',
171+
format: 'output',
172+
value: { foo: 1, bar: true, buz: function () {} },
173+
},
164174
]);
165175
const [history, setHistory] = useState([
166176
'show dbs',

0 commit comments

Comments
 (0)