Skip to content

Commit

Permalink
fix(react): add PropsWithChildren
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuoushub committed Jul 7, 2022
1 parent bc19253 commit a673b4f
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/auth/src/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { PropsWithChildren } from 'react'

import { createAuthClient } from './authClients'
import type {
Expand Down Expand Up @@ -114,7 +114,7 @@ type AuthProviderState = {
* ```
*/
export class AuthProvider extends React.Component<
AuthProviderProps,
PropsWithChildren<AuthProviderProps>,
AuthProviderState
> {
static defaultProps = {
Expand Down
9 changes: 7 additions & 2 deletions packages/router/src/active-route-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { useRef, useState, useEffect } from 'react'
import React, {
useRef,
useState,
useEffect,
PropsWithChildren,
} from 'react'

import { getAnnouncement, getFocus, resetFocus } from './a11yUtils'
import {
Expand Down Expand Up @@ -34,7 +39,7 @@ export const ActiveRouteLoader = ({
params,
whileLoadingPage,
children,
}: Props) => {
}: PropsWithChildren<Props>) => {
const location = useLocation()
const [pageName, setPageName] = useState('')
const loadingTimeout = useRef<NodeJS.Timeout>()
Expand Down
6 changes: 4 additions & 2 deletions packages/router/src/location.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { PropsWithChildren } from 'react'

import { gHistory } from './history'
import { createNamedContext, TrailingSlashesTypes } from './util'
Expand All @@ -20,7 +20,9 @@ interface LocationProviderProps {
trailingSlashes?: TrailingSlashesTypes
}

class LocationProvider extends React.Component<LocationProviderProps> {
class LocationProvider extends React.Component<
PropsWithChildren<LocationProviderProps>
> {
// When prerendering, there might be more than one level of location
// providers. Use the values from the one above.
static contextType = LocationContext
Expand Down
4 changes: 2 additions & 2 deletions packages/router/src/params.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from 'react'
import React, { PropsWithChildren, useContext } from 'react'

import { LocationContextType, useLocation } from './location'
import { useRouterState } from './router-context'
Expand All @@ -15,7 +15,7 @@ interface Props {
location?: LocationContextType
}

export const ParamsProvider: React.FC<Props> = ({
export const ParamsProvider: React.FC<PropsWithChildren<Props>> = ({
path,
location,
children,
Expand Down
15 changes: 9 additions & 6 deletions packages/router/src/router-context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { useReducer, createContext, useContext } from 'react'
import React, {
useReducer,
createContext,
useContext,
PropsWithChildren,
} from 'react'

import { useAuth } from '@redwoodjs/auth'

Expand Down Expand Up @@ -28,11 +33,9 @@ function stateReducer(state: RouterState, newState: Partial<RouterState>) {
return { ...state, ...newState }
}

export const RouterContextProvider: React.FC<RouterContextProviderProps> = ({
useAuth: customUseAuth,
paramTypes,
children,
}) => {
export const RouterContextProvider: React.FC<
PropsWithChildren<RouterContextProviderProps>
> = ({ useAuth: customUseAuth, paramTypes, children }) => {
const [state, setState] = useReducer(stateReducer, {
useAuth: customUseAuth || useAuth,
paramTypes,
Expand Down
6 changes: 3 additions & 3 deletions packages/router/src/router.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { PropsWithChildren } from 'react'

import { ActiveRouteLoader } from './active-route-loader'
import { useActivePageContext } from './ActivePageContext'
Expand Down Expand Up @@ -128,7 +128,7 @@ interface RouterProps extends RouterContextProviderProps {
pageLoadingDelay?: number
}

const Router: React.FC<RouterProps> = ({
const Router: React.FC<PropsWithChildren<RouterProps>> = ({
useAuth,
paramTypes,
pageLoadingDelay,
Expand All @@ -147,7 +147,7 @@ const Router: React.FC<RouterProps> = ({
</LocationProvider>
)

const LocationAwareRouter: React.FC<RouterProps> = ({
const LocationAwareRouter: React.FC<PropsWithChildren<RouterProps>> = ({
useAuth,
paramTypes,
pageLoadingDelay,
Expand Down
6 changes: 4 additions & 2 deletions packages/testing/src/web/MockParamsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react'
import React, { PropsWithChildren } from 'react'

import { useLocation, ParamsContext, parseSearch } from '@redwoodjs/router'

interface Props {
path?: string
}

export const MockParamsProvider: React.FC<Props> = ({ children }) => {
export const MockParamsProvider: React.FC<PropsWithChildren<Props>> = ({
children,
}) => {
const location = useLocation()

const searchParams = parseSearch(location.search)
Expand Down
6 changes: 4 additions & 2 deletions packages/testing/src/web/MockProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* NOTE: This module should not contain any nodejs functionality,
* because it's also used by Storybook in the browser.
*/
import React from 'react'
import React, { PropsWithChildren } from 'react'

import type { AuthContextInterface } from '@redwoodjs/auth'
import { AuthProvider } from '@redwoodjs/auth'
Expand Down Expand Up @@ -55,7 +55,9 @@ export const mockAuthClient = {
type: 'custom',
}

export const MockProviders: React.FunctionComponent = ({ children }) => {
export const MockProviders: React.FunctionComponent<PropsWithChildren> = ({
children,
}) => {
return (
<AuthProvider client={mockAuthClient} type="custom">
<RedwoodProvider titleTemplate="%PageTitle | %AppTitle">
Expand Down
6 changes: 4 additions & 2 deletions packages/testing/src/web/MockRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import React from 'react'
import React, { PropsWithChildren } from 'react'

// Bypass the `main` field in `package.json` because we alias `@redwoodjs/router`
// for jest and Storybook. Not doing so would cause an infinite loop.
Expand All @@ -16,7 +16,9 @@ export const routes: { [routeName: string]: () => string } = {}
* We overwrite the default `Router` export.
* It populates the `routes.<pagename>()` utility object.
*/
export const Router: React.FunctionComponent = ({ children }) => {
export const Router: React.FunctionComponent<PropsWithChildren> = ({
children,
}) => {
const flatChildArray = flattenAll(children)

flatChildArray.forEach((child) => {
Expand Down
34 changes: 21 additions & 13 deletions packages/web/src/apollo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PropsWithChildren } from 'react'

import type {
ApolloClientOptions,
setLogVerbosity,
Expand Down Expand Up @@ -91,13 +93,15 @@ export type GraphQLClientConfigProp = Omit<

export type UseAuthProp = () => AuthContextInterface

const ApolloProviderWithFetchConfig: React.FunctionComponent<{
config: Omit<GraphQLClientConfigProp, 'cacheConfig' | 'cache'> & {
cache: ApolloCache<unknown>
}
useAuth: UseAuthProp
logLevel: F.Return<typeof setLogVerbosity>
}> = ({ config, children, useAuth, logLevel }) => {
const ApolloProviderWithFetchConfig: React.FunctionComponent<
PropsWithChildren<{
config: Omit<GraphQLClientConfigProp, 'cacheConfig' | 'cache'> & {
cache: ApolloCache<unknown>
}
useAuth: UseAuthProp
logLevel: F.Return<typeof setLogVerbosity>
}>
> = ({ config, children, useAuth, logLevel }) => {
/**
* Should they run into it,
* this helps users with the "Cannot render cell; GraphQL success but data is null" error.
Expand Down Expand Up @@ -257,7 +261,9 @@ interface ErrorBoundaryProps {
onError: NonNullable<ComponentDidCatch>
}

class ErrorBoundary extends React.Component<ErrorBoundaryProps> {
class ErrorBoundary extends React.Component<
PropsWithChildren<ErrorBoundaryProps>
> {
componentDidCatch(...args: Parameters<NonNullable<ComponentDidCatch>>) {
this.setState({})
this.props.onError(...args)
Expand All @@ -268,11 +274,13 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps> {
}
}

export const RedwoodApolloProvider: React.FunctionComponent<{
graphQLClientConfig?: GraphQLClientConfigProp
useAuth?: UseAuthProp
logLevel?: F.Return<typeof setLogVerbosity>
}> = ({
export const RedwoodApolloProvider: React.FunctionComponent<
PropsWithChildren<{
graphQLClientConfig?: GraphQLClientConfigProp
useAuth?: UseAuthProp
logLevel?: F.Return<typeof setLogVerbosity>
}>
> = ({
graphQLClientConfig,
useAuth = useRWAuth,
logLevel = 'debug',
Expand Down
10 changes: 7 additions & 3 deletions packages/web/src/components/FetchConfigProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PropsWithChildren } from 'react'

import type { AuthContextInterface, SupportedAuthTypes } from '@redwoodjs/auth'

export const getApiGraphQLUrl = () => {
Expand All @@ -22,9 +24,11 @@ type UseAuthType = () => AuthContextInterface
* Note that the auth bearer token is now passed in packages/web/src/apollo/index.tsx
* as the token is retrieved async
*/
export const FetchConfigProvider: React.FunctionComponent<{
useAuth?: UseAuthType
}> = ({
export const FetchConfigProvider: React.FunctionComponent<
PropsWithChildren<{
useAuth?: UseAuthType
}>
> = ({
useAuth = global.__REDWOOD__USE_AUTH ?? (() => defaultAuthState),
...rest
}) => {
Expand Down

0 comments on commit a673b4f

Please sign in to comment.