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

Commit 94bfde6

Browse files
committed
dinamic value component in function of type of field. Calendar with autocomplete
1 parent 4a8412a commit 94bfde6

File tree

6 files changed

+119
-32
lines changed

6 files changed

+119
-32
lines changed

src/components/img/MetadataTags.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export default function MetadataTagsIcon() {
66
width="24"
77
height="24"
88
viewBox="0 0 24 24"
9-
stroke-width="2"
9+
strokeWidth="2"
1010
stroke="currentColor"
1111
fill="none"
12-
stroke-linecap="round"
13-
stroke-linejoin="round"
12+
strokeLinecap="round"
13+
strokeLinejoin="round"
1414
>
1515
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
1616
<path d="M13 7a2 2 0 0 1 2 2v12l-5 -3l-5 3v-12a2 2 0 0 1 2 -2h6z"></path>

src/components/modals/newColumn/handlers/MetadataToggleGroupHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class MetadataToggleGroupHandler extends AbstractHandlerClass<AddColumnMo
2727
.setName(t("settings_metatata_file_toggle_title"))
2828
.setDesc(t("settings_metatata_file_toggle_desc"))
2929
.addToggle(toggle =>
30-
toggle.setValue(view.diskConfig.yaml.columns.__file__.isHidden)
30+
toggle.setValue(!view.diskConfig.yaml.columns.__file__.isHidden)
3131
.onChange(metadata_file_toggle_promise)
3232
);
3333

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
import Autocomplete from "@mui/material/Autocomplete";
12
import Input from "@mui/material/Input";
2-
import React, { useState } from "react";
3+
import TextField from "@mui/material/TextField";
4+
import { InputType, StyleVariables } from "helpers/Constants";
5+
import React, { ChangeEvent, useState } from "react";
6+
import { valueToCalendarConverter } from "../mappers/FilterValuesMapper";
37

48
const ValueFilterComponent = (props: {
59
handler: (event: React.ChangeEvent<HTMLInputElement>) => void;
610
value: string;
711
type: string;
812
}) => {
9-
const [value, setValue] = useState(props.value);
13+
const { type, value } = props;
14+
const [valueState, setValueSate] = useState(value);
1015
const [valueTimeout, setValueTimeout] = useState(null);
1116
const proxyHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
1217
if (valueTimeout) {
1318
clearTimeout(valueTimeout);
1419
}
1520
// first update the input text as user type
16-
setValue(event.target.value);
21+
setValueSate(event.target.value);
1722
// initialize a setimeout by wrapping in our editNoteTimeout so that we can clear it out using clearTimeout
1823
setValueTimeout(
1924
setTimeout(() => {
@@ -22,15 +27,89 @@ const ValueFilterComponent = (props: {
2227
}, 1500)
2328
);
2429
};
30+
switch (type) {
31+
case InputType.CALENDAR:
32+
case InputType.CALENDAR_TIME:
33+
return (
34+
<AutocompleteFrom
35+
value={valueState}
36+
handler={proxyHandler}
37+
options={Array.from(valueToCalendarConverter.keys()).map(
38+
(key) => `@${key}`
39+
)}
40+
/>
41+
);
42+
case InputType.NUMBER:
43+
return (
44+
<InputForm value={valueState} handler={proxyHandler} type="number" />
45+
);
46+
default:
47+
return <InputForm value={valueState} handler={proxyHandler} />;
48+
}
49+
};
2550

51+
function InputForm(props: {
52+
handler: (event: React.ChangeEvent<HTMLInputElement>) => void;
53+
value: string;
54+
type?: string;
55+
}) {
56+
const { value, handler, type } = props;
2657
return (
2758
<Input
28-
type="text"
59+
type={type !== undefined ? type : "text"}
2960
className="form-control"
3061
value={value}
31-
onChange={proxyHandler}
62+
onChange={handler}
3263
/>
3364
);
34-
};
65+
}
66+
67+
function AutocompleteFrom(props: {
68+
handler: (event: React.ChangeEvent<HTMLInputElement>) => void;
69+
value: string;
70+
options: string[];
71+
}) {
72+
const { value, handler, options } = props;
73+
const [valueState, setValueSate] = useState(value);
74+
const [inputValue, setInputValue] = useState(value);
75+
return (
76+
<Autocomplete
77+
freeSolo
78+
disableClearable
79+
options={options}
80+
value={valueState}
81+
onChange={(event: any, newValue: string | null) => {
82+
setValueSate(newValue);
83+
handler({
84+
target: { value: newValue },
85+
} as ChangeEvent<HTMLInputElement>);
86+
}}
87+
inputValue={inputValue}
88+
onInputChange={(event, newInputValue) => {
89+
setInputValue(newInputValue);
90+
}}
91+
componentsProps={{
92+
paper: {
93+
sx: {
94+
width: "max-content",
95+
backgroundColor: StyleVariables.BACKGROUND_PRIMARY,
96+
color: StyleVariables.TEXT_NORMAL,
97+
},
98+
},
99+
}}
100+
renderInput={(params) => (
101+
<TextField
102+
{...params}
103+
className="form-control"
104+
InputProps={{
105+
...params.InputProps,
106+
type: "search",
107+
}}
108+
variant="standard"
109+
/>
110+
)}
111+
/>
112+
);
113+
}
35114

36115
export default ValueFilterComponent;

src/lib/features/filters/domain/ValidateFilter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Literal } from "obsidian-dataview";
2-
import { DateTime } from "luxon";
2+
import { DateTime, DurationUnit } from "luxon";
33
import { AtomicFilter, FilterGroup, FilterGroupCondition } from "../model/FiltersModel";
44
import { LocalSettings } from "cdm/SettingsModel";
55
import { ParseService } from "services/ParseService";
@@ -84,7 +84,7 @@ class ValidateFilter {
8484
return false;
8585
}
8686
const mappedValue = FilterValuesMapper.toCalendarValue(value);
87-
const diffMinutes = literalToCheck.diff(mappedValue, 'minute').minutes;
87+
const diffMinutes = literalToCheck.diff(mappedValue.date, mappedValue.unit).as(`${mappedValue.unit}s` as DurationUnit);
8888
switch (operator) {
8989
case OperatorFilter.EQUAL[1]:
9090
return diffMinutes === 0;
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import { DateTime } from "luxon";
2-
const valueToCalendarConverter = new Map<string, DateTime>([
3-
["today", DateTime.local().startOf("day")],
4-
["yesterday", DateTime.local().minus({ days: 1 }).startOf("day")],
5-
["tomorrow", DateTime.local().plus({ days: 1 }).startOf("day")],
6-
["thisweek", DateTime.local().startOf("week")],
7-
["lastweek", DateTime.local().minus({ weeks: 1 }).startOf("week")],
8-
["nextweek", DateTime.local().plus({ weeks: 1 }).startOf("week")],
9-
["thismonth", DateTime.local().startOf("month")],
10-
["lastmonth", DateTime.local().minus({ months: 1 }).startOf("month")],
11-
["nextmonth", DateTime.local().plus({ months: 1 }).startOf("month")],
12-
["thisyear", DateTime.local().startOf("year")],
13-
["lastyear", DateTime.local().minus({ years: 1 }).startOf("year")],
14-
["nextyear", DateTime.local().plus({ years: 1 }).startOf("year")],
15-
["thisquarter", DateTime.local().startOf("quarter")],
16-
["lastquarter", DateTime.local().minus({ quarters: 1 }).startOf("quarter")],
17-
["nextquarter", DateTime.local().plus({ quarters: 1 }).startOf("quarter")],
2+
import { FilterCalendarResponse } from "../model/FiltersModel";
3+
export const valueToCalendarConverter = new Map<string, FilterCalendarResponse>([
4+
["today", { unit: "day", date: DateTime.local().startOf("day") }],
5+
["yesterday", { unit: "day", date: DateTime.local().minus({ days: 1 }).startOf("day") }],
6+
["tomorrow", { unit: "day", date: DateTime.local().plus({ days: 1 }).startOf("day") }],
7+
["thisweek", { unit: "week", date: DateTime.local().startOf("week") }],
8+
["lastweek", { unit: "week", date: DateTime.local().minus({ weeks: 1 }).startOf("week") }],
9+
["nextweek", { unit: "week", date: DateTime.local().plus({ weeks: 1 }).startOf("week") }],
10+
["thismonth", { unit: "month", date: DateTime.local().startOf("month") }],
11+
["lastmonth", { unit: "month", date: DateTime.local().minus({ months: 1 }).startOf("month") }],
12+
["nextmonth", { unit: "month", date: DateTime.local().plus({ months: 1 }).startOf("month") }],
13+
["thisyear", { unit: "year", date: DateTime.local().startOf("year") }],
14+
["lastyear", { unit: "year", date: DateTime.local().minus({ years: 1 }).startOf("year") }],
15+
["nextyear", { unit: "year", date: DateTime.local().plus({ years: 1 }).startOf("year") }],
16+
["thisquarter", { unit: "quarter", date: DateTime.local().startOf("quarter") }],
17+
["lastquarter", { unit: "quarter", date: DateTime.local().minus({ quarters: 1 }).startOf("quarter") }],
18+
["nextquarter", { unit: "quarter", date: DateTime.local().plus({ quarters: 1 }).startOf("quarter") }],
1819
]);
1920

2021
export default class FilterValuesMapper {
21-
public static toCalendarValue(filterValue: string): DateTime {
22+
public static toCalendarValue(filterValue: string): FilterCalendarResponse {
2223
if (filterValue.startsWith("@")) {
2324
const potentialDate = valueToCalendarConverter.get(filterValue.slice(1).toLowerCase());
24-
return potentialDate ? potentialDate : DateTime.local();
25+
return potentialDate ? potentialDate : valueToCalendarConverter.get("today");
2526
}
26-
2727
const potentialDate = DateTime.fromISO(filterValue);
28-
return potentialDate.isValid ? potentialDate : DateTime.local();
28+
return { unit: "day", date: potentialDate.isValid ? potentialDate : DateTime.local() };
2929
}
3030
}

src/lib/features/filters/model/FiltersModel.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Table } from "@tanstack/react-table";
22
import { RowDataType } from "cdm/FolderModel";
33
import { FiltersModalManager } from "../domain/FiltersModal";
44
import { BaseModalHandlerResponse } from "cdm/ModalsModel";
5+
import { DateTime, DurationUnits } from "luxon";
56

67
export type FilterGroup = AtomicFilter | FilterGroupCondition;
78

@@ -51,6 +52,13 @@ export type GroupFilterComponentProps = {
5152
possibleColumns: ColumnFilterOption[];
5253
};
5354

55+
/***************************************
56+
* FILTERS MAPPERS
57+
***************************************/
58+
export type FilterCalendarResponse = {
59+
unit: DurationUnits;
60+
date: DateTime;
61+
}
5462
/***************************************
5563
* FILTERS MODAL
5664
***************************************/

0 commit comments

Comments
 (0)