1 | import React from "react";
|
2 | import { useDataObject } from "./context";
|
3 | import setDataObjectField from "./setDataObjectField";
|
4 |
|
5 | function BoundInput(props, forwardRef) {
|
6 | const { as, children, field, inputNullable, ...other } = props;
|
7 | const { currentRow, dataObject } = useDataObject();
|
8 | const Component = as || "input";
|
9 |
|
10 | const valueProps = {};
|
11 | const current = currentRow[field];
|
12 | if (other.type === "checkbox") {
|
13 | valueProps.checked = current || false;
|
14 | } else if (other.type === "date" && current instanceof Date) {
|
15 | valueProps.value = current.toISOString().substring(0, 10);
|
16 | } else if (["datetime", "datetime-local"].includes(other.type) && current instanceof Date) {
|
17 | valueProps.value = current.toISOString().substring(0, 16);
|
18 | } else {
|
19 | valueProps.value = inputNullable ? current : current ?? "";
|
20 | }
|
21 |
|
22 | return (
|
23 | <Component
|
24 | ref={forwardRef}
|
25 | onChange={evt => setDataObjectField(dataObject, field, evt)}
|
26 | data-field={field}
|
27 | {...valueProps}
|
28 | {...other}
|
29 | >
|
30 | {children}
|
31 | </Component>
|
32 | );
|
33 | }
|
34 |
|
35 | export default React.forwardRef(BoundInput);
|