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

Proposal for #5463: Add a resetState method to splitter to manually reset state #6394

Merged
Merged
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
9 changes: 8 additions & 1 deletion packages/primevue/src/splitter/Splitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ export interface SplitterEmitsOptions {

export declare type SplitterEmits = EmitFn<SplitterEmitsOptions>;

export interface SplitterMethods {
/**
* This method resizes all panels by either using the stored state in the case of a stateful Splitter, the size property of each SplitterPanel, or by resetting them to their default values.
*/
resetState(): void;
}

/**
* **PrimeVue - Splitter**
*
Expand All @@ -238,7 +245,7 @@ export declare type SplitterEmits = EmitFn<SplitterEmitsOptions>;
* @group Component
*
*/
declare const Splitter: DefineComponent<SplitterProps, SplitterSlots, SplitterEmits>;
declare const Splitter: DefineComponent<SplitterProps, SplitterSlots, SplitterEmits, SplitterMethods>;

declare module 'vue' {
export interface GlobalComponents {
Expand Down
56 changes: 31 additions & 25 deletions packages/primevue/src/splitter/Splitter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
</template>

<script>
import { getVNodeProp } from '@primevue/core/utils';
import { getWidth, getHeight, getOuterWidth, getOuterHeight } from '@primeuix/utils/dom';
import { getHeight, getOuterHeight, getOuterWidth, getWidth } from '@primeuix/utils/dom';
import { isArray } from '@primeuix/utils/object';
import { getVNodeProp } from '@primevue/core/utils';
import BaseSplitter from './BaseSplitter.vue';

export default {
Expand Down Expand Up @@ -53,29 +53,7 @@ export default {
};
},
mounted() {
if (this.panels && this.panels.length) {
let initialized = false;

if (this.isStateful()) {
initialized = this.restoreState();
}

if (!initialized) {
let children = [...this.$el.children].filter((child) => child.getAttribute('data-pc-name') === 'splitterpanel');
let _panelSizes = [];

this.panels.map((panel, i) => {
let panelInitialSize = panel.props && panel.props.size ? panel.props.size : null;
let panelSize = panelInitialSize || 100 / this.panels.length;

_panelSizes[i] = panelSize;
children[i].style.flexBasis = 'calc(' + panelSize + '% - ' + (this.panels.length - 1) * this.gutterSize + 'px)';
});

this.panelSizes = _panelSizes;
this.prevSize = parseFloat(_panelSizes[0]).toFixed(4);
}
}
this.initializePanels();
},
beforeUnmount() {
this.clear();
Expand All @@ -85,6 +63,31 @@ export default {
isSplitterPanel(child) {
return child.type.name === 'SplitterPanel';
},
initializePanels() {
if (this.panels && this.panels.length) {
let initialized = false;

if (this.isStateful()) {
initialized = this.restoreState();
}

if (!initialized) {
let children = [...this.$el.children].filter((child) => child.getAttribute('data-pc-name') === 'splitterpanel');
let _panelSizes = [];

this.panels.map((panel, i) => {
let panelInitialSize = panel.props && panel.props.size ? panel.props.size : null;
let panelSize = panelInitialSize || 100 / this.panels.length;

_panelSizes[i] = panelSize;
children[i].style.flexBasis = 'calc(' + panelSize + '% - ' + (this.panels.length - 1) * this.gutterSize + 'px)';
});

this.panelSizes = _panelSizes;
this.prevSize = parseFloat(_panelSizes[0]).toFixed(4);
}
}
},
onResizeStart(event, index, isKeyDown) {
this.gutterElement = event.currentTarget || event.target.parentElement;
this.size = this.horizontal ? getWidth(this.$el) : getHeight(this.$el);
Expand Down Expand Up @@ -348,6 +351,9 @@ export default {
}

return false;
},
resetState() {
this.initializePanels();
}
},
computed: {
Expand Down
Loading