Skip to content

Commit a7ea092

Browse files
committed
Add export and import information to intro
1 parent cf3d70e commit a7ea092

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

docs/intro.mdx

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ In the query builder below, click the "+Rule" button and then the fields selecto
3535
<SandpackRQB options={{ editorHeight: 240 }}>
3636

3737
```tsx
38-
import QueryBuilder from 'react-querybuilder';
38+
import { QueryBuilder } from 'react-querybuilder';
3939
import 'react-querybuilder/dist/query-builder.scss';
4040

4141
const fields: Field[] = [
@@ -56,7 +56,7 @@ The state variable `query` will be passed to the `query` prop, and the setter me
5656

5757
```tsx
5858
import { useState } from 'react';
59-
import QueryBuilder, { Field, RuleGroupType } from 'react-querybuilder';
59+
import { QueryBuilder, Field, RuleGroupType } from 'react-querybuilder';
6060
import 'react-querybuilder/dist/query-builder.scss';
6161

6262
const fields: Field[] = [
@@ -84,7 +84,7 @@ export default () => {
8484

8585
```jsx
8686
import { useState } from 'react';
87-
import QueryBuilder from 'react-querybuilder';
87+
import { QueryBuilder } from 'react-querybuilder';
8888
import 'react-querybuilder/dist/query-builder.scss';
8989

9090
const fields = [
@@ -113,9 +113,48 @@ This documentation primarily demonstrates use of the ESM and CommonJS builds of
113113

114114
:::
115115

116+
## Exporting queries
117+
118+
To convert a query object into other formats like [SQL](https://en.wikipedia.org/wiki/SQL), [MongoDB](https://www.mongodb.com/), and [CEL](https://github.com/google/cel-spec), you can use the `formatQuery` function ([full documentation here](./api/export)). The example below demonstrates the conversion of a query to its equivalent SQL form. Modify the query by manipulating the form elements and the SQL will update accordingly.
119+
120+
<SandpackRQB options={{ editorHeight: 480 }}>
121+
122+
```tsx
123+
import { useState } from 'react';
124+
import { QueryBuilder, Field, formatQuery, RuleGroupType } from 'react-querybuilder';
125+
import 'react-querybuilder/dist/query-builder.scss';
126+
127+
const fields: Field[] = [
128+
{ name: 'firstName', label: 'First Name' },
129+
{ name: 'lastName', label: 'Last Name' },
130+
];
131+
132+
export default () => {
133+
const [query, setQuery] = useState<RuleGroupType>({
134+
combinator: 'and',
135+
rules: [
136+
{ field: 'firstName', operator: 'beginsWith', value: 'Stev' },
137+
{ field: 'lastName', operator: 'in', value: 'Vai,Vaughan' },
138+
],
139+
});
140+
141+
return (
142+
<>
143+
<QueryBuilder fields={fields} query={query} onQueryChange={q => setQuery(q)} />
144+
<h4>
145+
SQL as result of <code>formatQuery(query, 'sql')</code>:
146+
</h4>
147+
<pre>{formatQuery(query, 'sql')}</pre>
148+
</>
149+
);
150+
};
151+
```
152+
153+
</SandpackRQB>
154+
116155
## Onward and upward!
117156

118-
To discover all the options of React Query Builder, check out the [API documentation](./api/querybuilder). To play around with various configurations, check out the ["kitchen sink" demo](https://react-querybuilder.github.io/react-querybuilder/).
157+
To discover all the options of React Query Builder, check out the [API documentation](./api/querybuilder). To play around with various configurations, including other export formats and [importing from SQL](./api/import), check out the ["kitchen sink" demo](https://react-querybuilder.js.org/react-querybuilder/).
119158

120159
## Training
121160

0 commit comments

Comments
 (0)