Skip to content

Commit 11564ce

Browse files
committed
use input outside list
1 parent 001771f commit 11564ce

File tree

3 files changed

+131
-175
lines changed

3 files changed

+131
-175
lines changed
Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback } from 'react';
1+
import React, { Component } from 'react';
22
import { css, fontFamilies, TextInput } from '@mongodb-js/compass-components';
33

44
const passwordPrompt = css({
@@ -12,51 +12,45 @@ const passwordPropmtInputStyles = css({
1212
display: 'inline-block',
1313
});
1414

15-
type PasswordPromptProps = {
16-
onChange: (value: string) => void;
17-
onFinish: (value: string) => void;
15+
interface PasswordPromptProps {
16+
onFinish: (result: string) => void;
1817
onCancel: () => void;
1918
prompt: string;
20-
password: string;
21-
};
19+
}
2220

23-
export const PasswordPrompt = React.forwardRef<
24-
HTMLInputElement,
25-
PasswordPromptProps
26-
>(function PasswordPrompt(
27-
{ prompt, password, onChange, onFinish, onCancel },
28-
ref
29-
) {
30-
const onKeyDown = useCallback(
31-
(ev: React.KeyboardEvent<HTMLInputElement>) => {
32-
switch (ev.key) {
33-
case 'Enter':
34-
onFinish((ev.target as HTMLInputElement).value);
35-
break;
36-
case 'Esc':
37-
case 'Escape':
38-
onCancel();
39-
break;
40-
default:
41-
break;
42-
}
43-
},
44-
[onCancel, onFinish]
45-
);
46-
return (
47-
<label id="password-prompt-label" className={passwordPrompt}>
48-
{prompt}:&nbsp;
49-
<TextInput
50-
ref={ref}
51-
value={password}
52-
data-testid="password-prompt"
53-
aria-labelledby="password-prompt-label"
54-
type="password"
55-
onChange={(evt) => onChange(evt.currentTarget.value)}
56-
onKeyDown={onKeyDown}
57-
className={passwordPropmtInputStyles}
58-
sizeVariant="xsmall"
59-
></TextInput>
60-
</label>
61-
);
62-
});
21+
export class PasswordPrompt extends Component<PasswordPromptProps> {
22+
constructor(props: PasswordPromptProps) {
23+
super(props);
24+
}
25+
26+
onKeyDown = (ev: React.KeyboardEvent<HTMLInputElement>): void => {
27+
switch (ev.key) {
28+
case 'Enter':
29+
this.props.onFinish((ev.target as HTMLInputElement).value);
30+
break;
31+
case 'Esc':
32+
case 'Escape':
33+
this.props.onCancel();
34+
break;
35+
default:
36+
break;
37+
}
38+
};
39+
40+
render(): JSX.Element {
41+
return (
42+
<label id="password-prompt-label" className={passwordPrompt}>
43+
{this.props.prompt}:&nbsp;
44+
<TextInput
45+
data-testid="password-prompt"
46+
aria-labelledby="password-prompt-label"
47+
type="password"
48+
onKeyDown={this.onKeyDown}
49+
className={passwordPropmtInputStyles}
50+
sizeVariant="xsmall"
51+
autoFocus
52+
></TextInput>
53+
</label>
54+
);
55+
}
56+
}
Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useMemo, useRef } from 'react';
1+
import React, { useEffect, useRef } from 'react';
22
import {
33
VirtualList,
44
type VirtualListRef,
@@ -8,62 +8,34 @@ export type { ShellOutputEntry } from './shell-output-line';
88

99
type ShellIOListProps = {
1010
output: ShellOutputEntry[];
11-
renderInputPrompt: () => JSX.Element;
12-
setScrollRef: (ref: HTMLDivElement) => void;
13-
__TEST_OVERSCAN_COUNT?: number;
14-
__TEST_LIST_HEIGHT?: number;
11+
setInnerContainerRef: (ref: HTMLDivElement) => void;
1512
};
1613

1714
export const ShellOutput = ({
1815
output,
19-
renderInputPrompt,
20-
setScrollRef,
21-
__TEST_OVERSCAN_COUNT,
22-
__TEST_LIST_HEIGHT,
16+
setInnerContainerRef,
2317
}: ShellIOListProps) => {
2418
const listRef: VirtualListRef = useRef();
2519

2620
useEffect(() => {
27-
if (!listRef.current) {
28-
return;
29-
}
30-
(window as any).listRef = listRef.current;
31-
listRef.current.resetAfterIndex(0);
32-
const timeout = setTimeout(() => {
33-
// After output changes, scroll to the end (which is
34-
// prompt input)
35-
listRef.current.scrollToItem(output.length, 'end');
36-
}, 100);
37-
return () => clearTimeout(timeout);
38-
}, [output, listRef]);
39-
40-
// Adding prompt to the input list so that its also rendered
41-
// by the virtual list
42-
const items = useMemo(() => [...output, { type: 'inputPrompt' }], [output]);
21+
listRef.current?.resetAfterIndex(0);
22+
listRef.current?.scrollToItem(output.length - 1, 'end');
23+
}, [output]);
4324

4425
return (
4526
<VirtualList
4627
dataTestId="shell-output-virtual-list"
47-
items={items}
48-
overScanCount={__TEST_OVERSCAN_COUNT ?? 1}
28+
items={output}
29+
overScanCount={5}
4930
listRef={listRef}
50-
scrollableContainerRef={setScrollRef}
51-
renderItem={(item, ref) => {
52-
if (item.type === 'inputPrompt') {
53-
return (
54-
<div ref={ref} data-testid="shell-input-prompt">
55-
{renderInputPrompt()}
56-
</div>
57-
);
58-
}
59-
return (
60-
<div ref={ref} data-testid="shell-output-line">
61-
<ShellOutputLine entry={item as ShellOutputEntry} />
62-
</div>
63-
);
64-
}}
31+
// TODO: Depends on adding this in compass components
32+
{...{ setInnerContainerRef }}
33+
renderItem={(item, ref) => (
34+
<div ref={ref} data-testid="shell-output-line">
35+
<ShellOutputLine entry={item} />
36+
</div>
37+
)}
6538
estimateItemInitialHeight={() => 24}
66-
__TEST_LIST_HEIGHT={__TEST_LIST_HEIGHT}
6739
/>
6840
);
6941
};

0 commit comments

Comments
 (0)