@olenbetong/appframe-ds 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ import { Button, Tooltip } from "@digdir/designsystemet-react";
2
+ import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
3
+ import ChevronRightIcon from "@mui/icons-material/ChevronRight";
4
+ import { getLocalizedString } from "@olenbetong/appframe-core";
5
+ import type { DataObject } from "@olenbetong/appframe-data";
6
+ import { useCurrentIndex, useDataLength, useDataObject } from "@olenbetong/appframe-react";
7
+ import { CancelIconButton } from "./CancelIconButton.js";
8
+ import { RefreshRowIconButton } from "./RefreshRowIconButton.js";
9
+ import { SaveIconButton } from "./SaveIconButton.js";
10
+
11
+ function IndexNavigator({ dataObject }: { dataObject: DataObject<any> }) {
12
+ let count = useDataLength(dataObject);
13
+ let index = useCurrentIndex(dataObject);
14
+
15
+ if (count <= 1) {
16
+ return null;
17
+ }
18
+
19
+ return (
20
+ <>
21
+ <Button
22
+ icon
23
+ variant="tertiary"
24
+ aria-label={getLocalizedString("Previous")}
25
+ onClick={() => dataObject.setCurrentIndex(index - 1)}
26
+ disabled={index <= 0}
27
+ >
28
+ <ChevronLeftIcon />
29
+ </Button>
30
+ <Button
31
+ icon
32
+ variant="tertiary"
33
+ aria-label={getLocalizedString("Next")}
34
+ onClick={() => dataObject.setCurrentIndex(index + 1)}
35
+ disabled={index >= count - 1}
36
+ >
37
+ <ChevronRightIcon />
38
+ </Button>
39
+ </>
40
+ );
41
+ }
42
+
43
+ export type DataEditToolbarProps = React.HTMLAttributes<HTMLDivElement> & {
44
+ /**
45
+ * If different data objects are used for navigation and editing, you can set the data object
46
+ * used for navigation with this property.
47
+ */
48
+ navigationDataObject?: DataObject<any>;
49
+ /**
50
+ * If true, will add buttons to move to the next/previous index in the data object.
51
+ */
52
+ showNavigation?: boolean;
53
+ };
54
+
55
+ export function DataEditToolbar({
56
+ children,
57
+ navigationDataObject,
58
+ showNavigation,
59
+ style,
60
+ ...props
61
+ }: DataEditToolbarProps) {
62
+ let dataObject = useDataObject();
63
+
64
+ return (
65
+ <div
66
+ style={{
67
+ display: "flex",
68
+ alignItems: "center",
69
+ minHeight: "48px",
70
+ borderBottom: "1px solid rgb(0 0 0 / 0.12)",
71
+ borderTop: "1px solid rgb(0 0 0 / 0.12)",
72
+ ...style,
73
+ }}
74
+ {...props}
75
+ >
76
+ <Tooltip content={getLocalizedString("Save changes")}>
77
+ <SaveIconButton />
78
+ </Tooltip>
79
+ <Tooltip content={getLocalizedString("Revert unsaved changes")}>
80
+ <CancelIconButton />
81
+ </Tooltip>
82
+ <Tooltip content={getLocalizedString("Refresh record")}>
83
+ <RefreshRowIconButton />
84
+ </Tooltip>
85
+ {children}
86
+ {showNavigation && (
87
+ <div style={{ marginLeft: "auto" }}>
88
+ <IndexNavigator dataObject={navigationDataObject ?? dataObject} />
89
+ </div>
90
+ )}
91
+ </div>
92
+ );
93
+ }
@@ -0,0 +1,121 @@
1
+ import { Field, Label, Select } from "@digdir/designsystemet-react";
2
+ import type { DataObject } from "@olenbetong/appframe-data";
3
+ import { useFetchData, useField } from "@olenbetong/appframe-react";
4
+ import { useEffect, useId, useRef } from "react";
5
+
6
+ export type DataObjectSelectProps<T extends Record<string, unknown>> = React.SelectHTMLAttributes<HTMLSelectElement> & {
7
+ /**
8
+ * Data object to get options from. Only the data handler is used, so the component
9
+ * will not affect the state of the data object.
10
+ */
11
+ dataObject: DataObject<T>;
12
+ /**
13
+ * Filter to use when getting options.
14
+ */
15
+ filter?: string;
16
+ /**
17
+ * The label displayed above the select.
18
+ */
19
+ label?: React.ReactNode;
20
+ /**
21
+ * Whether the user is allowed to clear the value. Will add an empty option at the
22
+ * start of the options list.
23
+ */
24
+ nullable?: boolean;
25
+ /**
26
+ * If nullable is true, this is the label for the empty option. Default is an empty string.
27
+ */
28
+ blankLabel?: string;
29
+ /**
30
+ * The field in the options record to use as the value.
31
+ */
32
+ valueField?: string;
33
+ /**
34
+ * The field in the options record to use as the label.
35
+ */
36
+ labelField?: string;
37
+ /**
38
+ * If the value isn't simply a record field, you can pass a function here to
39
+ * get the value from the record.
40
+ */
41
+ getOptionValue?: (option: T) => string;
42
+ /**
43
+ * If you want to display labels that are not simply a record field, you can
44
+ * pass a function that takes a record and returns a label.
45
+ */
46
+ getOptionLabel?: (item: T) => string;
47
+ };
48
+
49
+ export function DataObjectSelect<T extends Record<string, unknown>>({
50
+ nullable = false,
51
+ blankLabel = "",
52
+ dataObject,
53
+ filter,
54
+ id: idProp,
55
+ label,
56
+ name,
57
+ getOptionLabel,
58
+ getOptionValue,
59
+ valueField = "ID",
60
+ labelField = "Description",
61
+ value,
62
+ defaultValue,
63
+ ...props
64
+ }: DataObjectSelectProps<T>) {
65
+ let { data } = useFetchData(dataObject, filter ?? "");
66
+ let fallbackId = useId();
67
+ let id = idProp ?? fallbackId;
68
+ let inputRef = useRef<HTMLSelectElement>(null);
69
+
70
+ // biome-ignore lint/correctness/useExhaustiveDependencies: default value should only affect the initial value
71
+ useEffect(() => {
72
+ let defaultExists = data.find((r) => String(getOptionValue?.(r) ?? r[valueField]) === defaultValue);
73
+ if (inputRef.current && defaultExists) {
74
+ inputRef.current.value = defaultValue as string;
75
+ }
76
+ }, [data]);
77
+
78
+ return (
79
+ <Field>
80
+ {label && (
81
+ <Label htmlFor={id}>
82
+ {label}
83
+ {props.required && " *"}
84
+ </Label>
85
+ )}
86
+ <Select id={id} name={name} {...props} value={value === undefined ? undefined : (value ?? "")} ref={inputRef}>
87
+ {nullable && <option value="">{blankLabel}</option>}
88
+ {data.map((t) => {
89
+ let optionValue = getOptionValue?.(t) ?? t[valueField];
90
+ let optionLabel = getOptionLabel?.(t) ?? t[labelField];
91
+
92
+ return (
93
+ <option value={optionValue as any} key={optionValue as any}>
94
+ {optionLabel as any}
95
+ </option>
96
+ );
97
+ })}
98
+ </Select>
99
+ </Field>
100
+ );
101
+ }
102
+
103
+ export function BoundDataObjectSelect<T extends Record<string, unknown>>({
104
+ field,
105
+ ...props
106
+ }: DataObjectSelectProps<T> & {
107
+ /**
108
+ * Field to bind the value of the select component to.
109
+ */
110
+ field: string;
111
+ }) {
112
+ let { value, setValue } = useField(field);
113
+
114
+ return (
115
+ <DataObjectSelect
116
+ value={(value ?? "") as string}
117
+ onChange={(event) => setValue((event.target as HTMLSelectElement).value)}
118
+ {...props}
119
+ />
120
+ );
121
+ }
@@ -0,0 +1,31 @@
1
+ import { Button, type ButtonProps } from "@digdir/designsystemet-react";
2
+ import { getLocalizedString } from "@olenbetong/appframe-core";
3
+ import { type DeletePrompt, useDeleteButton } from "@olenbetong/appframe-react";
4
+ import { type ForwardRefExoticComponent, forwardRef, type RefAttributes } from "react";
5
+
6
+ export type DeleteButtonProps = ButtonProps & { index?: number; prompt?: DeletePrompt };
7
+
8
+ export const DeleteButton: ForwardRefExoticComponent<DeleteButtonProps & RefAttributes<HTMLButtonElement>> = forwardRef<
9
+ HTMLButtonElement,
10
+ DeleteButtonProps
11
+ >(function DeleteButton({ prompt, onClick, index, children, ...props }, ref) {
12
+ let { deleting, deleteRow } = useDeleteButton(prompt);
13
+
14
+ return (
15
+ <Button
16
+ loading={deleting}
17
+ data-color="danger"
18
+ variant="secondary"
19
+ onClick={(evt) => {
20
+ onClick?.(evt);
21
+ if (!evt.defaultPrevented) {
22
+ deleteRow(index);
23
+ }
24
+ }}
25
+ {...props}
26
+ ref={ref}
27
+ >
28
+ {children ?? getLocalizedString("Delete")}
29
+ </Button>
30
+ );
31
+ });
@@ -0,0 +1,34 @@
1
+ import { Button, type ButtonProps } from "@digdir/designsystemet-react";
2
+ import DeleteIcon from "@mui/icons-material/Delete";
3
+ import { getLocalizedString } from "@olenbetong/appframe-core";
4
+ import { type DeletePrompt, useDeleteButton } from "@olenbetong/appframe-react";
5
+ import { forwardRef } from "react";
6
+
7
+ export type DeleteIconButtonProps = Omit<ButtonProps, "icon"> & { index?: number; prompt?: DeletePrompt };
8
+
9
+ export const DeleteIconButton = forwardRef<HTMLButtonElement, DeleteIconButtonProps>(function DeleteIconButton(
10
+ { prompt, onClick, index, children, ...props },
11
+ ref,
12
+ ) {
13
+ let { isDeleting, deleteRow } = useDeleteButton(prompt);
14
+
15
+ return (
16
+ <Button
17
+ icon
18
+ loading={isDeleting}
19
+ data-color="danger"
20
+ aria-label={getLocalizedString("Delete")}
21
+ variant="tertiary"
22
+ onClick={(evt) => {
23
+ onClick?.(evt);
24
+ if (!evt.defaultPrevented) {
25
+ deleteRow(index);
26
+ }
27
+ }}
28
+ {...props}
29
+ ref={ref}
30
+ >
31
+ {children ?? <DeleteIcon />}
32
+ </Button>
33
+ );
34
+ });
@@ -0,0 +1,27 @@
1
+ import { Button, type ButtonProps } from "@digdir/designsystemet-react";
2
+ import { getLocalizedString } from "@olenbetong/appframe-core";
3
+ import { useRefreshButton } from "@olenbetong/appframe-react";
4
+ import { type ForwardRefExoticComponent, forwardRef, type RefAttributes } from "react";
5
+
6
+ export const RefreshButton: ForwardRefExoticComponent<ButtonProps & RefAttributes<HTMLButtonElement>> = forwardRef<
7
+ HTMLButtonElement,
8
+ ButtonProps
9
+ >(function RefreshButton({ onClick, children, ...props }, ref) {
10
+ let { loading, refresh } = useRefreshButton();
11
+
12
+ return (
13
+ <Button
14
+ loading={loading}
15
+ onClick={(evt) => {
16
+ onClick?.(evt);
17
+ if (!evt.defaultPrevented) {
18
+ refresh();
19
+ }
20
+ }}
21
+ {...props}
22
+ ref={ref}
23
+ >
24
+ {children ?? getLocalizedString("Refresh")}
25
+ </Button>
26
+ );
27
+ });
@@ -0,0 +1,32 @@
1
+ import { Button, type ButtonProps } from "@digdir/designsystemet-react";
2
+ import RefreshIcon from "@mui/icons-material/Refresh";
3
+ import { getLocalizedString } from "@olenbetong/appframe-core";
4
+ import { useDataObject, useLoading } from "@olenbetong/appframe-react";
5
+ import { forwardRef } from "react";
6
+
7
+ export const RefreshIconButton = forwardRef<HTMLButtonElement, Omit<ButtonProps, "icon">>(function RefreshIconButton(
8
+ { onClick, children, ...props },
9
+ ref,
10
+ ) {
11
+ let dataObject = useDataObject();
12
+ let loading = useLoading(dataObject);
13
+
14
+ return (
15
+ <Button
16
+ icon
17
+ loading={loading}
18
+ aria-label={getLocalizedString("Refresh data")}
19
+ variant="tertiary"
20
+ onClick={(evt) => {
21
+ onClick?.(evt);
22
+ if (!evt.defaultPrevented) {
23
+ dataObject.refreshDataSource();
24
+ }
25
+ }}
26
+ {...props}
27
+ ref={ref}
28
+ >
29
+ {children ?? <RefreshIcon />}
30
+ </Button>
31
+ );
32
+ });
@@ -0,0 +1,32 @@
1
+ import { Button, type ButtonProps } from "@digdir/designsystemet-react";
2
+ import RefreshIcon from "@mui/icons-material/Refresh";
3
+ import { getLocalizedString } from "@olenbetong/appframe-core";
4
+ import { useRefreshRowButton } from "@olenbetong/appframe-react";
5
+ import { forwardRef } from "react";
6
+
7
+ export type RefreshRowIconButtonProps = Omit<ButtonProps, "icon"> & { index?: number };
8
+
9
+ export const RefreshRowIconButton = forwardRef<HTMLButtonElement, RefreshRowIconButtonProps>(
10
+ function RefreshRowIconButton({ onClick, index, children, ...props }, ref) {
11
+ let { loading, refreshRow } = useRefreshRowButton();
12
+
13
+ return (
14
+ <Button
15
+ icon
16
+ loading={loading}
17
+ aria-label={getLocalizedString("Refresh current row")}
18
+ variant="tertiary"
19
+ onClick={(evt) => {
20
+ onClick?.(evt);
21
+ if (!evt.defaultPrevented) {
22
+ refreshRow(index);
23
+ }
24
+ }}
25
+ {...props}
26
+ ref={ref}
27
+ >
28
+ {children ?? <RefreshIcon />}
29
+ </Button>
30
+ );
31
+ },
32
+ );
@@ -0,0 +1,27 @@
1
+ import { Button, type ButtonProps } from "@digdir/designsystemet-react";
2
+ import { getLocalizedString } from "@olenbetong/appframe-core";
3
+ import { useSaveButton } from "@olenbetong/appframe-react";
4
+ import { type ForwardRefExoticComponent, forwardRef, type RefAttributes } from "react";
5
+
6
+ export const SaveButton: ForwardRefExoticComponent<ButtonProps & RefAttributes<HTMLButtonElement>> = forwardRef<
7
+ HTMLButtonElement,
8
+ ButtonProps
9
+ >(function SaveButton({ onClick, children, ...props }, ref) {
10
+ let { saving, save } = useSaveButton();
11
+
12
+ return (
13
+ <Button
14
+ loading={saving}
15
+ onClick={(evt) => {
16
+ onClick?.(evt);
17
+ if (!evt.defaultPrevented) {
18
+ save();
19
+ }
20
+ }}
21
+ {...props}
22
+ ref={ref}
23
+ >
24
+ {children ?? getLocalizedString("Save")}
25
+ </Button>
26
+ );
27
+ });
@@ -0,0 +1,31 @@
1
+ import { Button, type ButtonProps } from "@digdir/designsystemet-react";
2
+ import SaveIcon from "@mui/icons-material/Save";
3
+ import { getLocalizedString } from "@olenbetong/appframe-core";
4
+ import { useSaveButton } from "@olenbetong/appframe-react";
5
+ import { forwardRef } from "react";
6
+
7
+ export const SaveIconButton = forwardRef<HTMLButtonElement, Omit<ButtonProps, "icon">>(function SaveIconButton(
8
+ { onClick, children, ...props },
9
+ ref,
10
+ ) {
11
+ let { saving, save } = useSaveButton();
12
+
13
+ return (
14
+ <Button
15
+ icon
16
+ loading={saving}
17
+ aria-label={getLocalizedString("Save")}
18
+ variant="tertiary"
19
+ onClick={(evt) => {
20
+ onClick?.(evt);
21
+ if (!evt.defaultPrevented) {
22
+ save();
23
+ }
24
+ }}
25
+ {...props}
26
+ ref={ref}
27
+ >
28
+ {children ?? <SaveIcon />}
29
+ </Button>
30
+ );
31
+ });
@@ -0,0 +1,19 @@
1
+ export * from "./BoundCheckbox.js";
2
+ export * from "./BoundDatePicker.js";
3
+ export * from "./BoundDateTimePicker.js";
4
+ export * from "./BoundInput.js";
5
+ export * from "./BoundNumericTextField.js";
6
+ export * from "./BoundSelect.js";
7
+ export * from "./BoundSwitch.js";
8
+ export * from "./BoundTextField.js";
9
+ export * from "./CancelButton.js";
10
+ export * from "./CancelIconButton.js";
11
+ export * from "./DataEditToolbar.js";
12
+ export * from "./DataObjectSelect.js";
13
+ export * from "./DeleteButton.js";
14
+ export * from "./DeleteIconButton.js";
15
+ export * from "./RefreshButton.js";
16
+ export * from "./RefreshIconButton.js";
17
+ export * from "./RefreshRowIconButton.js";
18
+ export * from "./SaveButton.js";
19
+ export * from "./SaveIconButton.js";
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./AfLookup/index.js";
2
+ export * from "./binding/index.js";
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../tsconfig.build.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./es"
6
+ },
7
+ "include": ["src"],
8
+ "exclude": ["es", "node_modules"]
9
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../tsconfig.build.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./es"
6
+ },
7
+ "include": ["src"]
8
+ }