Skip to content

Commit fe61f90

Browse files
committed
feat: better number formatting
1 parent da99a3a commit fe61f90

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

packages/keybr-intl/lib/numbers.ts

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ const factory = (intl: IntlShape): IntlNumbers => {
4444
style: "percent",
4545
maximumFractionDigits: opts,
4646
};
47+
} else if (opts.style !== "percent") {
48+
opts = {
49+
...opts,
50+
style: "percent",
51+
};
4752
}
4853
// Do not round up to 100% if the value is less than one.
4954
// Add decimal digits instead.

packages/page-typing-test/lib/components/ReplayProgress.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useIntlNumbers } from "@keybr/intl";
12
import { useFormatter } from "@keybr/lesson-ui";
23
import {
34
AnimationFrames,
@@ -9,19 +10,23 @@ import { useEffect, useState } from "react";
910
import { type ReplayState } from "../session/index.ts";
1011

1112
export function ReplayProgress({ stepper }: { readonly stepper: ReplayState }) {
13+
const { formatInteger } = useIntlNumbers();
1214
const { formatSpeed } = useFormatter();
13-
const { progress, time } = useReplayProgress(stepper);
15+
const {
16+
progress: { progress, length, speed },
17+
time,
18+
} = useReplayProgress(stepper);
1419
return (
1520
<Para align="center">
1621
<NameValue
1722
name="Progress"
18-
value={`${progress.progress}/${progress.length}`}
23+
value={`${formatInteger(progress)}/${formatInteger(length)}`}
1924
/>
2025
<NameValue
2126
name="Time"
2227
value={formatDuration(time, { showMillis: true })}
2328
/>
24-
<NameValue name="Speed" value={formatSpeed(progress.speed)} />
29+
<NameValue name="Speed" value={formatSpeed(speed)} />
2530
</Para>
2631
);
2732
}

packages/page-typing-test/lib/components/TestProgress.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const TestProgress0 = memo(function TestProgress({
1010
}: {
1111
readonly progress: Progress;
1212
}) {
13-
const { formatNumber, formatPercents } = useIntlNumbers();
13+
const { formatInteger, formatPercents } = useIntlNumbers();
1414
const { formatSpeed } = useFormatter();
1515
return (
1616
<Para className={styles.root}>
@@ -21,9 +21,14 @@ export const TestProgress0 = memo(function TestProgress({
2121
<div className={styles.info}>
2222
<Value value={formatDuration(time, { showMillis: true })} />
2323
{" / "}
24-
<Value value={formatNumber(length)} />
24+
<Value value={formatInteger(length)} />
2525
{" / "}
26-
<Value value={formatPercents(progress)} />
26+
<Value
27+
value={formatPercents(progress, {
28+
minimumFractionDigits: 2,
29+
maximumFractionDigits: 2,
30+
})}
31+
/>
2732
{" / "}
2833
<Value value={formatSpeed(speed)} />
2934
</div>

0 commit comments

Comments
 (0)