Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 2e091fa

Browse files
authored
Update the open dialog API with more options. (#14)
Signed-off-by: Mikhail Aheichyk <[email protected]>
1 parent 59927a8 commit 2e091fa

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

src/ModuleApi.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import React from "react";
1919
import { PlainSubstitution, TranslationStringsObject } from "./types/translations";
2020
import { DialogContent, DialogProps } from "./components/DialogContent";
2121
import { AccountAuthInfo } from "./types/AccountAuthInfo";
22+
import { ModuleUiDialogOptions } from "./types/ModuleUiDialogOptions";
2223

2324
/**
2425
* A module API surface for the react-sdk. Provides a stable API for modules to
@@ -46,14 +47,16 @@ export interface ModuleApi {
4647

4748
/**
4849
* Opens a dialog in the client.
49-
* @param title The title of the dialog
50+
* @param initialTitleOrOptions Initial options for the dialog. Can be the title of the dialog, or a
51+
* configuration object. Note that the dialog implementation may later
52+
* modify its own options via the {@link DialogProps.setOptions} callback.
5053
* @param body The function which creates a body component for the dialog.
51-
* @param props Optional props to provide to the dialog.
54+
* @param props Optional props to provide to {@link body}, in addition to the common set in {@link DialogProps}.
5255
* @returns Whether the user submitted the dialog or closed it, and the model returned by the
5356
* dialog component if submitted.
5457
*/
5558
openDialog<M extends object, P extends DialogProps = DialogProps, C extends DialogContent<P> = DialogContent<P>>(
56-
title: string,
59+
initialTitleOrOptions: string | ModuleUiDialogOptions,
5760
body: (props: P, ref: React.RefObject<C>) => React.ReactNode,
5861
props?: Omit<P, keyof DialogProps>,
5962
): Promise<{ didOkOrSubmit: boolean, model: M }>;

src/components/DialogContent.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,44 @@ limitations under the License.
1717
import * as React from "react";
1818
import { ModuleApi } from "../ModuleApi";
1919
import { PlainSubstitution } from "../types/translations";
20+
import { ModuleUiDialogOptions } from "../types/ModuleUiDialogOptions";
2021

22+
/** React properties for dialog content implementations based on {@link DialogContent} */
2123
export interface DialogProps {
24+
/**
25+
* A reference to the active Module API.
26+
*/
2227
moduleApi: ModuleApi;
28+
29+
/**
30+
* Callback to update the dialog options.
31+
*
32+
* Dialog content implementations can call this to update any of the options that were
33+
* originally set via {@link ModuleApi.openDialog}.
34+
*
35+
* @param options - The updates that should be applied to the dialog options. Any properties
36+
* not set in the {@link options} are left unchanged.
37+
*/
38+
setOptions(options: Partial<ModuleUiDialogOptions>): void;
39+
40+
/**
41+
* Cancel the dialog programmatically.
42+
*/
43+
cancel(): void;
2344
}
2445

46+
/** State of {@link DialogContent} */
2547
export interface DialogState {
2648
busy: boolean;
2749
error?: string;
2850
}
2951

52+
/**
53+
* Base class for the content of a Dialog.
54+
*
55+
* The `body` callback passed to {@link ModuleApi.openDialog} should return an instance of a
56+
* class based on this.
57+
*/
3058
export abstract class DialogContent<P extends DialogProps = DialogProps, S extends DialogState = DialogState, M extends object = {}>
3159
extends React.PureComponent<P, S> {
3260

src/types/ModuleUiDialogOptions.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2023 Mikhail Aheichyk
3+
Copyright 2023 Nordeck IT + Consulting GmbH.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
/**
19+
* Options for {@link ModuleApi#openDialog}.
20+
*/
21+
export interface ModuleUiDialogOptions {
22+
/**
23+
* The title of the dialog.
24+
*/
25+
title: string;
26+
27+
/**
28+
* The label of the action button. If unset, a default label will be used.
29+
*/
30+
actionLabel?: string;
31+
32+
/**
33+
* The label of the cancel button. If unset, a default label will be used.
34+
*/
35+
cancelLabel?: string;
36+
37+
/**
38+
* Enable or disable the action button when the dialog is created.
39+
* @defaultValue The default is `true`
40+
*/
41+
canSubmit?: boolean;
42+
}

0 commit comments

Comments
 (0)