Skip to content

Commit 68262e5

Browse files
Fix time components (#731)
1 parent 82a4c80 commit 68262e5

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

admin-js/src/App.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,27 @@ import {
1414
BooleanField, DateField, NumberField, ReferenceField, ReferenceManyField,
1515
ReferenceOneField, SelectField, TextField,
1616
// Inputs
17-
BooleanInput, DateInput, DateTimeInput, NumberInput, SelectInput, TextInput, TimeInput,
18-
ReferenceInput as _ReferenceInput,
17+
BooleanInput, DateInput, DateTimeInput, NumberInput, SelectInput, TextInput,
18+
TimeInput as _TimeInput, ReferenceInput as _ReferenceInput,
1919
// Filters
2020
email, maxLength, maxValue, minLength, minValue, regex, required,
2121
// Misc
2222
AutocompleteInput, EditButton, HttpError, WithRecord
2323
} from "react-admin";
2424
import VisibilityOffIcon from "@mui/icons-material/VisibilityOff";
2525

26+
// Hacked TimeField/TimeInput to actually work with times.
27+
// TODO: Replace once new components are introduced using Temporal API.
28+
29+
const TimeField = (props) => (
30+
<WithRecord {...props} render={
31+
(record) => <DateField {...props} showDate={false} showTime={true}
32+
record={{...record, [props["source"]]: record[props["source"]] === null ? null : "2020-01-01T" + record[props["source"]]}} />
33+
} />
34+
);
35+
36+
const TimeInput = (props) => (<_TimeInput format={(v) => v} parse={(v) => v} {...props} />);
37+
2638
/** Reconfigure ReferenceInput to filter by the displayed repr field. */
2739
const ReferenceInput = (props) => {
2840
const ref = props["reference"];
@@ -48,7 +60,7 @@ const COMPONENTS = {
4860
Datagrid, DatagridSingle,
4961

5062
BooleanField, DateField, NumberField, ReferenceField, ReferenceManyField,
51-
ReferenceOneField, SelectField, TextField,
63+
ReferenceOneField, SelectField, TextField, TimeField,
5264

5365
BooleanInput, DateInput, DateTimeInput, NumberInput, ReferenceInput, SelectInput,
5466
TextInput, TimeInput

aiohttp_admin/backends/abc.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@
2828

2929
class Encoder(json.JSONEncoder):
3030
def default(self, o: object) -> Any:
31-
if isinstance(o, date):
31+
if isinstance(o, (date, time)):
3232
return str(o)
33-
if isinstance(o, time):
34-
# React-admin needs a datetime to display only a time...
35-
return f"2000-01-01T{o}"
3633
if isinstance(o, Enum):
3734
return o.value
3835

aiohttp_admin/backends/sqlalchemy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
sa.Integer: ("NumberField", "NumberInput", MPT({})),
3939
sa.Numeric: ("NumberField", "NumberInput", MPT({})),
4040
sa.String: ("TextField", "TextInput", MPT({})),
41-
sa.Time: ("DateField", "TimeInput", MPT({"showDate": False, "showTime": True})),
41+
sa.Time: ("TimeField", "TimeInput", MPT({})),
4242
sa.Uuid: ("TextField", "TextInput", MPT({})), # TODO: validators
4343
# TODO: Set fields for below types.
4444
# sa.sql.sqltypes._AbstractInterval: (),

0 commit comments

Comments
 (0)