diff --git a/ui/src/ifql/components/FuncArgsPreview.tsx b/ui/src/ifql/components/FuncArgsPreview.tsx new file mode 100644 index 0000000000..1425c8b064 --- /dev/null +++ b/ui/src/ifql/components/FuncArgsPreview.tsx @@ -0,0 +1,66 @@ +import React, {PureComponent} from 'react' +import {Arg} from 'src/types/ifql' +import uuid from 'uuid' + +interface Props { + args: Arg[] +} + +export default class FuncArgsPreview extends PureComponent { + public render() { + return
{this.summarizeArguments}
+ } + + private get summarizeArguments(): JSX.Element | JSX.Element[] { + const {args} = this.props + + if (!args) { + return + } + + return this.colorizedArguments + } + + private get colorizedArguments(): JSX.Element | JSX.Element[] { + const {args} = this.props + + return args.map((arg, i): JSX.Element => { + if (!arg.value) { + return + } + + const separator = i === 0 ? null : ', ' + + return ( + + {separator} + {arg.key}: {this.colorArgType(`${arg.value}`, arg.type)} + + ) + }) + } + + private colorArgType = (argument: string, type: string): JSX.Element => { + switch (type) { + case 'time': + case 'number': + case 'period': + case 'duration': + case 'array': { + return {argument} + } + case 'bool': { + return {argument} + } + case 'string': { + return "{argument}" + } + case 'invalid': { + return {argument} + } + default: { + return {argument} + } + } + } +} diff --git a/ui/src/ifql/components/FuncNode.tsx b/ui/src/ifql/components/FuncNode.tsx index 36e8b695e7..18341e5153 100644 --- a/ui/src/ifql/components/FuncNode.tsx +++ b/ui/src/ifql/components/FuncNode.tsx @@ -1,6 +1,7 @@ import React, {PureComponent, MouseEvent} from 'react' -import uuid from 'uuid' + import FuncArgs from 'src/ifql/components/FuncArgs' +import FuncArgsPreview from 'src/ifql/components/FuncArgsPreview' import {OnDeleteFuncNode, OnChangeArg, Func} from 'src/types/ifql' import {ErrorHandling} from 'src/shared/decorators/errors' @@ -33,6 +34,7 @@ export default class FuncNode extends PureComponent { public render() { const { func, + func: {args}, bodyID, onChangeArg, declarationID, @@ -47,7 +49,7 @@ export default class FuncNode extends PureComponent { onMouseLeave={this.handleMouseLeave} >
{func.name}
- {this.coloredSyntaxArgs} + {isExpanded && ( { ) } - private get coloredSyntaxArgs(): JSX.Element { - const { - func: {args}, - } = this.props - - if (!args) { - return - } - - const coloredSyntax = args.map((arg, i): JSX.Element => { - if (!arg.value) { - return - } - - const separator = i === 0 ? null : ', ' - - return ( - - {separator} - {arg.key}: {this.colorArgType(`${arg.value}`, arg.type)} - - ) - }) - - return
{coloredSyntax}
- } - - private colorArgType = (argument: string, type: string): JSX.Element => { - switch (type) { - case 'time': - case 'number': - case 'period': - case 'duration': - case 'array': { - return {argument} - } - case 'bool': { - return {argument} - } - case 'string': { - return "{argument}" - } - case 'invalid': { - return {argument} - } - default: { - return {argument} - } - } - } - private handleDelete = (): void => { const {func, bodyID, declarationID} = this.props diff --git a/ui/src/types/ifql.ts b/ui/src/types/ifql.ts index 65bd406f93..e84e4d2c07 100644 --- a/ui/src/types/ifql.ts +++ b/ui/src/types/ifql.ts @@ -51,7 +51,7 @@ export interface Func { type Value = string | boolean -interface Arg { +export interface Arg { key: string value: Value type: string