Commit 0c437340 by Vladislav Lagunov

[WIP] Динамическая загрузка автодополнений

parent 26433fa3
...@@ -5,7 +5,7 @@ import { AuthCtx as Ctx } from '~/context'; ...@@ -5,7 +5,7 @@ import { AuthCtx as Ctx } from '~/context';
import { FieldProps } from '~/create-form'; import { FieldProps } from '~/create-form';
import TextField from '~/fields/TextField'; import TextField from '~/fields/TextField';
import { Props as TextFieldProps } from '~/fields/TextField'; import { Props as TextFieldProps } from '~/fields/TextField';
import { StandardProps } from '@material-ui/core'; import { StandardProps, ListItem } from '@material-ui/core';
import Icon from './Icon'; import Icon from './Icon';
import withStyles, { StyleRules } from '@material-ui/core/styles/withStyles'; import withStyles, { StyleRules } from '@material-ui/core/styles/withStyles';
import { Theme } from '@material-ui/core/styles/createMuiTheme'; import { Theme } from '@material-ui/core/styles/createMuiTheme';
...@@ -185,18 +185,30 @@ export default class AutoComplete<A=string> extends React.Component<Props<A> & { ...@@ -185,18 +185,30 @@ export default class AutoComplete<A=string> extends React.Component<Props<A> & {
endAdornment: this.endAdornment(!!nonNull, !!disabled, __), endAdornment: this.endAdornment(!!nonNull, !!disabled, __),
}; };
}; };
handleSeeMore = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
// const { query: { offset, limit } } = this.state;
this.dispatch({ tag: 'More' });
};
render() { render() {
const { className, fullWidth, children, ctx, source, debounce, renderItem, printItem, openOnFocus, openOnClick, suggestionProps, keepOpenAfterSelect, textFieldProps, classes, anchorEl, observable, ...rest } = this.props; const { className, fullWidth, children, ctx, source, debounce, renderItem, printItem, openOnFocus, openOnClick, suggestionProps, keepOpenAfterSelect, textFieldProps, classes, anchorEl, observable, ...rest } = this.props;
const { suggestions, open } = this.state; const { suggestions, open, pending } = this.state;
const rootClass = classNames(classes!.root, className, { const rootClass = classNames(classes!.root, className, {
[classes!.fullWidth!]: fullWidth [classes!.fullWidth!]: fullWidth
}); });
const seeMoreLink = <ListItem>
<a href={"#"} onMouseDown={this.handleSeeMore}>{('See more')}</a>
</ListItem>;
return <div {...rest} className={rootClass}> return <div {...rest} className={rootClass}>
{!observable && (children ? React.cloneElement(children, this.childrenProps()) : <TextField {...this.childrenProps() as any} {...textFieldProps}/>)} {!observable && (children ? React.cloneElement(children, this.childrenProps()) : <TextField {...this.childrenProps() as any} {...textFieldProps}/>)}
<Suggestions <Suggestions
{...suggestionProps} {...suggestionProps}
pending={pending}
ctx={ctx!} ctx={ctx!}
anchorEl={anchorEl || this.anchorEl || undefined} anchorEl={anchorEl || this.anchorEl || undefined}
open={open} open={open}
...@@ -204,6 +216,7 @@ export default class AutoComplete<A=string> extends React.Component<Props<A> & { ...@@ -204,6 +216,7 @@ export default class AutoComplete<A=string> extends React.Component<Props<A> & {
renderSuggestion={renderItem || printItem} renderSuggestion={renderItem || printItem}
onSelect={this.handleSuggestionSelect} onSelect={this.handleSuggestionSelect}
onVisibilityChange={this.handleVisibilityChnage} onVisibilityChange={this.handleVisibilityChnage}
after={seeMoreLink}
/> />
</div>; </div>;
} }
......
...@@ -68,6 +68,7 @@ export type Action<A> = ...@@ -68,6 +68,7 @@ export type Action<A> =
| { tag: 'Error', error: Err } | { tag: 'Error', error: Err }
| { tag: 'Search', value: string } | { tag: 'Search', value: string }
| { tag: 'Offset', value: number } | { tag: 'Offset', value: number }
| { tag: 'More' }
| { tag: 'Query', query: AutoCompleteQuery } | { tag: 'Query', query: AutoCompleteQuery }
| { tag: 'Query/success', suggestions: A[], total: number, query: AutoCompleteQuery } | { tag: 'Query/success', suggestions: A[], total: number, query: AutoCompleteQuery }
| { tag: 'Focus' } | { tag: 'Focus' }
...@@ -146,7 +147,11 @@ export function update<A>(ctx: Ctx<A>, action: Action<A>, state: State<A>): [Sta ...@@ -146,7 +147,11 @@ export function update<A>(ctx: Ctx<A>, action: Action<A>, state: State<A>): [Sta
return [state, cmd.of<Action<A>>({ tag: 'Query', query: { ...state.query, offset: action.value } })]; return [state, cmd.of<Action<A>>({ tag: 'Query', query: { ...state.query, offset: action.value } })];
} }
case 'Search': { case 'Search': {
return [state, cmd.of<Action<A>>({ tag: 'Query', query: { ...state.query, search: action.value } })]; return [state, cmd.of<Action<A>>({ tag: 'Query', query: { ...defaultQuery, search: action.value } })];
}
case 'More': {
const { query: { limit } } = state;
return [state, cmd.of<Action<A>>({ tag: 'Query', query: { ...state.query, limit: limit + 20 } })];
} }
case 'Focus': { case 'Focus': {
if (ctx.options.openOnFocus && !ctx.options.openOnClick) { if (ctx.options.openOnFocus && !ctx.options.openOnClick) {
......
...@@ -22,6 +22,8 @@ export type Props<A=any> = StandardProps<React.HTMLProps<HTMLDivElement>, string ...@@ -22,6 +22,8 @@ export type Props<A=any> = StandardProps<React.HTMLProps<HTMLDivElement>, string
marginThreshold?: number; marginThreshold?: number;
pending?: boolean; pending?: boolean;
dontMove?: boolean; dontMove?: boolean;
before?: React.ReactNode;
after?: React.ReactNode;
// Callbacks // Callbacks
onSelect?(a: A): void; onSelect?(a: A): void;
...@@ -179,12 +181,12 @@ class Suggestions<A> extends React.Component<Props<A>, State> { ...@@ -179,12 +181,12 @@ class Suggestions<A> extends React.Component<Props<A>, State> {
} }
render() { render() {
const { __, classes, open, pending, suggestions } = this.props; const { __, classes, open, pending, suggestions, before, after } = this.props;
const PaperProps = { ref: this.handlePaperRef, className: classes.paper }; const PaperProps = { ref: this.handlePaperRef, className: classes.paper };
return <Modal className={classes.modal} open={!!open} onClose={this.handleClose} disableAutoFocus disableEnforceFocus disableRestoreFocus hideBackdrop> return <Modal className={classes.modal} open={!!open} onClose={this.handleClose} disableAutoFocus disableEnforceFocus disableRestoreFocus hideBackdrop>
<Paper {...PaperProps}> <Paper {...PaperProps}>
{!!suggestions.length && <MenuList className={classes.menu}>{this.renderSuggestions(suggestions)}</MenuList>} {!!suggestions.length && <MenuList className={classes.menu}>{before}{this.renderSuggestions(suggestions)}{after}</MenuList>}
{!suggestions.length && <div className={classes.empty}><span>{__('Nothing found…')}</span></div>} {!suggestions.length && <div className={classes.empty}><span>{__('Nothing found…')}</span></div>}
<PendingOverlay pending={pending || false}/> <PendingOverlay pending={pending || false}/>
</Paper> </Paper>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment