|
| 1 | +// Libraries |
| 2 | +import React, {FC, createRef} from 'react' |
| 3 | + |
| 4 | +// Component |
| 5 | +import { |
| 6 | + Popover, |
| 7 | + PopoverPosition, |
| 8 | + PopoverInteraction, |
| 9 | + Appearance, |
| 10 | + Button, |
| 11 | + ComponentSize, |
| 12 | + ComponentColor, |
| 13 | + DapperScrollbars, |
| 14 | +} from '@influxdata/clockface' |
| 15 | + |
| 16 | +// Types |
| 17 | +import {FluxToolbarFunction} from 'src/types/shared' |
| 18 | + |
| 19 | +interface Props { |
| 20 | + func: FluxToolbarFunction |
| 21 | + onClickFunction: (func: FluxToolbarFunction) => void |
| 22 | + testID: string |
| 23 | +} |
| 24 | + |
| 25 | +interface TooltipProps { |
| 26 | + func: FluxToolbarFunction |
| 27 | +} |
| 28 | + |
| 29 | +const defaultProps = { |
| 30 | + testID: 'flux-function', |
| 31 | +} |
| 32 | + |
| 33 | +const FunctionTooltipContents: FC<TooltipProps> = ({func}) => { |
| 34 | + let argComponent = <div className="flux-function-docs--arguments">None</div> |
| 35 | + |
| 36 | + if (func.args.length > 0) { |
| 37 | + argComponent = ( |
| 38 | + <> |
| 39 | + {func.args.map(a => ( |
| 40 | + <div className="flux-function-docs--arguments" key={a.name}> |
| 41 | + <span>{a.name}:</span> |
| 42 | + <span>{a.type}</span> |
| 43 | + <div>{a.desc}</div> |
| 44 | + </div> |
| 45 | + ))} |
| 46 | + </> |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <div className="flux-function-docs" data-testid={`flux-docs--${func.name}`}> |
| 52 | + <DapperScrollbars autoHide={false}> |
| 53 | + <div className="flux-toolbar--popover"> |
| 54 | + <article className="flux-functions-toolbar--description"> |
| 55 | + <div className="flux-function-docs--heading">Description</div> |
| 56 | + <span>{func.desc}</span> |
| 57 | + </article> |
| 58 | + <article> |
| 59 | + <div className="flux-function-docs--heading">Arguments</div> |
| 60 | + <div className="flux-function-docs--snippet">{argComponent}</div> |
| 61 | + </article> |
| 62 | + <p className="tooltip--link"> |
| 63 | + Still have questions? Check out the{' '} |
| 64 | + <a target="_blank" rel="noreferrer" href={func.link}> |
| 65 | + Flux Docs |
| 66 | + </a> |
| 67 | + . |
| 68 | + </p> |
| 69 | + </div> |
| 70 | + </DapperScrollbars> |
| 71 | + </div> |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +const ToolbarFunction: FC<Props> = ({func, onClickFunction, testID}) => { |
| 76 | + const functionRef = createRef<HTMLDListElement>() |
| 77 | + const handleClickFunction = () => { |
| 78 | + onClickFunction(func) |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <Popover |
| 84 | + appearance={Appearance.Outline} |
| 85 | + enableDefaultStyles={false} |
| 86 | + position={PopoverPosition.ToTheLeft} |
| 87 | + triggerRef={functionRef} |
| 88 | + showEvent={PopoverInteraction.Hover} |
| 89 | + hideEvent={PopoverInteraction.Hover} |
| 90 | + distanceFromTrigger={8} |
| 91 | + testID="toolbar-popover" |
| 92 | + contents={() => <FunctionTooltipContents func={func} />} |
| 93 | + /> |
| 94 | + <dd |
| 95 | + ref={functionRef} |
| 96 | + data-testid={`flux--${testID}`} |
| 97 | + className="flux-toolbar--list-item flux-toolbar--function" |
| 98 | + > |
| 99 | + <code>{func.name}</code> |
| 100 | + <Button |
| 101 | + testID={`flux--${testID}--inject`} |
| 102 | + text="Inject" |
| 103 | + onClick={handleClickFunction} |
| 104 | + size={ComponentSize.ExtraSmall} |
| 105 | + className="flux-toolbar--injector" |
| 106 | + color={ComponentColor.Primary} |
| 107 | + /> |
| 108 | + </dd> |
| 109 | + </> |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +ToolbarFunction.defaultProps = defaultProps |
| 114 | + |
| 115 | +export default ToolbarFunction |
0 commit comments