-
Notifications
You must be signed in to change notification settings - Fork 600
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
3 changed files
with
117 additions
and
89 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
103 changes: 103 additions & 0 deletions
103
...ages/devextreme/js/__internal/grids/new/card_view/header_panel/resizable_header_panel.tsx
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,103 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { resizeObserverSingleton } from '@ts/core/m_resize_observer'; | ||
import { getBoundingRect } from '@ts/core/utils/m_position'; | ||
import { Component, createRef } from 'inferno'; | ||
|
||
import type { HeaderPanelProps } from './header_panel'; | ||
import { HeaderPanel } from './header_panel'; | ||
|
||
export type Props = Omit<HeaderPanelProps, 'shownColumnCount' | 'elementRef'>; | ||
|
||
interface State { | ||
shownColumnCount: number; | ||
} | ||
|
||
export class ResizableHeaderPanel extends Component<Props, State> { | ||
private status: 'initial' | 'shrinking' | 'normal' = 'initial'; | ||
|
||
private normalHeight = 0; | ||
|
||
private readonly ref = createRef<HTMLDivElement>(); | ||
|
||
state = { | ||
shownColumnCount: 1, | ||
}; | ||
|
||
public render(): JSX.Element { | ||
return ( | ||
<HeaderPanel | ||
{...this.props} | ||
elementRef={this.ref} | ||
shownColumnCount={this.state.shownColumnCount} | ||
/> | ||
); | ||
} | ||
|
||
private getHeight(): number { | ||
return getBoundingRect(this.ref.current!).height as number; | ||
} | ||
|
||
private updateShownColumns(): void { | ||
// eslint-disable-next-line no-undef-init | ||
let stateToApply: undefined | State = undefined; | ||
|
||
switch (this.status) { | ||
case 'initial': { | ||
if (this.state.shownColumnCount !== 1) { | ||
throw new Error(); | ||
} | ||
|
||
if (!this.props.columns.length) { | ||
return; | ||
} | ||
|
||
this.normalHeight = this.getHeight(); | ||
this.status = 'shrinking'; | ||
stateToApply = { | ||
shownColumnCount: this.props.columns.length, | ||
}; | ||
break; | ||
} | ||
|
||
case 'shrinking': { | ||
if (this.getHeight() > this.normalHeight) { | ||
stateToApply = { | ||
shownColumnCount: this.state.shownColumnCount - 1, | ||
}; | ||
} else { | ||
this.status = 'normal'; | ||
} | ||
break; | ||
} | ||
|
||
case 'normal': { | ||
this.status = 'shrinking'; | ||
stateToApply = { | ||
shownColumnCount: this.props.columns.length, | ||
}; | ||
break; | ||
} | ||
|
||
default: { | ||
// eslint-disable-next-line max-len | ||
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars | ||
const _: never = this.status; | ||
} | ||
} | ||
|
||
if (stateToApply) { | ||
this.setState(stateToApply); | ||
} | ||
} | ||
|
||
componentDidMount(): void { | ||
resizeObserverSingleton.observe( | ||
this.ref.current!, | ||
() => this.updateShownColumns(), | ||
); | ||
} | ||
|
||
componentDidUpdate(): void { | ||
this.updateShownColumns(); | ||
} | ||
} |
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