Merge pull request #4670 from matrix-org/t3chguy/autocomplete-scrollintoview
Autocomplete: use scrollIntoView for auto-scroll to fix it
This commit is contained in:
commit
7cfeda96d0
7 changed files with 59 additions and 70 deletions
|
@ -90,11 +90,12 @@ export default class CommunityProvider extends AutocompleteProvider {
|
||||||
type: "community",
|
type: "community",
|
||||||
href: makeGroupPermalink(groupId),
|
href: makeGroupPermalink(groupId),
|
||||||
component: (
|
component: (
|
||||||
<PillCompletion initialComponent={
|
<PillCompletion title={name} description={groupId}>
|
||||||
<BaseAvatar name={name || groupId}
|
<BaseAvatar name={name || groupId}
|
||||||
width={24} height={24}
|
width={24}
|
||||||
|
height={24}
|
||||||
url={avatarUrl ? cli.mxcUrlToHttp(avatarUrl, 24, 24) : null} />
|
url={avatarUrl ? cli.mxcUrlToHttp(avatarUrl, 24, 24) : null} />
|
||||||
} title={name} description={groupId} />
|
</PillCompletion>
|
||||||
),
|
),
|
||||||
range,
|
range,
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import React, {forwardRef} from 'react';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
|
||||||
/* These were earlier stateless functional components but had to be converted
|
/* These were earlier stateless functional components but had to be converted
|
||||||
|
@ -30,50 +30,37 @@ interface ITextualCompletionProps {
|
||||||
className?: string;
|
className?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TextualCompletion extends React.PureComponent<ITextualCompletionProps> {
|
export const TextualCompletion = forwardRef<ITextualCompletionProps, any>((props, ref) => {
|
||||||
render() {
|
const {title, subtitle, description, className, ...restProps} = props;
|
||||||
const {
|
return (
|
||||||
title,
|
<div {...restProps}
|
||||||
subtitle,
|
className={classNames('mx_Autocomplete_Completion_block', className)}
|
||||||
description,
|
role="option"
|
||||||
className,
|
ref={ref}
|
||||||
...restProps
|
>
|
||||||
} = this.props;
|
<span className="mx_Autocomplete_Completion_title">{ title }</span>
|
||||||
return (
|
<span className="mx_Autocomplete_Completion_subtitle">{ subtitle }</span>
|
||||||
<div className={classNames('mx_Autocomplete_Completion_block', className)} role="option" {...restProps}>
|
<span className="mx_Autocomplete_Completion_description">{ description }</span>
|
||||||
<span className="mx_Autocomplete_Completion_title">{ title }</span>
|
</div>
|
||||||
<span className="mx_Autocomplete_Completion_subtitle">{ subtitle }</span>
|
);
|
||||||
<span className="mx_Autocomplete_Completion_description">{ description }</span>
|
});
|
||||||
</div>
|
|
||||||
);
|
interface IPillCompletionProps extends ITextualCompletionProps {
|
||||||
}
|
children?: React.ReactNode,
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IPillCompletionProps {
|
export const PillCompletion = forwardRef<IPillCompletionProps, any>((props, ref) => {
|
||||||
title?: string;
|
const {title, subtitle, description, className, children, ...restProps} = props;
|
||||||
subtitle?: string;
|
return (
|
||||||
description?: string;
|
<div {...restProps}
|
||||||
initialComponent?: React.ReactNode,
|
className={classNames('mx_Autocomplete_Completion_pill', className)}
|
||||||
className?: string;
|
role="option"
|
||||||
}
|
ref={ref}
|
||||||
|
>
|
||||||
export class PillCompletion extends React.PureComponent<IPillCompletionProps> {
|
{ children }
|
||||||
render() {
|
<span className="mx_Autocomplete_Completion_title">{ title }</span>
|
||||||
const {
|
<span className="mx_Autocomplete_Completion_subtitle">{ subtitle }</span>
|
||||||
title,
|
<span className="mx_Autocomplete_Completion_description">{ description }</span>
|
||||||
subtitle,
|
</div>
|
||||||
description,
|
);
|
||||||
initialComponent,
|
});
|
||||||
className,
|
|
||||||
...restProps
|
|
||||||
} = this.props;
|
|
||||||
return (
|
|
||||||
<div className={classNames('mx_Autocomplete_Completion_pill', className)} role="option" {...restProps}>
|
|
||||||
{ initialComponent }
|
|
||||||
<span className="mx_Autocomplete_Completion_title">{ title }</span>
|
|
||||||
<span className="mx_Autocomplete_Completion_subtitle">{ subtitle }</span>
|
|
||||||
<span className="mx_Autocomplete_Completion_description">{ description }</span>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -121,9 +121,9 @@ export default class EmojiProvider extends AutocompleteProvider {
|
||||||
return {
|
return {
|
||||||
completion: unicode,
|
completion: unicode,
|
||||||
component: (
|
component: (
|
||||||
<PillCompletion title={shortname} aria-label={unicode} initialComponent={
|
<PillCompletion title={shortname} aria-label={unicode}>
|
||||||
<span style={{maxWidth: '1em'}}>{ unicode }</span>
|
<span style={{maxWidth: '1em'}}>{ unicode }</span>
|
||||||
} />
|
</PillCompletion>
|
||||||
),
|
),
|
||||||
range,
|
range,
|
||||||
};
|
};
|
||||||
|
|
|
@ -48,7 +48,9 @@ export default class NotifProvider extends AutocompleteProvider {
|
||||||
type: "at-room",
|
type: "at-room",
|
||||||
suffix: ' ',
|
suffix: ' ',
|
||||||
component: (
|
component: (
|
||||||
<PillCompletion initialComponent={<RoomAvatar width={24} height={24} room={this.room} />} title="@room" description={_t("Notify the whole room")} />
|
<PillCompletion title="@room" description={_t("Notify the whole room")}>
|
||||||
|
<RoomAvatar width={24} height={24} room={this.room} />
|
||||||
|
</PillCompletion>
|
||||||
),
|
),
|
||||||
range,
|
range,
|
||||||
}];
|
}];
|
||||||
|
|
|
@ -103,7 +103,9 @@ export default class RoomProvider extends AutocompleteProvider {
|
||||||
suffix: ' ',
|
suffix: ' ',
|
||||||
href: makeRoomPermalink(room.displayedAlias),
|
href: makeRoomPermalink(room.displayedAlias),
|
||||||
component: (
|
component: (
|
||||||
<PillCompletion initialComponent={<RoomAvatar width={24} height={24} room={room.room} />} title={room.room.name} description={room.displayedAlias} />
|
<PillCompletion title={room.room.name} description={room.displayedAlias}>
|
||||||
|
<RoomAvatar width={24} height={24} room={room.room} />
|
||||||
|
</PillCompletion>
|
||||||
),
|
),
|
||||||
range,
|
range,
|
||||||
};
|
};
|
||||||
|
|
|
@ -125,10 +125,9 @@ export default class UserProvider extends AutocompleteProvider {
|
||||||
suffix: (selection.beginning && range.start === 0) ? ': ' : ' ',
|
suffix: (selection.beginning && range.start === 0) ? ': ' : ' ',
|
||||||
href: makeUserPermalink(user.userId),
|
href: makeUserPermalink(user.userId),
|
||||||
component: (
|
component: (
|
||||||
<PillCompletion
|
<PillCompletion title={displayName} description={user.userId}>
|
||||||
initialComponent={<MemberAvatar member={user} width={24} height={24} />}
|
<MemberAvatar member={user} width={24} height={24} />
|
||||||
title={displayName}
|
</PillCompletion>
|
||||||
description={user.userId} />
|
|
||||||
),
|
),
|
||||||
range,
|
range,
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,8 +15,7 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import React, {createRef} from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import flatMap from 'lodash/flatMap';
|
import flatMap from 'lodash/flatMap';
|
||||||
import {ICompletion, ISelectionRange, IProviderCompletions} from '../../../autocomplete/Autocompleter';
|
import {ICompletion, ISelectionRange, IProviderCompletions} from '../../../autocomplete/Autocompleter';
|
||||||
|
@ -54,7 +53,7 @@ export default class Autocomplete extends React.PureComponent<IProps, IState> {
|
||||||
autocompleter: Autocompleter;
|
autocompleter: Autocompleter;
|
||||||
queryRequested: string;
|
queryRequested: string;
|
||||||
debounceCompletionsRequest: NodeJS.Timeout;
|
debounceCompletionsRequest: NodeJS.Timeout;
|
||||||
containerRef: React.RefObject<HTMLDivElement>;
|
private containerRef = createRef<HTMLDivElement>();
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
@ -78,8 +77,6 @@ export default class Autocomplete extends React.PureComponent<IProps, IState> {
|
||||||
|
|
||||||
forceComplete: false,
|
forceComplete: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.containerRef = React.createRef();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
@ -256,14 +253,15 @@ export default class Autocomplete extends React.PureComponent<IProps, IState> {
|
||||||
componentDidUpdate(prevProps: IProps) {
|
componentDidUpdate(prevProps: IProps) {
|
||||||
this.applyNewProps(prevProps.query, prevProps.room);
|
this.applyNewProps(prevProps.query, prevProps.room);
|
||||||
// this is the selected completion, so scroll it into view if needed
|
// this is the selected completion, so scroll it into view if needed
|
||||||
const selectedCompletion = this.refs[`completion${this.state.selectionOffset}`];
|
const selectedCompletion = this.refs[`completion${this.state.selectionOffset}`] as HTMLElement;
|
||||||
if (selectedCompletion && this.containerRef.current) {
|
|
||||||
const domNode = ReactDOM.findDOMNode(selectedCompletion);
|
if (selectedCompletion) {
|
||||||
const offsetTop = domNode && (domNode as HTMLElement).offsetTop;
|
selectedCompletion.scrollIntoView({
|
||||||
if (offsetTop > this.containerRef.current.scrollTop + this.containerRef.current.offsetHeight ||
|
behavior: "auto",
|
||||||
offsetTop < this.containerRef.current.scrollTop) {
|
block: "nearest",
|
||||||
this.containerRef.current.scrollTop = offsetTop - this.containerRef.current.offsetTop;
|
});
|
||||||
}
|
} else {
|
||||||
|
this.containerRef.current.scrollTo({ top: 0 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue