parent
4f3cd30e77
commit
c197e23c04
5 changed files with 150 additions and 2 deletions
@ -0,0 +1,122 @@ |
|||||||
|
import React, { useState } from 'react'; |
||||||
|
import { IconButton, Menu, MenuItem, TextField, Box } from '@material-ui/core'; |
||||||
|
import SearchIcon from '@material-ui/icons/Search'; |
||||||
|
import NestedMenuItem from "material-ui-nested-menu-item"; |
||||||
|
import { QueryElem, QueryLeafBy, QueryLeafOp } from '../../lib/Query'; |
||||||
|
import Autocomplete from '@material-ui/lab/Autocomplete' |
||||||
|
|
||||||
|
export function QBQueryButtonBase(props: any) { |
||||||
|
return <IconButton {...props}> |
||||||
|
<SearchIcon style={{ fontSize: 80 }} /> |
||||||
|
</IconButton> |
||||||
|
} |
||||||
|
|
||||||
|
export interface FreeSoloInputProps { |
||||||
|
label: string, |
||||||
|
options: string[], |
||||||
|
onSubmit: (s: string, exactMatch: boolean) => void, |
||||||
|
} |
||||||
|
|
||||||
|
export function QBMenuFreeSoloInput(props: FreeSoloInputProps) { |
||||||
|
const [value, setValue] = useState<string>(''); |
||||||
|
|
||||||
|
const onInputChange = (event: any, _val: any, reason: any) => { |
||||||
|
if (reason === 'reset') { |
||||||
|
// User selected a preset option.
|
||||||
|
props.onSubmit(_val, true); |
||||||
|
} else { |
||||||
|
setValue(_val); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return <Autocomplete |
||||||
|
style={{ width: 300 }} |
||||||
|
options={props.options} |
||||||
|
freeSolo={true} |
||||||
|
onInputChange={onInputChange} |
||||||
|
onKeyDown={(e: any) => { |
||||||
|
if (e.key === 'Enter') { |
||||||
|
// User submitted free-form value.
|
||||||
|
props.onSubmit(value, props.options.includes(value)); |
||||||
|
} |
||||||
|
}} |
||||||
|
renderInput={(params: any) => <TextField |
||||||
|
{...params} |
||||||
|
label={props.label} |
||||||
|
/>} |
||||||
|
/> |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
export interface MenuProps { |
||||||
|
anchorEl: null | HTMLElement, |
||||||
|
onClose: () => void, |
||||||
|
onCreateQuery: (q: QueryElem) => void, |
||||||
|
} |
||||||
|
|
||||||
|
export function QBQueryButtonMenu(props: MenuProps) { |
||||||
|
let anchorEl = props.anchorEl; |
||||||
|
let onClose = props.onClose; |
||||||
|
|
||||||
|
const artists = [ |
||||||
|
'Queens', |
||||||
|
'Muse', |
||||||
|
'Triggerfinger' |
||||||
|
]; |
||||||
|
|
||||||
|
return <Menu |
||||||
|
id="simple-menu" |
||||||
|
anchorEl={anchorEl} |
||||||
|
keepMounted |
||||||
|
open={Boolean(anchorEl)} |
||||||
|
onClose={onClose} |
||||||
|
> |
||||||
|
<NestedMenuItem |
||||||
|
label="Artist" |
||||||
|
parentMenuOpen={Boolean(anchorEl)} |
||||||
|
> |
||||||
|
<QBMenuFreeSoloInput |
||||||
|
options={artists} |
||||||
|
label="Artist" |
||||||
|
onSubmit={(s: string, exact: boolean) => { |
||||||
|
onClose(); |
||||||
|
props.onCreateQuery({ |
||||||
|
a: QueryLeafBy.ArtistName, |
||||||
|
op: exact ? QueryLeafOp.Equals : QueryLeafOp.Like, |
||||||
|
b: s |
||||||
|
}); |
||||||
|
}} |
||||||
|
/> |
||||||
|
</NestedMenuItem> |
||||||
|
</Menu > |
||||||
|
} |
||||||
|
|
||||||
|
export interface IProps { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
export default function QBQueryButton(props: IProps) { |
||||||
|
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null); |
||||||
|
const onOpen = (e: React.MouseEvent<HTMLButtonElement>) => { |
||||||
|
setAnchorEl(e.currentTarget); |
||||||
|
} |
||||||
|
const onClose = () => { |
||||||
|
setAnchorEl(null); |
||||||
|
} |
||||||
|
const onCreateQuery = (q: QueryElem) => { |
||||||
|
console.log("created query:", q); |
||||||
|
} |
||||||
|
|
||||||
|
return <> |
||||||
|
<QBQueryButtonBase |
||||||
|
aria-controls="simple-menu" |
||||||
|
aria-haspopup="true" |
||||||
|
onClick={onOpen} |
||||||
|
/> |
||||||
|
<QBQueryButtonMenu |
||||||
|
anchorEl={anchorEl} |
||||||
|
onClose={onClose} |
||||||
|
onCreateQuery={onCreateQuery} |
||||||
|
/> |
||||||
|
</>; |
||||||
|
} |
@ -0,0 +1,20 @@ |
|||||||
|
import React from 'react'; |
||||||
|
import { Box } from '@material-ui/core'; |
||||||
|
import QBQueryButton from './QBQueryButton'; |
||||||
|
import { QBQueryElem } from './QBQueryElem'; |
||||||
|
import { QueryElem } from '../../lib/Query'; |
||||||
|
|
||||||
|
export interface IProps { |
||||||
|
query: QueryElem |
||||||
|
} |
||||||
|
|
||||||
|
export default function QueryBuilder(props: IProps) { |
||||||
|
return <Box display="flex" alignItems="center"> |
||||||
|
<Box m={2}> |
||||||
|
<QBQueryButton/> |
||||||
|
</Box> |
||||||
|
<Box m={2}> |
||||||
|
<QBQueryElem elem={props.query}/> |
||||||
|
</Box> |
||||||
|
</Box> |
||||||
|
} |
Loading…
Reference in new issue