Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

feat(context): migrate core [STEP 1] #2178

Merged
merged 28 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7610079
feat(context): migrate core [STEP 1]
Haroenv Mar 18, 2019
ce9d955
chore: remove context cache
Haroenv Mar 18, 2019
9c33619
chore(tslint): add back accidentally removed prettier
Haroenv Mar 18, 2019
57d7406
fix(connector): call gPP with correct "this"
Haroenv Mar 18, 2019
30df9f3
chore(TS): ReactType for Root
Haroenv Mar 18, 2019
b63fff3
test(connector): swap mount for shallow where possible
Haroenv Mar 25, 2019
9389fab
chore(InstantSearch): refactor branch
Haroenv Mar 25, 2019
cf8e92c
test(InstantSearch): avoid mutation
Haroenv Mar 25, 2019
b4d7808
chore(connector): remove unused
Haroenv Mar 25, 2019
b5bfbed
chore(createConnector): warn on creation instead of mount
Haroenv Mar 25, 2019
eac69e0
fix(connector): remove unused variable
Haroenv Mar 25, 2019
b81001d
chore(connector): consistent if
Haroenv Mar 25, 2019
4deb399
chore(store): simplify typing
Haroenv Mar 25, 2019
0377ef4
chore(stories): better display name
Haroenv Mar 25, 2019
632e6ea
chore(TS): fix typing of results
Haroenv Mar 26, 2019
9ae17c2
chore: remove useless comments
Haroenv Mar 26, 2019
eed09d2
consistent naming
Haroenv Mar 26, 2019
f88284c
fix(InstantSearch): update index name when it changes
Haroenv Mar 26, 2019
72f5750
prettier
Haroenv Mar 26, 2019
c141197
chore: remove old comment
Haroenv Apr 8, 2019
24dd2f5
chore: fix merge
Haroenv Apr 9, 2019
a7b317e
ci: remove no-empty rule
Haroenv Apr 9, 2019
50bc90c
fix: make TS work in new connectors
Haroenv Apr 9, 2019
db9e3ca
chore: fix bundlesize
Haroenv Apr 9, 2019
6445075
feedback from review (remove comments & warning)
Haroenv Apr 12, 2019
daa6016
more review comments
Haroenv Apr 12, 2019
04e70fa
Update packages/react-instantsearch-core/src/components/InstantSearch…
samouss Apr 13, 2019
508c033
feat(context): migrate base connector [STEP 2] (#2179)
Haroenv Apr 15, 2019
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,19 @@
},
{
"path": "packages/react-instantsearch/dist/umd/Connectors.min.js",
"maxSize": "40.50 kB"
"maxSize": "41 kB"
},
{
"path": "packages/react-instantsearch/dist/umd/Dom.min.js",
"maxSize": "63.75 kB"
},
{
"path": "packages/react-instantsearch-core/dist/umd/ReactInstantSearchCore.min.js",
"maxSize": "41.50 kB"
"maxSize": "42 kB"
},
{
"path": "packages/react-instantsearch-dom/dist/umd/ReactInstantSearchDOM.min.js",
"maxSize": "63.75 kB"
"maxSize": "64.25 kB"
},
{
"path": "packages/react-instantsearch-dom-maps/dist/umd/ReactInstantSearchDOMMaps.min.js",
Expand Down
111 changes: 0 additions & 111 deletions packages/react-instantsearch-core/src/components/Index.js

This file was deleted.

145 changes: 145 additions & 0 deletions packages/react-instantsearch-core/src/components/Index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import React, { Component, Children, ReactType } from 'react';
import PropTypes from 'prop-types';
import {
InstantSearchConsumer,
InstantSearchContext,
IndexProvider,
IndexContext,
} from '../core/context';

type Props = {
indexName: string;
indexId: string;
root: {
Root: ReactType;
props: {};
};
};

type InnerProps = Props & { contextValue: InstantSearchContext };

type State = {
indexContext: IndexContext;
};

/**
* @description
* `<Index>` is the component that allows you to apply widgets to a dedicated index. It's
* useful if you want to build an interface that targets multiple indices.
* @kind widget
* @name <Index>
* @propType {string} indexName - index in which to search.
* @propType {{ Root: string|function, props: object }} [root] - Use this to customize the root element. Default value: `{ Root: 'div' }`
* @example
* import React from 'react';
* import { InstantSearch, Index, SearchBox, Hits, Configure } from 'react-instantsearch-dom';
*
* const App = () => (
* <InstantSearch
* appId="latency"
* apiKey="6be0576ff61c053d5f9a3225e2a90f76"
* indexName="instant_search"
* >
* <Configure hitsPerPage={5} />
* <SearchBox />
* <Index indexName="instant_search">
* <Hits />
* </Index>
* <Index indexName="bestbuy">
* <Hits />
* </Index>
* </InstantSearch>
* );
*/
class Index extends Component<InnerProps, State> {
static propTypes = {
// @TODO: These props are currently constant.
indexName: PropTypes.string.isRequired,
indexId: PropTypes.string.isRequired,
children: PropTypes.node,
root: PropTypes.shape({
Root: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.object,
]),
props: PropTypes.object,
}).isRequired,
};

unregisterWidget?: () => void;

state = {
indexContext: {
targetedIndex: this.props.indexId,
},
};

constructor(props: InnerProps) {
super(props);

this.props.contextValue.onSearchParameters(
this.getSearchParameters.bind(this),
{
ais: this.props.contextValue,
multiIndexContext: this.state.indexContext,
},
this.props
);
}

componentDidMount() {
this.unregisterWidget = this.props.contextValue.widgetsManager.registerWidget(
this
);
}

componentWillReceiveProps(nextProps: InnerProps) {
if (this.props.indexName !== nextProps.indexName) {
this.props.contextValue.widgetsManager.update();
}
if (this.props.indexId !== nextProps.indexId) {
this.setState({
indexContext: {
targetedIndex: nextProps.indexId,
},
});
}
}

componentWillUnmount() {
if (typeof this.unregisterWidget === 'function') {
this.unregisterWidget();
}
}

getSearchParameters(searchParameters, props) {
return searchParameters.setIndex(
this.props ? this.props.indexName : props.indexName
);
}

render() {
const childrenCount = Children.count(this.props.children);
const { Root, props } = this.props.root;
if (childrenCount === 0) {
return null;
}
return (
<Root {...props}>
<IndexProvider value={this.state.indexContext}>
{this.props.children}
</IndexProvider>
</Root>
);
}
}

const IndexWrapper: React.FC<Props> = props => (
<InstantSearchConsumer>
{contextValue => <Index contextValue={contextValue} {...props} />}
</InstantSearchConsumer>
);

export const IndexComponentWithoutContext = Index;
export default IndexWrapper;
Loading