Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add virtual scrolling to dropdown and polar chart #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"react-intl-universal": "^2.11.1",
"react-router": "^6.26.1",
"react-router-dom": "^6.26.1",
"react-window": "^1.8.10",
"zustand": "^4.5.5"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React from 'react';
import { VariableSizeList, ListChildComponentProps } from 'react-window';
import Typography from '@mui/material/Typography';

const LISTBOX_PADDING = 8; // px

const OuterElementContext = React.createContext({});

const OuterElementType = React.forwardRef<HTMLDivElement>((props, ref) => {
const outerProps = React.useContext(OuterElementContext);
return <div ref={ref} {...props} {...outerProps} />;
});

// Adapter for react-window
const ListboxComponent = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLElement>>(
function ListboxComponent(props, ref) {
const { children, ...other } = props;
const itemData: React.ReactElement<unknown>[] = [];
(children as React.ReactElement<unknown>[]).forEach(
(
item: React.ReactElement<unknown> & {
children?: React.ReactElement<unknown>[];
},
) => {
itemData.push(item);
},
);

const itemCount = itemData.length;
const childSize = 48; // 每个子元素的高度

const useResetCache = (data: any) => {
const ref = React.useRef<VariableSizeList>(null);
React.useEffect(() => {
if (ref.current != null) {
ref.current.resetAfterIndex(0, true);
}
}, [data]);
return ref;
};

const renderRow = (props: ListChildComponentProps) => {
const { data, index, style } = props;
const dataSet = data[index];
const inlineStyle = {
...style,
top: (style.top as number) + LISTBOX_PADDING,
};

const [rowProps, option, state, renderOption] = dataSet;

const { key, ...optionProps } = rowProps;
return (
<Typography key={key} component="li" {...optionProps} noWrap style={inlineStyle}>
{renderOption(rowProps, option, state)}
</Typography>
);
};

const getHeight = () => {
if (itemCount > 8) {
return 8 * childSize;
}
return itemCount * childSize;
};

const gridRef = useResetCache(itemCount);

return (
<div ref={ref}>
<OuterElementContext.Provider value={other}>
<VariableSizeList
itemData={itemData}
height={getHeight() + 2 * LISTBOX_PADDING}
width="100%"
ref={gridRef}
outerElementType={OuterElementType}
innerElementType="ul"
itemSize={() => childSize}
overscanCount={5}
itemCount={itemCount}
>
{renderRow}
</VariableSizeList>
</OuterElementContext.Provider>
</div>
);
},
);

export default ListboxComponent;
53 changes: 38 additions & 15 deletions apps/web/src/plugin/components/multi-entity-select/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { Autocomplete, TextField, Checkbox, Tooltip, Chip } from '@mui/material';
import {
Autocomplete,
TextField,
Checkbox,
Tooltip,
Chip,
styled,
Popper,
autocompleteClasses,
} from '@mui/material';

import type { AutocompleteProps } from '@mui/material';

import { useI18n } from '@milesight/shared/src/hooks';
import { useEntitySelectOptions } from '../../hooks';

import './style.less';
import ListboxComponent from './ListboxComponent';

type EntitySelectProps = AutocompleteProps<EntityOptionType, true, false, undefined> &
EntitySelectCommonProps<EntityOptionType[]>;
Expand Down Expand Up @@ -47,24 +57,32 @@ const MultiEntitySelect = (props: EntitySelectProps) => {
});

const renderOption: EntitySelectProps['renderOption'] = (optionProps, option, { selected }) => {
const { label, value, description } = option || {};
const { label, description } = option || {};

return (
<li {...(optionProps || {})} key={value}>
<div className="ms-multi-entity-select">
<Checkbox style={{ marginRight: 8 }} checked={selected} />
<div className="ms-entity-select-item">
<div className="ms-entity-select-item__label" title={label}>
{label}
</div>
<div className="ms-entity-select-item__description" title={description}>
{description}
</div>
<div className="ms-multi-entity-select">
<Checkbox style={{ marginRight: 8 }} checked={selected} />
<div className="ms-entity-select-item">
<div className="ms-entity-select-item__label" title={label}>
{label}
</div>
<div className="ms-entity-select-item__description" title={description}>
{description}
</div>
</div>
</li>
</div>
);
};

const StyledPopper = styled(Popper)({
[`& .${autocompleteClasses.listbox}`]: {
boxSizing: 'border-box',
'& ul': {
padding: 0,
margin: 0,
},
},
});
return (
<Autocomplete
{...restProps}
Expand Down Expand Up @@ -99,7 +117,9 @@ const MultiEntitySelect = (props: EntitySelectProps) => {
}
/>
)}
renderOption={renderOption}
renderOption={(props, option, state) =>
[{ ...props, key: option.value }, option, state, renderOption] as React.ReactNode
}
getOptionLabel={option => option?.label || ''}
loading={loading}
filterOptions={options => options}
Expand All @@ -108,7 +128,6 @@ const MultiEntitySelect = (props: EntitySelectProps) => {
getEntityOptions();
return;
}

getEntityOptions(keyword);
}}
isOptionEqualToValue={(option, currentVal) => option.value === currentVal.value}
Expand All @@ -122,6 +141,10 @@ const MultiEntitySelect = (props: EntitySelectProps) => {
);
})
}
slots={{
popper: StyledPopper,
listbox: ListboxComponent,
}}
/>
);
};
Expand Down
86 changes: 86 additions & 0 deletions apps/web/src/plugin/plugins/polar-area-chart/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"type": "polarAreaChart",
"name": "polar Area Chart",
"class": "data_chart",
"icon": "./icon.png",
"defaultRow": 6,
"defaultCol": 8,
"minRow": 4,
"minCol": 4,
"configProps": [
{
"style": "width: 100%",
"components": [
{
"type": "multiEntitySelect",
"title": "Entity",
"key": "entityList",
"style": "width: 100%",
"getDataUrl": "",
"valueType": "array",
"componentProps": {
"size": "small",
"entityType": ["PROPERTY", "EVENT"],
"entityValueTypes": ["LONG", "DOUBLE"]
},
"rules": {
"required": true
}
}
]
},
{
"components": [
{
"type": "input",
"title": "Title",
"key": "title",
"defaultValue": "Title",
"componentProps": {
"size": "small",
"inputProps": {
"maxLength": 35
}
}
}
]
},
{
"style": "width: 100%;",
"theme": {
"default": {
"style": "font-size: 18px;"
}
},
"components": [
{
"type": "ChartTimeSelect",
"title": "Time",
"key": "time",
"style": "width: 100%;",
"componentProps": {
"size": "small"
},
"defaultValue": 86400000
}
]
},
{
"style": "width: 100%;",
"components": [
{
"type": "chartMetricsSelect",
"title": "metrics",
"key": "metrics",
"style": "width: 100%;",
"componentProps": {
"size": "small"
},
"defaultValue": "LAST",
"filters": ["SUM", "COUNT"]
}
]
}
],
"view": []
}
29 changes: 29 additions & 0 deletions apps/web/src/plugin/plugins/polar-area-chart/configure/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { forwardRef } from 'react';
import { RenderConfig } from '../../../render';

interface ConfigPluginProps {
config: CustomComponentProps;
value: any;
onOk: (data: any) => void;
onChange: (data: any) => void;
}

const Plugin = forwardRef((props: ConfigPluginProps, ref: any) => {
const { onOk, onChange, value, config } = props;

const handleSubmit = (data: any) => {
onOk(data);
};

return (
<RenderConfig
config={config}
onOk={handleSubmit}
ref={ref}
onChange={onChange}
value={value}
/>
);
});

export default Plugin;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions apps/web/src/plugin/plugins/polar-area-chart/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { EntityAPISchema } from '@/services/http';

export interface ViewConfigProps {
title: string;
entityList: EntityOptionType[];
time: number;
metrics: DataAggregateType;
}

export type ConfigureType = any;

export interface AggregateHistoryList {
entity: EntityOptionType;
data: EntityAPISchema['getAggregateHistory']['response'];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useSource } from './useSource';
Loading