-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
860 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useState } from 'react' | ||
import cn from 'classnames' | ||
import styles from './DropdownMenu.module.css' | ||
|
||
const DropdownMenu = ({ label, menuItems }) => { | ||
const [opened, setOpened] = useState(false) | ||
|
||
const toggle = () => { | ||
setOpened(!opened) | ||
} | ||
|
||
const handleClick = (callback) => { | ||
return () => { | ||
setOpened(false) | ||
callback() | ||
} | ||
} | ||
|
||
return ( | ||
<div class={styles.dropdown}> | ||
<button class={cn(styles.dropdownButton, { [styles.opened]: opened })} onClick={toggle}>{label}</button> | ||
<div class={cn(styles.menu, { [styles.opened]: opened })}> | ||
{menuItems.map((item) => ( | ||
<button class={styles.menuItem} onClick={handleClick(item.onClick)}>{item.label}</button> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default DropdownMenu |
139 changes: 139 additions & 0 deletions
139
frontend/src/components/DropdownMenu/DropdownMenu.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* dropdown */ | ||
|
||
.dropdown { | ||
display: inline-block; | ||
|
||
position: relative; | ||
} | ||
|
||
/* dropdownButton */ | ||
|
||
.dropdownButton { | ||
border: none; | ||
border-radius: calc(var(--rem) * 1.5); | ||
|
||
padding-top: calc(var(--rem) * 3); | ||
padding-bottom: calc(var(--rem) * 3); | ||
padding-left: calc(var(--rem) * 6); | ||
padding-right: calc(var(--rem) * 6); | ||
|
||
background-color: var(--color-button-default); | ||
|
||
font-family: var(--title-font); | ||
font-weight: 400; | ||
font-size: calc(var(--rem) * 4); | ||
line-height: calc(var(--rem) * 5.5); | ||
color: var(--color-font-accent); | ||
|
||
cursor: pointer; | ||
} | ||
|
||
.dropdownButton:hover { | ||
background-color: var(--color-button-hover); | ||
} | ||
|
||
.dropdownButton:active { | ||
background-color: var(--color-button-active); | ||
} | ||
|
||
.dropdownButton:focus { | ||
outline: var(--rem) solid var(--color-button-focus); | ||
} | ||
|
||
.dropdownButton::after { | ||
content: ''; | ||
display: inline-block; | ||
|
||
width: 0; | ||
height: 0; | ||
|
||
margin-left: calc(var(--rem) * 2); | ||
|
||
border-left: calc(var(--rem) * 1.5) solid transparent; | ||
border-right: calc(var(--rem) * 1.5) solid transparent; | ||
border-top: calc(var(--rem) * 1.5) solid currentColor; | ||
border-bottom: none; | ||
|
||
vertical-align: middle; | ||
} | ||
|
||
.dropdownButton.opened::after { | ||
content: ''; | ||
display: inline-block; | ||
|
||
width: 0; | ||
height: 0; | ||
|
||
margin-left: calc(var(--rem) * 2); | ||
|
||
border-left: calc(var(--rem) * 1.5) solid transparent; | ||
border-right: calc(var(--rem) * 1.5) solid transparent; | ||
border-top: none; | ||
border-bottom: calc(var(--rem) * 1.5) solid currentColor; | ||
|
||
vertical-align: middle; | ||
} | ||
|
||
/* menu */ | ||
|
||
.menu { | ||
position: absolute; | ||
top: calc(var(--rem) * 14); | ||
z-index: 10; | ||
|
||
display: none; | ||
box-sizing: border-box; | ||
|
||
border-radius: calc(var(--rem) * 1.5); | ||
padding: var(--rem); | ||
min-width: 100%; | ||
|
||
grid-auto-rows: max-content; | ||
gap: var(--rem); | ||
|
||
box-shadow: var(--rem) var(--rem) calc(var(--rem) * 4) var(--color-shadow); | ||
|
||
background-color: var(--color-background); | ||
} | ||
|
||
.menu.opened { | ||
display: grid; | ||
} | ||
|
||
/* menuItem */ | ||
|
||
.menuItem { | ||
border: none; | ||
border-radius: calc(var(--rem) * 1.5); | ||
|
||
padding-top: calc(var(--rem) * 1); | ||
padding-bottom: calc(var(--rem) * 1); | ||
padding-left: calc(var(--rem) * 3); | ||
padding-right: calc(var(--rem) * 3); | ||
|
||
background-color: transparent; | ||
|
||
font-family: var(--title-font); | ||
font-weight: 400; | ||
font-size: calc(var(--rem) * 4); | ||
line-height: calc(var(--rem) * 5.5); | ||
color: var(--color-font-base); | ||
|
||
cursor: pointer; | ||
} | ||
|
||
.menuItem:hover { | ||
background-color: var(--color-button-hover); | ||
color: var(--color-font-accent); | ||
} | ||
|
||
.menuItem:active { | ||
background-color: var(--color-button-active); | ||
color: var(--color-font-accent); | ||
} | ||
|
||
.menuItem:focus { | ||
background-color: var(--color-button-hover); | ||
color: var(--color-font-accent); | ||
outline: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './DropdownMenu' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { useEffect, useState, useMemo } from 'react' | ||
import { useNavigate } from "react-router-dom" | ||
import { Button, DropdownMenu } from '@components' | ||
import { paramsToObject, objectToParams } from './utils' | ||
import styles from './Panel.module.css' | ||
|
||
const Panel = ({ schema: rawSchema, worker, reloadPage }) => { | ||
const [params, setParams] = useState({}) | ||
const [options, setOptions] = useState({}) | ||
const navigate = useNavigate() | ||
|
||
const addField = (prop) => ( | ||
() => { | ||
const searchParams = new URLSearchParams(window.location.search) | ||
const value = searchParams.has(prop) ? searchParams.get(prop) : "" | ||
|
||
console.log(options) | ||
console.log(addedFields) | ||
setParams({ ...params, [prop]: value }) | ||
} | ||
) | ||
|
||
const removeField = (prop) => ( | ||
() => { | ||
const newParams = { ...params } | ||
delete newParams[prop] | ||
setParams(newParams) | ||
} | ||
) | ||
|
||
const onChangeField = (prop) => ( | ||
(e) => { | ||
const newParams = {...params} | ||
newParams[prop] = e.target.value | ||
setParams(newParams) | ||
} | ||
) | ||
|
||
const search = async () => { | ||
const searchParams = objectToParams(params) | ||
|
||
navigate({ | ||
pathname: window.location.pathname, | ||
search: "?" + searchParams.toString() | ||
}) | ||
|
||
await reloadPage() | ||
} | ||
|
||
const schema = useMemo(() => ( | ||
Object.keys(rawSchema).reduce((acc, key) => ( | ||
rawSchema[key]?.hasOwnProperty('parent') // without root of schema | ||
? [...acc, { ...rawSchema[key], name: key }] | ||
: acc | ||
), []) | ||
), [rawSchema]) | ||
|
||
const notAddedFields = useMemo(() => ( | ||
schema.filter((item) => !params?.hasOwnProperty(item.name)) | ||
), [schema, params]) | ||
|
||
const addedFields = useMemo(() => ( | ||
Object.keys(params).reduce((acc, key) => ( | ||
rawSchema[key]?.hasOwnProperty('parent') | ||
? [ ...acc, { key, value: params[key] }] | ||
: acc | ||
), []) | ||
), [params, rawSchema]) | ||
|
||
const menuItems = useMemo(() => ( | ||
notAddedFields.map((field) => ( | ||
{ label: field.name, onClick: addField(field.name) } | ||
)) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
), [notAddedFields]) | ||
|
||
useEffect(() => ( | ||
(async () => { | ||
var _options = {} | ||
for (const param of schema) { | ||
_options[param.name] = await worker.queryOptions(param.name) | ||
} | ||
setOptions(_options) | ||
})() | ||
), [schema]) | ||
|
||
useEffect(() => { | ||
const searchParams = new URLSearchParams(window.location.search) | ||
setParams(paramsToObject(searchParams)) | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<form className={styles.form}> | ||
{addedFields.map(({ key, value }) => ( | ||
<div> | ||
<label>{key}</label> | ||
<span onClick={removeField(key)}> X</span> | ||
<br/> | ||
<input | ||
className={styles.input} | ||
type="text" | ||
list={`${key}_list`} | ||
value={value} | ||
placeholder={key} | ||
onChange={onChangeField(key)} | ||
/> | ||
<datalist id={`${key}_list`}> | ||
{options[key]?.map(option => ( | ||
<option value={option}/> | ||
))} | ||
</datalist> | ||
</div> | ||
))} | ||
</form> | ||
<DropdownMenu | ||
label='' | ||
menuItems={menuItems} | ||
/> | ||
<Button type="button" onClick={search}>Search</Button> | ||
</> | ||
)} | ||
|
||
export default Panel |
Oops, something went wrong.