Skip to content

Commit

Permalink
feat: added hard-update option for setConfig and react component (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
zefirka authored Jul 17, 2023
1 parent f02c3e8 commit 104334f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/YagrCore/mixins/dynamic-updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ function areSeriesChanged(a: YagrConfig['series'], b?: YagrConfig['series']) {
return false;
}

function setConfigImpl(yagr: Yagr, batch: Batch, newConfig: Partial<YagrConfig>) {
function setConfigImpl(yagr: Yagr, batch: Batch, newConfig: Partial<YagrConfig>, fullUpdate = false) {
if (fullUpdate) {
yagr.config = {...yagr.config, ...newConfig};
batch.reinit = true;
return;
}

const isChangedKey = isChanged(yagr.config, newConfig);

if (newConfig.title && isChangedKey('title')) {
Expand Down Expand Up @@ -515,8 +521,8 @@ export class DynamicUpdatesMixin<T extends MinimalValidConfig> {
* @param newConfig Partial<YagrConfig>
* @descriptino Sets new config and redraws.
*/
setConfig(this: Yagr<T>, newConfig: Partial<YagrConfig>) {
this.batch((batch) => setConfigImpl(this, batch, newConfig));
setConfig(this: Yagr<T>, newConfig: Partial<YagrConfig>, fullUpdate = false) {
this.batch((batch) => setConfigImpl(this, batch, newConfig, fullUpdate));
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface YagrChartProps {
config: MinimalValidConfig;
/** Root component class name */
className?: string;
/** Update strategy */
update?: 'dynamic' | 'hard';
/** Debug data */
debug?: {
filename: string;
Expand All @@ -29,7 +31,7 @@ export interface YagrReactRef {

// eslint-disable-next-line prefer-arrow-callback
export default React.forwardRef(function YagrReact(
{id, config, className = '', debug, onChartLoad, onSelect}: YagrChartProps,
{id, config, className = '', debug, onChartLoad, onSelect, update = 'dynamic'}: YagrChartProps,
ref,
) {
const chartRef = React.useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -63,7 +65,7 @@ export default React.forwardRef(function YagrReact(
}, []);

React.useEffect(() => {
config && chart.current?.setConfig(config);
config && chart.current?.setConfig(config, update === 'hard');
}, [config]);

React.useEffect(() => {
Expand Down

0 comments on commit 104334f

Please sign in to comment.