Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shareable container implementation #198

Merged
merged 4 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
// fix for eslint-plugin-flowtype/384 not supporting wildcard
_: 'readonly',
},
plugins: ['react', 'react-hooks', 'import', 'flowtype'],
plugins: ['react', 'react-hooks', 'import'],
rules: {
'no-shadow': ['error'],
indent: ['off'],
Expand Down
37 changes: 37 additions & 0 deletions examples/advanced-shared/components/color.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
createStore,
createHook,
type StoreActionApi,
} from 'react-sweet-state';
import { ThemingContainer } from './theming';

type State = {
color: string;
};

const initialState: State = {
color: 'white',
};

const actions = {
set:
(color: string) =>
({ setState }: StoreActionApi<State>) => {
setState({ color });
},
};

const Store = createStore({
initialState,
actions,
containedBy: ThemingContainer,
handlers: {
onInit:
() =>
({ setState }, { initialData }) => {
if (initialData) setState({ color: initialData.color });
},
},
});

export const useColor = createHook(Store);
6 changes: 6 additions & 0 deletions examples/advanced-shared/components/theming.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createContainer } from 'react-sweet-state';

export type ThemingContainerProps = {
initialData?: { width: number; color: string };
};
export const ThemingContainer = createContainer<ThemingContainerProps>();
37 changes: 37 additions & 0 deletions examples/advanced-shared/components/width.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
createStore,
createHook,
type StoreActionApi,
} from 'react-sweet-state';
import { ThemingContainer } from './theming';

type State = {
width: number;
};

const initialState: State = {
width: 200,
};

const actions = {
set:
(width: number) =>
({ setState }: StoreActionApi<State>) => {
setState({ width });
},
};

const Store = createStore({
initialState,
actions,
containedBy: ThemingContainer,
handlers: {
onInit:
() =>
({ setState }, { initialData }) => {
if (initialData) setState({ width: initialData.width });
},
},
});

export const useWidth = createHook(Store);
23 changes: 23 additions & 0 deletions examples/advanced-shared/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Advanced shared scoped example</title>
<style>
body {
font-family: sans-serif;
}
main {
display: flex;
line-height: 1.5;
}
hr {
margin: 1em;
}
</style>
</head>

<body>
<div id="root"></div>
<script src="./bundle.js" type="text/javascript"></script>
</body>
</html>
56 changes: 56 additions & 0 deletions examples/advanced-shared/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { StrictMode } from 'react';
import ReactDOM from 'react-dom/client';

import { useColor } from './components/color';
import { useWidth } from './components/width';
import { ThemingContainer } from './components/theming';

const colors = ['white', 'aliceblue', 'beige', 'gainsboro', 'honeydew'];
const widths = [200, 220, 240, 260, 280];
const rand = () => Math.floor(Math.random() * colors.length);
const initialData = { color: colors[rand()], width: widths[rand()] };

/**
* Components
*/
const ThemeHook = ({ title }: { title: string }) => {
const [{ color }, { set: setColor }] = useColor();
const [{ width }, { set: setWidth }] = useWidth();

return (
<div style={{ background: color, width }}>
<h3>Component {title}</h3>
<p>Color: {color}</p>
<p>Width: {width}</p>
<button onClick={() => setColor(colors[rand()])}>Change color</button>
<button onClick={() => setWidth(widths[rand()])}>Change width</button>
</div>
);
};

/**
* Main App
*/
const App = () => (
<div>
<h1>Advanced dynamic scoped example</h1>
<main>
<ThemingContainer scope="t1">
<ThemeHook title="scope" />
</ThemingContainer>
<ThemingContainer initialData={initialData}>
<ThemeHook title="local" />
</ThemingContainer>
<ThemingContainer scope="t1">
<ThemeHook title="scope sync" />
</ThemingContainer>
</main>
</div>
);

const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(
<StrictMode>
<App />
</StrictMode>
);
40 changes: 0 additions & 40 deletions examples/basic-flow/components.js

This file was deleted.

18 changes: 0 additions & 18 deletions examples/basic-flow/index.html

This file was deleted.

34 changes: 0 additions & 34 deletions examples/basic-flow/index.js

This file was deleted.

1 change: 0 additions & 1 deletion examples/basic-ts/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import React, { StrictMode } from 'react';
import ReactDOM from 'react-dom/client';

Expand Down
6 changes: 0 additions & 6 deletions src/__tests__/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,3 @@ export const storeStateMock = {
listeners: () => [],
mutator: () => {},
};

export const registryMock = {
configure: jest.fn(),
getStoreState: jest.fn(),
deleteStoreState: jest.fn(),
};
45 changes: 45 additions & 0 deletions src/__tests__/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,48 @@ Test = (
bla
</TypePropsContainer>
);

/**
* Shared Container types tests
*/

type SharedContainerProps = {|
initValue?: number,
|};
const TypeSharedContainer = createContainer<SharedContainerProps>();

Test = (
// $FlowExpectedError[incompatible-type]
<TypeSharedContainer>{({ count }) => count}</TypeSharedContainer>
);

Test = (
// $FlowExpectedError[prop-missing]
<TypeSharedContainer foo="1">bla</TypeSharedContainer>
);

// Correct
Test = <TypeSharedContainer>bla</TypeSharedContainer>;
Test = <TypeSharedContainer scope="a">bla</TypeSharedContainer>;
Test = <TypeSharedContainer isGlobal>bla</TypeSharedContainer>;
Test = <TypeSharedContainer initValue={1}>bla</TypeSharedContainer>;

createStore<State, Actions, SharedContainerProps>({
initialState: { count: 0 },
actions: {},
containedBy: TypeSharedContainer,
handlers: {
onInit:
() =>
({ setState }, { initValue }) => {
if (initValue) {
// $FlowExpectedError[prop-missing]
initValue.split;
setState({ count: initValue });
}
},
onUpdate: () => () => undefined,
onDestroy: () => () => undefined,
onContainerUpdate: () => () => undefined,
},
});
2 changes: 2 additions & 0 deletions src/components/__tests__/container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { createSubscriber } from '../subscriber';
const mockLocalRegistry = {
configure: jest.fn(),
getStore: jest.fn(),
hasStore: jest.fn(),
deleteStore: jest.fn(),
};

Expand All @@ -21,6 +22,7 @@ jest.mock('../../store/registry', () => ({
defaultRegistry: {
configure: jest.fn(),
getStore: jest.fn(),
hasStore: jest.fn(),
deleteStore: jest.fn(),
},
}));
Expand Down
Loading