-
Notifications
You must be signed in to change notification settings - Fork 59
Pre dynamic page changes #1982
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
Open
iobuhov
wants to merge
7
commits into
main
Choose a base branch
from
pre-dynamic-page-changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,618
−479
Open
Pre dynamic page changes #1982
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dffb3d4
refactor: extract logic to grid plugin
iobuhov 85c5662
refactor: rewrite empty placeholder
iobuhov 163dc89
refactor: separate code in plugin
iobuhov 4d94c09
refactor: split select-all feature
iobuhov 45dd033
style: fix comment
iobuhov 3484b67
style: fix comments & type
iobuhov 5b97175
fix: update test types
iobuhov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
16 changes: 16 additions & 0 deletions
16
packages/pluggableWidgets/datagrid-web/src/features/empty-message/EmptyPlaceholder.tsx
This file contains hidden or 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,16 @@ | ||
| import classNames from "classnames"; | ||
| import { observer } from "mobx-react-lite"; | ||
| import { ReactNode } from "react"; | ||
| import { useEmptyPlaceholderVM } from "./injection-hooks"; | ||
|
|
||
| export const EmptyPlaceholder = observer(function EmptyPlaceholder(): ReactNode { | ||
| const vm = useEmptyPlaceholderVM(); | ||
|
|
||
| if (!vm.content) return null; | ||
|
|
||
| return ( | ||
| <div className={classNames("td", "td-borders")} style={vm.style}> | ||
| <div className="empty-placeholder">{vm.content}</div> | ||
| </div> | ||
| ); | ||
| }); |
32 changes: 32 additions & 0 deletions
32
...es/pluggableWidgets/datagrid-web/src/features/empty-message/EmptyPlaceholder.viewModel.ts
This file contains hidden or 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,32 @@ | ||
| import { ComputedAtom } from "@mendix/widget-plugin-mobx-kit/main"; | ||
| import { makeAutoObservable } from "mobx"; | ||
| import { CSSProperties, ReactNode } from "react"; | ||
|
|
||
| export class EmptyPlaceholderViewModel { | ||
| constructor( | ||
| private widgets: ComputedAtom<ReactNode>, | ||
| private visibleColumnsCount: ComputedAtom<number>, | ||
| private config: { checkboxColumnEnabled: boolean; selectorColumnEnabled: boolean } | ||
| ) { | ||
| makeAutoObservable(this); | ||
| } | ||
|
|
||
| get content(): ReactNode { | ||
| return this.widgets.get(); | ||
| } | ||
|
|
||
| get span(): number { | ||
| let span = this.visibleColumnsCount.get(); | ||
| if (this.config.checkboxColumnEnabled) { | ||
| span += 1; | ||
| } | ||
| if (this.config.selectorColumnEnabled) { | ||
| span += 1; | ||
| } | ||
| return Math.max(span, 1); | ||
| } | ||
|
|
||
| get style(): CSSProperties { | ||
| return { gridColumn: `span ${this.span}` }; | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
...gets/datagrid-web/src/features/empty-message/__tests__/EmptyPlaceholder.viewModel.spec.ts
This file contains hidden or 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,52 @@ | ||
| import { computed, observable } from "mobx"; | ||
| import { ReactNode } from "react"; | ||
| import { EmptyPlaceholderViewModel } from "../EmptyPlaceholder.viewModel"; | ||
|
|
||
| describe("EmptyPlaceholderViewModel", () => { | ||
| describe("style getter", () => { | ||
| it("reacts to changes in visible columns count", () => { | ||
| const mockWidgets = computed(() => "Empty message" as ReactNode); | ||
| const columnCount = observable.box(3); | ||
| const config = { checkboxColumnEnabled: false, selectorColumnEnabled: false }; | ||
|
|
||
| const viewModel = new EmptyPlaceholderViewModel(mockWidgets, columnCount, config); | ||
|
|
||
| expect(viewModel.style).toEqual({ gridColumn: "span 3" }); | ||
|
|
||
| columnCount.set(5); | ||
| expect(viewModel.style).toEqual({ gridColumn: "span 5" }); | ||
|
|
||
| columnCount.set(0); | ||
| expect(viewModel.style).toEqual({ gridColumn: "span 1" }); | ||
| }); | ||
|
|
||
| it("reacts to changes in visible columns count with config flags enabled", () => { | ||
| const mockWidgets = computed(() => "Empty message" as ReactNode); | ||
| const columnCount = observable.box(3); | ||
| const config = { checkboxColumnEnabled: true, selectorColumnEnabled: true }; | ||
|
|
||
| const viewModel = new EmptyPlaceholderViewModel(mockWidgets, columnCount, config); | ||
|
|
||
| expect(viewModel.style).toEqual({ gridColumn: "span 5" }); | ||
|
|
||
| columnCount.set(5); | ||
| expect(viewModel.style).toEqual({ gridColumn: "span 7" }); | ||
|
|
||
| columnCount.set(0); | ||
| expect(viewModel.style).toEqual({ gridColumn: "span 2" }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("content getter", () => { | ||
| it("returns widgets from atom", () => { | ||
| const message = "Empty message"; | ||
| const atom = computed(() => message); | ||
| const columnCount = observable.box(3); | ||
| const config = { checkboxColumnEnabled: false, selectorColumnEnabled: false }; | ||
|
|
||
| const viewModel = new EmptyPlaceholderViewModel(atom, columnCount, config); | ||
|
|
||
| expect(viewModel.content).toBe(message); | ||
| }); | ||
| }); | ||
| }); |
4 changes: 4 additions & 0 deletions
4
packages/pluggableWidgets/datagrid-web/src/features/empty-message/injection-hooks.ts
This file contains hidden or 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,4 @@ | ||
| import { createInjectionHooks } from "brandi-react"; | ||
| import { DG_TOKENS as DG } from "../../model/tokens"; | ||
|
|
||
| export const [useEmptyPlaceholderVM] = createInjectionHooks(DG.emptyPlaceholderVM); |
156 changes: 25 additions & 131 deletions
156
packages/pluggableWidgets/datagrid-web/src/features/select-all/SelectAllBar.viewModel.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,160 +1,54 @@ | ||
| import { DerivedPropsGate, SetupComponent, SetupComponentHost } from "@mendix/widget-plugin-mobx-kit/main"; | ||
| import { DynamicValue, ListValue, SelectionMultiValue, SelectionSingleValue } from "mendix"; | ||
| import { action, makeAutoObservable, reaction } from "mobx"; | ||
|
|
||
| type DynamicProps = { | ||
| datasource: ListValue; | ||
| selectAllTemplate?: DynamicValue<string>; | ||
| selectAllText?: DynamicValue<string>; | ||
| itemSelection?: SelectionSingleValue | SelectionMultiValue; | ||
| allSelectedText?: DynamicValue<string>; | ||
| }; | ||
|
|
||
| interface SelectService { | ||
| selectAllPages(): Promise<{ success: boolean }> | { success: boolean }; | ||
| clearSelection(): void; | ||
| } | ||
|
|
||
| interface CounterService { | ||
| selectedCount: number; | ||
| selectedCountText: string; | ||
| clearButtonLabel: string; | ||
| } | ||
| import { SelectAllEvents } from "@mendix/widget-plugin-grid/select-all/select-all.model"; | ||
| import { Emitter } from "@mendix/widget-plugin-mobx-kit/main"; | ||
| import { makeAutoObservable } from "mobx"; | ||
|
|
||
| /** @injectable */ | ||
| export class SelectAllBarViewModel implements SetupComponent { | ||
| private barVisible = false; | ||
| private clearVisible = false; | ||
|
|
||
| pending = false; | ||
|
|
||
| export class SelectAllBarViewModel { | ||
| constructor( | ||
| host: SetupComponentHost, | ||
| private readonly gate: DerivedPropsGate<DynamicProps>, | ||
| private readonly selectService: SelectService, | ||
| private readonly count: CounterService, | ||
| private readonly enableSelectAll: boolean | ||
| private emitter: Emitter<SelectAllEvents>, | ||
| private state: { pending: boolean; visible: boolean; clearBtnVisible: boolean }, | ||
| private selectionTexts: { | ||
| clearSelectionButtonLabel: string; | ||
| selectedCountText: string; | ||
| }, | ||
| private selectAllTexts: { | ||
| selectAllLabel: string; | ||
| selectionStatus: string; | ||
| }, | ||
| private enableSelectAll: boolean | ||
| ) { | ||
| host.add(this); | ||
| type PrivateMembers = "setClearVisible" | "setPending" | "hideBar" | "showBar"; | ||
| makeAutoObservable<this, PrivateMembers>(this, { | ||
| setClearVisible: action, | ||
| setPending: action, | ||
| hideBar: action, | ||
| showBar: action | ||
| }); | ||
| } | ||
|
|
||
| private get props(): DynamicProps { | ||
| return this.gate.props; | ||
| } | ||
|
|
||
| private setClearVisible(value: boolean): void { | ||
| this.clearVisible = value; | ||
| } | ||
|
|
||
| private setPending(value: boolean): void { | ||
| this.pending = value; | ||
| } | ||
|
|
||
| private hideBar(): void { | ||
| this.barVisible = false; | ||
| this.clearVisible = false; | ||
| } | ||
|
|
||
| private showBar(): void { | ||
| this.barVisible = true; | ||
| } | ||
|
|
||
| private get total(): number { | ||
| return this.props.datasource.totalCount ?? 0; | ||
| } | ||
|
|
||
| private get selectAllFormat(): string { | ||
| return this.props.selectAllTemplate?.value ?? "Select all %d rows in the data source"; | ||
| } | ||
|
|
||
| private get selectAllText(): string { | ||
| return this.props.selectAllText?.value ?? "Select all rows in the data source"; | ||
| } | ||
|
|
||
| private get allSelectedText(): string { | ||
| const str = this.props.allSelectedText?.value ?? "All %d rows selected."; | ||
| return str.replace("%d", `${this.count.selectedCount}`); | ||
| } | ||
|
|
||
| private get isCurrentPageSelected(): boolean { | ||
| const selection = this.props.itemSelection; | ||
|
|
||
| if (!selection || selection.type === "Single") return false; | ||
|
|
||
| const pageIds = new Set(this.props.datasource.items?.map(item => item.id) ?? []); | ||
| const selectionSubArray = selection.selection.filter(item => pageIds.has(item.id)); | ||
| return selectionSubArray.length === pageIds.size && pageIds.size > 0; | ||
| } | ||
|
|
||
| private get isAllItemsSelected(): boolean { | ||
| if (this.total > 0) return this.total === this.count.selectedCount; | ||
|
|
||
| const { offset, limit, items = [], hasMoreItems } = this.gate.props.datasource; | ||
| const noMoreItems = typeof hasMoreItems === "boolean" && hasMoreItems === false; | ||
| const fullyLoaded = offset === 0 && limit >= items.length; | ||
|
|
||
| return fullyLoaded && noMoreItems && items.length === this.count.selectedCount; | ||
| makeAutoObservable(this); | ||
| } | ||
|
|
||
| get selectAllLabel(): string { | ||
| if (this.total > 0) return this.selectAllFormat.replace("%d", `${this.total}`); | ||
| return this.selectAllText; | ||
| return this.selectAllTexts.selectAllLabel; | ||
| } | ||
|
|
||
| get clearSelectionLabel(): string { | ||
| return this.count.clearButtonLabel; | ||
| return this.selectionTexts.clearSelectionButtonLabel; | ||
| } | ||
|
|
||
| get selectionStatus(): string { | ||
| if (this.isAllItemsSelected) return this.allSelectedText; | ||
| return this.count.selectedCountText; | ||
| return this.selectAllTexts.selectionStatus; | ||
| } | ||
|
|
||
| get isBarVisible(): boolean { | ||
| return this.enableSelectAll && this.barVisible; | ||
| return this.enableSelectAll && this.state.visible; | ||
| } | ||
|
|
||
| get isClearVisible(): boolean { | ||
| return this.clearVisible; | ||
| return this.state.clearBtnVisible; | ||
| } | ||
|
|
||
| get isSelectAllDisabled(): boolean { | ||
| return this.pending; | ||
| } | ||
|
|
||
| setup(): (() => void) | void { | ||
| if (!this.enableSelectAll) return; | ||
|
|
||
| return reaction( | ||
| () => this.isCurrentPageSelected, | ||
| isCurrentPageSelected => { | ||
| if (isCurrentPageSelected === false) { | ||
| this.hideBar(); | ||
| } else if (this.isAllItemsSelected === false) { | ||
| this.showBar(); | ||
| } | ||
| } | ||
| ); | ||
| return this.state.pending; | ||
| } | ||
|
|
||
| onClear(): void { | ||
| this.selectService.clearSelection(); | ||
| this.emitter.emit("clear"); | ||
| } | ||
|
|
||
| async onSelectAll(): Promise<void> { | ||
| this.setPending(true); | ||
| try { | ||
| const { success } = await this.selectService.selectAllPages(); | ||
| this.setClearVisible(success); | ||
| } finally { | ||
| this.setPending(false); | ||
| } | ||
| onSelectAll(): void { | ||
| this.emitter.emit("startSelecting"); | ||
| } | ||
| } |
6 changes: 0 additions & 6 deletions
6
packages/pluggableWidgets/datagrid-web/src/features/select-all/SelectAllGateProps.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.