This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datascrollercontext): add ability to use context instead of rowdata
- Loading branch information
Showing
8 changed files
with
310 additions
and
4 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/components/DataScrollerContext/DataScrollerContext.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import React from 'react'; | ||
import { DataScrollerContextProvider, getRowData } from './DataScrollerContext'; | ||
import { render, cleanup } from 'react-testing-library'; | ||
|
||
beforeEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
describe('getRowData', () => { | ||
it('get the data correctly', () => { | ||
const data = { | ||
foo: 'foo', | ||
bar: 'bar', | ||
bam: 'bam', | ||
}; | ||
|
||
const Child = (props: any) => { | ||
return ( | ||
<div> | ||
{Object.entries(props.data).map(([key, value]) => ( | ||
<div key={key}>{value}</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
const TestChild = getRowData()(Child); | ||
|
||
const TestWrapper = () => ( | ||
<DataScrollerContextProvider data={data}> | ||
<TestChild /> | ||
</DataScrollerContextProvider> | ||
); | ||
|
||
const { getByText } = render(<TestWrapper />); | ||
|
||
expect(getByText('foo')).toBeTruthy(); | ||
expect(getByText('bar')).toBeTruthy(); | ||
expect(getByText('bam')).toBeTruthy(); | ||
}); | ||
it('gets only the requested correctly', () => { | ||
const data = { | ||
foo: 'foo', | ||
bar: 'bar', | ||
bam: 'bam', | ||
}; | ||
|
||
const Child = (props: any) => { | ||
return ( | ||
<div> | ||
{Object.entries(props).map(([key, value]) => ( | ||
<div key={key}>{value}</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
const TestChild = getRowData((_, data: any) => ({ | ||
bar: data.bar, | ||
}))(Child); | ||
|
||
const TestWrapper = () => ( | ||
<DataScrollerContextProvider data={data}> | ||
<TestChild /> | ||
</DataScrollerContextProvider> | ||
); | ||
|
||
const { getByText } = render(<TestWrapper />); | ||
|
||
expect(getByText('bar')).toBeTruthy(); | ||
expect(() => getByText('foo')).toThrow(); | ||
expect(() => getByText('bam')).toThrow(); | ||
}); | ||
|
||
it('passes the props correctly', () => { | ||
const data = { | ||
bam: 'bam', | ||
}; | ||
|
||
const Child = (props: any) => { | ||
return ( | ||
<div> | ||
{Object.entries(props).map(([key, value]) => ( | ||
<div key={key}>{value}</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
const TestChild = getRowData((props: any, data: any) => { | ||
return { | ||
...props, | ||
bam: data.bam, | ||
}; | ||
})(Child); | ||
|
||
const TestWrapper = () => ( | ||
<DataScrollerContextProvider data={data}> | ||
<TestChild foo="foo" bar="bar" /> | ||
</DataScrollerContextProvider> | ||
); | ||
|
||
const { getByText } = render(<TestWrapper />); | ||
|
||
expect(getByText('foo')).toBeTruthy(); | ||
expect(getByText('bar')).toBeTruthy(); | ||
expect(getByText('bam')).toBeTruthy(); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
src/components/DataScrollerContext/DataScrollerContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { useContext } from 'react'; | ||
|
||
type DataScrollerContextValue = { | ||
data: any; | ||
}; | ||
|
||
export const DataScrollerContext = React.createContext< | ||
DataScrollerContextValue | ||
>({ | ||
data: {}, | ||
}); | ||
|
||
type ComponentProps<Data = any> = { | ||
data: Data; | ||
children: React.ReactNode; | ||
}; | ||
|
||
export function DataScrollerContextProvider<Data>(props: ComponentProps<Data>) { | ||
return ( | ||
<DataScrollerContext.Provider value={{ data: props.data }}> | ||
{props.children} | ||
</DataScrollerContext.Provider> | ||
); | ||
} | ||
|
||
function defaultMapContextDataValueToProps<Props, Data>( | ||
props: Props, | ||
data: Data, | ||
): any { | ||
return { ...props, data }; | ||
} | ||
|
||
type MapContextValueToProps<OuterProps, Data, InnerProps> = ( | ||
props: OuterProps, | ||
data: Data, | ||
) => InnerProps; | ||
|
||
export function getRowData<OuterProps, Data, InnerProps>( | ||
mapContextValueToProps?: MapContextValueToProps<OuterProps, Data, InnerProps>, | ||
) { | ||
return function(Component: React.FC<InnerProps>) { | ||
const MemoizedComponent = (React.memo(Component) as any) as React.FC< | ||
InnerProps | ||
>; | ||
|
||
return function<P extends OuterProps>(props: P) { | ||
const contextValue = useContext(DataScrollerContext); | ||
|
||
const propsWithState = mapContextValueToProps | ||
? mapContextValueToProps(props, contextValue.data) | ||
: (defaultMapContextDataValueToProps<OuterProps, Data>( | ||
props, | ||
contextValue.data, | ||
) as InnerProps); | ||
|
||
return <MemoizedComponent {...propsWithState} />; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { DataScrollerContextProvider, getRowData } from './DataScrollerContext'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,4 +58,4 @@ function Rows({ | |
); | ||
} | ||
|
||
export default Rows; | ||
export default React.memo(Rows); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters