Skip to content

Commit

Permalink
add a context provider for specifying options
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Feb 23, 2024
1 parent b7b759c commit 1b47af3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Support old versions of react (pre v16.14)
- Prefer idiomatic phrasing by default (use `numeric: 'always'` for the old behaviour)
- Added a context provider to allow default options to be specified

## 2.0.0 (2024-02-09)

Expand Down
37 changes: 36 additions & 1 deletion src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { assert, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import TimeAgo, { TimeAgoProps } from "../index.js";
import TimeAgo, {
type TimeAgoOptions,
type TimeAgoProps,
TimeAgoProvider,
} from "../index.js";

function setup(props: TimeAgoProps) {
render(
Expand Down Expand Up @@ -132,3 +136,34 @@ describe("TimeAgo", () => {
);
});
});

describe("TimeAgoProvider", () => {
function setupWithProvider(
props: TimeAgoProps,
providerProps: TimeAgoOptions
) {
render(
<TimeAgoProvider {...providerProps}>
<div role="main">
<TimeAgo {...props} />
</div>
</TimeAgoProvider>
);
return screen.getByRole("main");
}

it("uses options from the context provider", () => {
expect(
setupWithProvider({ date: "2023" }, { locale: "ru-Cyrl-RU" })
).toHaveTextContent("5 屑械褋褟褑械胁 薪邪蟹邪写");
});

it("prefers props over values from the context provider", () => {
expect(
setupWithProvider(
{ date: "2023", locale: "it-CH" },
{ locale: "ru-Cyrl-RU" }
)
).toHaveTextContent("5 mesi fa");
});
});
38 changes: 30 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
PropsWithChildren,
createContext,
createElement,
memo,
useCallback,
useContext,
useEffect,
useMemo,
useState,
Expand Down Expand Up @@ -63,15 +66,34 @@ export type TimeAgoProps = {
timeElement?: boolean;
};

export type TimeAgoOptions = Omit<TimeAgoProps, "date">;

const Context = createContext<TimeAgoOptions | undefined>(undefined);

/**
* This context provider allows you specify defaults for
* all options.
*/
export const TimeAgoProvider: React.FC<TimeAgoOptions & PropsWithChildren> = ({
children,
...props
}) => createElement(Context.Provider, { value: props }, children);

const TimeAgo = memo<TimeAgoProps>(
({
date,
locale = navigator.language,
formatOptions,
hideSeconds = true,
roundStrategy = "round",
timeElement = true,
}) => {
//
(props) => {
const {
date,
locale = navigator.language,
formatOptions,
hideSeconds = true,
roundStrategy = "round",
timeElement = true,
} = {
...useContext(Context),
...props,
};

const [text, setText] = useState("");
const [unit, setUnit] = useState<Unit>();

Expand Down

0 comments on commit 1b47af3

Please sign in to comment.