Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add date formatting option in custom api #2228

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/widgets/services/customapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ widget:
another: key3
label: Field 3
format: percent # optional - defaults to text
- field: key # needs to be YAML string or object
label: Field 4
format: date # optional - defaults to text
dateStyle: long # optional - defaults to "long". Allowed values: `["full", "long", "medium", "short"]`.
```

Supported formats for the values are `text`, `number`, `float`, `percent`, `bytes` and `bitrate`.
Supported formats for the values are `text`, `number`, `float`, `percent`, `bytes`, `bitrate` and `date`.

## Example

Expand Down
3 changes: 3 additions & 0 deletions next-i18next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ module.exports = {
i18next.services.formatter.add("percent", (value, lng, options) =>
new Intl.NumberFormat(lng, { style: "percent", ...options }).format(parseFloat(value) / 100.0),
);
i18next.services.formatter.add("date", (value, lng, options) =>
new Intl.DateTimeFormat(lng, { ...options }).format(new Date(value)),
);
},
type: "3rdParty",
},
Expand Down
3 changes: 2 additions & 1 deletion public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"bibitrate": "{{value, rate(bits: true; binary: true)}}",
"percent": "{{value, percent}}",
"number": "{{value, number}}",
"ms": "{{value, number}}"
"ms": "{{value, number}}",
"date": "{{value, date}}"
},
"widget": {
"missing_type": "Missing Widget Type: {{type}}",
Expand Down
3 changes: 3 additions & 0 deletions src/widgets/customapi/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ function formatValue(t, mapping, rawValue) {
case "bitrate":
value = t("common.bitrate", { value });
break;
case "date":
value = t("common.date", { value, dateStyle: mapping?.dateStyle ?? "long" });
break;
case "text":
default:
// nothing
Expand Down