forked from finos/vuu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-data-item-edit-control.tsx
More file actions
108 lines (104 loc) · 2.49 KB
/
get-data-item-edit-control.tsx
File metadata and controls
108 lines (104 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import type {
DataValueDescriptor,
TableSchemaTable,
} from "@vuu-ui/vuu-data-types";
import {
VuuDatePicker,
VuuInput,
VuuTimePicker,
VuuTypeaheadInput,
VuuTypeaheadInputProps,
} from "@vuu-ui/vuu-ui-controls";
import {
CommitHandler,
isDateTimeDataValue,
isTimeDataValue,
} from "@vuu-ui/vuu-utils";
import { InputProps } from "@salt-ds/core";
import { asTimeString } from "@vuu-ui/vuu-utils";
export interface DataItemEditControlProps {
InputProps?: Partial<InputProps>;
TypeaheadProps?: Pick<VuuTypeaheadInputProps, "highlightFirstSuggestion">;
className?: string;
commitWhenCleared?: boolean;
/**
* A table column or form field Descriptor.
*/
dataDescriptor: DataValueDescriptor;
errorMessage?: string;
onCommit: CommitHandler<HTMLElement>;
table?: TableSchemaTable;
}
export type ValidationStatus = "initial" | true | string;
export const getDataItemEditControl = ({
InputProps,
TypeaheadProps,
className,
commitWhenCleared,
dataDescriptor,
errorMessage,
onCommit,
table,
}: DataItemEditControlProps) => {
const handleCommitNumber: CommitHandler<HTMLElement, number> = (
evt,
value,
) => {
onCommit(evt, value.toString());
};
if (dataDescriptor.editable === false) {
return (
<VuuInput
variant="secondary"
{...InputProps}
onCommit={onCommit}
readOnly
data-edit-control
/>
);
} else if (isTimeDataValue(dataDescriptor)) {
if (InputProps?.inputProps) {
const { value, onChange } = InputProps.inputProps;
return (
<VuuTimePicker
className={className}
value={asTimeString(value, true)}
onChange={onChange}
onCommit={onCommit}
data-edit-control
/>
);
}
} else if (isDateTimeDataValue(dataDescriptor)) {
return (
<VuuDatePicker
className={className}
onCommit={handleCommitNumber}
data-edit-control
/>
);
} else if (dataDescriptor.serverDataType === "string" && table) {
return (
<VuuTypeaheadInput
{...InputProps}
{...TypeaheadProps}
className={className}
column={dataDescriptor.name}
onCommit={onCommit}
table={table}
data-edit-control
/>
);
}
return (
<VuuInput
variant="secondary"
{...InputProps}
className={className}
commitWhenCleared={commitWhenCleared}
onCommit={onCommit}
errorMessage={errorMessage}
data-edit-control
/>
);
};