Skip to content

Commit 4788f96

Browse files
committed
fix: 🏷️ Fix TimeProvider.children type
Adjusts the `children` prop type of `TimeProvider` to accept `ReactNode` in order to work with the default type of `React.FC` components.
1 parent 9ecd80e commit 4788f96

4 files changed

Lines changed: 40 additions & 6 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,15 @@ You can use a `<TimeProvider>` component to use a custom instance of `TimeSync`,
150150
Example:
151151

152152
```js
153+
import { useState } from "react";
153154
import { TimeProvider } from "react-time-sync";
154155
import TimeSync from "time-sync";
155156

156-
const App = ({ content }) => {
157-
const timeSync = new TimeSync();
157+
const App = ({ children }) => {
158+
const [timeSync] = useState(() => new TimeSync());
158159
return (
159160
<div>
160-
<TimeProvider timeSync={timeSync}>{content}</TimeProvider>
161+
<TimeProvider timeSync={timeSync}>{children}</TimeProvider>
161162
</div>
162163
);
163164
};

src/__snapshots__/time-provider.test.tsx.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`#TimeProvider should accept default ReactProps.children type as children 1`] = `
4+
<DocumentFragment>
5+
<div>
6+
Test1
7+
</div>
8+
<div>
9+
Test2
10+
</div>
11+
</DocumentFragment>
12+
`;
13+
314
exports[`#TimeProvider should render a single child 1`] = `
415
<DocumentFragment>
516
<div>

src/time-provider.test.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from "react";
22
import TimeContext from "./context";
33
import TimeProvider from "./time-provider";
4+
import PropTypes from "prop-types";
5+
import TimeSync from "time-sync";
46
import { render, cleanup } from "@testing-library/react";
57

68
declare const require: any;
@@ -127,13 +129,33 @@ describe("#TimeProvider", () => {
127129
);
128130
}
129131

132+
const timeSync = new TimeSync();
130133
const { unmount } = render(
131-
<TimeProvider timeSync={new TimeSync()}>
134+
<TimeProvider timeSync={timeSync}>
132135
<ContextConsumer />
133136
</TimeProvider>
134137
);
135138
unmount();
136139

137140
expect(addTimer).toHaveBeenCalledTimes(1);
138141
});
142+
143+
it("should accept default ReactProps.children type as children", () => {
144+
const timeSync = new TimeSync();
145+
146+
const ExampleWrapper: React.FC = ({ children }) => {
147+
return <TimeProvider timeSync={timeSync}>{children}</TimeProvider>;
148+
};
149+
ExampleWrapper.propTypes = {
150+
children: PropTypes.node,
151+
};
152+
153+
const { asFragment } = render(
154+
<ExampleWrapper>
155+
<div>Test1</div>
156+
<div>Test2</div>
157+
</ExampleWrapper>
158+
);
159+
expect(asFragment()).toMatchSnapshot();
160+
});
139161
});

src/time-provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React, { Component, ReactElement } from "react";
1+
import React, { Component, ReactNode } from "react";
22
import PropTypes from "prop-types";
33
import TimeContext, { TimeSyncContext } from "./context";
44
import TimeSync from "time-sync";
55

66
interface TimeProviderProps {
7-
children: ReactElement | ReactElement[];
7+
children: ReactNode;
88
timeSync: TimeSync;
99
}
1010

0 commit comments

Comments
 (0)