Skip to content

Commit

Permalink
fix(Proptypes): Allow numbers and objects in filters (#19)
Browse files Browse the repository at this point in the history
* docs: Format README.md

The precommit hook did this automatically.

* fix: Allow numbers and objects in filters

For example: `filter={["thing", "==", db.doc("things/" + thing)]}`

Closes #16
  • Loading branch information
bfirsh authored and green-arrow committed Jun 7, 2018
1 parent b759d03 commit b90f528
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
46 changes: 34 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import firebase from '@firebase/app';
import '@firebase/firestore';
import { FirestoreProvider } from "react-firestore";
import { FirestoreProvider } from 'react-firestore';

import App from './App';

Expand Down Expand Up @@ -183,21 +183,43 @@ The maximum number of documents to retrieve from the collection.
> `array` or `array of array` | defaults to `null`
Passing in an array of strings creates a simple query to filter the collection by

```jsx
<FirestoreCollection
path={"users"}
filter={['firstName', '==', 'Mike']}
render={()=>{/* render stuff*/}}
/>
path={'users'}
filter={['firstName', '==', 'Mike']}
render={() => {
/* render stuff*/
}}
/>
```

Passing in an array of arrays creates a compound query to filter the collection by

```jsx
<FirestoreCollection
path={"users"}
filter={[['firstName', '==', 'Mike'], ['lastName', '==', 'Smith']]}
render={()=>{/* render stuff*/}}
/>
path={'users'}
filter={[['firstName', '==', 'Mike'], ['lastName', '==', 'Smith']]}
render={() => {
/* render stuff*/
}}
/>
```

Passing in document references allows you to filter by reference fields:

```jsx
<FirestoreCollection
path={'users'}
filter={[
'organization',
'==',
firestore.collection('organizations').doc('foocorp'),
]}
render={() => {
/* render stuff*/
}}
/>
```

##### render
Expand Down Expand Up @@ -287,8 +309,8 @@ or if you would just rather interact directly with the `firestore` object.
This is the function where you render whatever you want using the firestore
object passed in.

| property | type | description |
| --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| property | type | description |
| --------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| firestore | `Object` | The `Firestore` class from [firestore][firestore-package]. See the docs for the [Firestore class][firestore-class-docs] for more information. |

### `withFirestore`
Expand All @@ -299,7 +321,7 @@ directly to the wrapped component via the `firestore` prop.
```jsx
class MyComponent extends Component {
state = {
story: null
story: null,
};

componentDidMount() {
Expand Down
8 changes: 7 additions & 1 deletion src/FirestoreCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ class FirestoreCollection extends Component {
sort: PropTypes.string,
limit: PropTypes.number,
filter: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.object,
]),
),
PropTypes.arrayOf(PropTypes.array),
]),
children: PropTypes.func,
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/collection.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,25 @@ test('filters the documents returned with a compound filter', () => {
}),
);
});

test('filter accepts objects and numbers', () => {
const { firestoreMock } = createMocksForCollection();
const renderMock = jest.fn().mockReturnValue(<div />);
const collectionName = 'users';
mount(
<FirestoreCollection
path={collectionName}
filter={['number', '==', 5]}
render={renderMock}
/>,
{ context: { firestoreDatabase: firestoreMock, firestoreCache: {} } },
);
mount(
<FirestoreCollection
path={collectionName}
filter={['ref', '==', firestoreMock.doc('things/foobar')]}
render={renderMock}
/>,
{ context: { firestoreDatabase: firestoreMock, firestoreCache: {} } },
);
});

0 comments on commit b90f528

Please sign in to comment.