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

refactor(root): remove createIndex & createInstantSearch #2339

Merged
merged 12 commits into from
Jun 17, 2019
Merged
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
},
{
"path": "packages/react-instantsearch/dist/umd/Connectors.min.js",
"maxSize": "41 kB"
"maxSize": "42 kB"
},
{
"path": "packages/react-instantsearch/dist/umd/Dom.min.js",
Expand Down
63 changes: 24 additions & 39 deletions packages/react-instantsearch-core/src/components/Index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component, Children, ReactType } from 'react';
import React, { Component, Children } from 'react';
import PropTypes from 'prop-types';
import {
InstantSearchConsumer,
Expand All @@ -7,13 +7,15 @@ import {
IndexContext,
} from '../core/context';

function getIndexContext(props: Props): IndexContext {
return {
targetedIndex: props.indexId || props.indexName,
};
}

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

type InnerProps = Props & { contextValue: InstantSearchContext };
Expand All @@ -29,7 +31,6 @@ type State = {
* @kind widget
* @name <Index>
* @propType {string} indexName - index in which to search.
samouss marked this conversation as resolved.
Show resolved Hide resolved
* @propType {{ Root: string|function, props: object }} [root] - Use this to customize the root element. Default value: `{ Root: 'div' }`
* @example
* import React from 'react';
* import algoliasearch from 'algoliasearch/lite';
Expand Down Expand Up @@ -58,36 +59,31 @@ type State = {
*/
class Index extends Component<InnerProps, State> {
static propTypes = {
// @TODO: These props are currently constant.
indexName: PropTypes.string.isRequired,
indexId: PropTypes.string.isRequired,
indexId: PropTypes.string,
children: PropTypes.node,
root: PropTypes.shape({
Root: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.object,
]),
props: PropTypes.object,
}).isRequired,
};

unregisterWidget?: () => void;
static getDerivedStateFromProps(props: InnerProps) {
return {
indexContext: getIndexContext(props),
};
}

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

unregisterWidget?: () => void;

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

this.props.contextValue.onSearchParameters(
this.getSearchParameters.bind(this),
{
ais: this.props.contextValue,
multiIndexContext: this.state.indexContext,
multiIndexContext: getIndexContext(props),
samouss marked this conversation as resolved.
Show resolved Hide resolved
},
this.props
);
Expand All @@ -99,18 +95,10 @@ class Index extends Component<InnerProps, State> {
);
}

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

componentWillUnmount() {
Expand All @@ -119,24 +107,21 @@ class Index extends Component<InnerProps, State> {
}
}

getSearchParameters(searchParameters, props) {
getSearchParameters(searchParameters, props: InnerProps) {
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>
<IndexProvider value={this.state.indexContext}>
{this.props.children}
</IndexProvider>
);
}
}
Expand Down
28 changes: 6 additions & 22 deletions packages/react-instantsearch-core/src/components/InstantSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component, Children, ReactType } from 'react';
import React, { Component, Children } from 'react';
import PropTypes from 'prop-types';
import createInstantSearchManager from '../core/createInstantSearchManager';
import { InstantSearchProvider, InstantSearchContext } from '../core/context';
Expand Down Expand Up @@ -61,10 +61,6 @@ type Props = {
searchState: SearchState
) => void;
stalledSearchDelay?: number;
root: {
Root: ReactType;
props: {};
};
resultsState: SearchResults | { [indexId: string]: SearchResults };
};

Expand Down Expand Up @@ -113,6 +109,7 @@ type State = {
class InstantSearch extends Component<Props, State> {
static defaultProps = {
stalledSearchDelay: 200,
refresh: false,
};

static propTypes = {
Expand All @@ -128,7 +125,7 @@ class InstantSearch extends Component<Props, State> {

createURL: PropTypes.func,

refresh: PropTypes.bool.isRequired,
refresh: PropTypes.bool,

searchState: PropTypes.object,
onSearchStateChange: PropTypes.func,
Expand All @@ -137,16 +134,6 @@ class InstantSearch extends Component<Props, State> {
resultsState: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),

children: PropTypes.node,

root: PropTypes.shape({
Root: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.object,
]),
props: PropTypes.object,
}).isRequired,

stalledSearchDelay: PropTypes.number,
};

Expand Down Expand Up @@ -261,16 +248,13 @@ class InstantSearch extends Component<Props, State> {

render() {
const childrenCount = Children.count(this.props.children);
const { Root, props } = this.props.root;
if (childrenCount === 0) {
return null;
}
return (
<Root {...props}>
<InstantSearchProvider value={this.state.contextValue}>
{this.props.children}
</InstantSearchProvider>
</Root>
<InstantSearchProvider value={this.state.contextValue}>
{this.props.children}
</InstantSearchProvider>
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ describe('Index', () => {
it('calls update if indexName prop changes', () => {
const context = createContext();

const wrapper = shallow(
// componentDidUpdate wasn't called on `shallow`
samouss marked this conversation as resolved.
Show resolved Hide resolved
const wrapper = mount(
<IndexComponentWithoutContext {...requiredProps} contextValue={context}>
<div />
</IndexComponentWithoutContext>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,15 @@ describe('InstantSearch', () => {
indexName: 'foobar',
});

expect(wrapper.html()).toMatchInlineSnapshot(`"<div>foobar</div>"`);
expect(wrapper.html()).toMatchInlineSnapshot(`"foobar"`);

expect(ism.updateIndex).not.toHaveBeenCalled();

wrapper.setProps({
indexName: 'newIndexName',
});

expect(wrapper.html()).toMatchInlineSnapshot(`"<div>newIndexName</div>"`);
expect(wrapper.html()).toMatchInlineSnapshot(`"newIndexName"`);

expect(ism.updateIndex).toHaveBeenCalledTimes(1);
});
Expand Down

This file was deleted.

This file was deleted.

Loading