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

[Doc] Add Search component #8021

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
192 changes: 192 additions & 0 deletions docs/Search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
layout: default
title: "The Search Component"
---

# `<Search>`

This [Enterprise Edition](https://marmelab.com/ra-enterprise)<img class="icon" src="./img/premium.svg" /> component, part of [`ra-search`](https://marmelab.com/ra-enterprise/modules/ra-search), lets user do a site-wide search via a smart Omnibox.

![ra-search](https://marmelab.com/ra-enterprise/modules/assets/ra-search-demo.gif)

`<Search>` renders a global search input. It's designed to be integrated into the top `<AppBar>`.

It relies on the `dataProvider` to provide a `search()` method, so you can use it with any search engine (Lucene, ElasticSearch, Solr, Algolia, Google Cloud Search, and many others). And if you don't have a search engine, no problem! `<Search>` can also do the search across several resources via parallel `dataProvider.getList()` queries.

## Usage

Include the `<Search>` component inside a custom `<AppBar>` component:

{% raw %}
```jsx
// in src/MyAppBar.jsx
import { AppBar } from "react-admin";
import { Typography } from "@mui/material";
import { Search } from "@react-admin/ra-search";

export const MyAppbar = (props) => (
<AppBar {...props}>
<Typography
variant="h6"
color="inherit"
sx={{
flex: 1,
textOverflow: "ellipsis",
whiteSpace: "nowrap",
overflow: "hidden",
}}
id="react-admin-title"
/>
<Search />
</AppBar>
);
```
{% endraw %}

Include that AppBar in [a custom layout component](./Layout.md):

```jsx
// in src/MyLayout.jsx
import { Layout } from "react-admin";
import { MyAppbar } from "./MyAppBar";

export const MyLayout = (props) => <Layout {...props} appBar={MyAppbar} />;
```

Finally, include that custom layout in the `<Admin>`. You'll also need to setup the ì18nProvider`, as the `ra-search` package comes with some new translations.

```jsx
// in src/Admin.ts
import { Admin } from "react-admin";

import { MyLayout } from "./MyLayout";

export const App = () => (
<Admin
dataProvider={searchDataProvider}
i18nProvider={i18nProvider}
layout={MyLayout}
>
// ...
</Admin>
);
```

Your `dataProvider` should support the `search()` method. Check [the `ra-search` documentation](https://marmelab.com/ra-enterprise/modules/ra-search) to learn its i,nput and output interface, as well as tricks to use `dataProvider.search()` without a search engine.

## Props


| Prop | Required | Type | Default | Description |
| ----------- | -------- | ------------------ | -------- | ------------------------------------------------------------------ |
| `children` | Optional | `ReactNode` | `<SearchResultsPanel>` | The search result renderer |
| `options` | Optional | `object` | - | The search options (see details below) |
| `wait` | Optional | `number` | 500 | The delay of debounce for the search to launch after typing in ms. |
| `color` | Optional | `'light' | 'dark'` | 'light' | The color mode for the input, applying light or dark background. |

The `options` object can contain the following keys:

- `targets`: `string[]` An array of the indices on which to perform the search. Defaults to an empty array.
- `historySize`: `number` The max number of search texts kept in the history. Default is 5.
- `{any}`: `{any}` Any custom option to pass to the search engine.

## Customizing The Result Items

By default, `<Search>` displays the results in `<SearchResultsPanel>`, which displays each results in a `<SearchResultItem>`. So rendering `<Search>` without children is equivalent to rendering:

```jsx
const MySearch = () => (
<Search>
<SearchResultsPanel>
<SearchResultItem />
</SearchResultsPanel>
</Search>
);
```

`<SearchResultItem>` renders the `content.label` and `content.description` for each result. You can customize what it renders by providing a function as the `label` and the `description` props. This function takes the search result as a parameter and must return a React element.

For instance:

```jsx
import {
Search,
SearchResultsPanel,
SearchResultItem,
} from '@react-admin/ra-search';

const MySearch = () => (
<Search>
<SearchResultsPanel>
<SearchResultItem
label={record => (
<>
{record.type === 'artists' ? (
<PersonIcon />
) : (
<MusicIcon />
)}
<span>{record.content.label}</span>
</>
)}
/>
</SearchResultsPanel>
</Search>
);
```

You can also completely replace the search result item component:

```jsx
import { Search, SearchResultsPanel } from '@react-admin/ra-search';

const MySearchResultItem = ({ data, onClose }) => (
<li key={data.id}>
<Link to={data.url} onClick={onClose}>
<strong>{data.content.label}</strong>
</Link>
<p>{data.content.description}</p>
</li>
);

const MySearch = () => (
<Search>
<SearchResultsPanel>
<MySearchResultItem />
</SearchResultsPanel>
</Search>
);
```

## Customizing the Entire Search Results

Pass a custom React element as a child of `<Search>` to customize the appearance of the search results. This can be useful e.g. to customize the results grouping, or to arrange search results differently.

`ra-search` renders the `<Search>` inside a `SearchContext`. You can use the `useSearchResultContext` hook to read the search results, as follows:

```jsx
import { Search, useSearchResult } from '@react-admin/ra-search';

const MySearch = props => (
<Search>
<CustomSearchResultsPanel />
</Search>
);

const CustomSearchResultsPanel = () => {
const { data, onClose } = useSearchResult();

return (
<ul>
{data.map(searchResult => (
<li key={searchResult.id}>
<Link to={searchResult.url} onClick={onClose}>
<strong>{searchResult.content.label}</strong>
</Link>
<p>{searchResult.content.description}</p>
</li>
))}
</ul>
);
};
```
121 changes: 23 additions & 98 deletions docs/Theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ const App = () => (
);
```

## Light and Dark Themes
## Using A Dark Theme

MUI ships two base themes: light and dark. React-admin uses the light one by default. To use the dark theme, create a custom theme object with a `mode: 'dark'` palette, and pass it as the `<Admin theme>` prop:

Expand All @@ -365,35 +365,36 @@ const App = () => (

![Dark theme](./img/dark-theme.png)

If you want to let users choose between the light and dark themes, check the next section.
## Letting Users Choose The Theme

## Changing the Theme Programmatically
The `<ToggleThemeButton>` component lets users switch from light to dark mode, and persists that choice by leveraging the [store](./Store.md).

You can define several themes (usually a light and a dark theme), and let the user choose between them.
![Dark Mode support](./img/ToggleThemeButton.gif)

React-admin provides the `useTheme` hook to read and update the theme programmatically. It uses the same syntax as `useState`:
You can add the `<ToggleThemeButton>` to a custom App Bar:

```jsx
import { defaultTheme, useTheme } from 'react-admin';
import { Button } from '@mui/material';
import * as React from 'react';
import { defaultTheme, Layout, AppBar, ToggleThemeButton } from 'react-admin';
import { createTheme, Box, Typography } from '@mui/material';

const lightTheme = defaultTheme;
const darkTheme = {
...defaultTheme,
palette: {
mode: 'dark',
},
};
const darkTheme = createTheme({
palette: { mode: 'dark' },
});

const ThemeToggler = () => {
const [theme, setTheme] = useTheme();
const MyAppBar = props => (
<AppBar {...props}>
<Box flex="1">
<Typography variant="h6" id="react-admin-title"></Typography>
</Box>
<ToggleThemeButton
lightTheme={defaultTheme}
darkTheme={darkTheme}
/>
</AppBar>
);

return (
<Button onClick={() => setTheme(theme.palette.mode === 'dark' ? lightTheme : darkTheme)}>
{theme.palette.mode === 'dark' ? 'Switch to light theme' : 'Switch to dark theme'}
</Button>
);
}
const MyLayout = props => <Layout {...props} appBar={MyAppBar} />;
```

## Conditional Formatting
Expand Down Expand Up @@ -760,50 +761,6 @@ export default MyLayout;
```
{% endraw %}

## Adding a Breadcrumb
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary anymore since we have a Breadcrumb page


The `<Breadcrumb>` component is part of `ra-navigation`, an [Enterprise Edition](https://marmelab.com/ra-enterprise)<img class="icon" src="./img/premium.svg" /> module. It displays a breadcrumb based on a site structure that you can override at will.

```jsx
import * as React from 'react';
import {
AppLocationContext,
Breadcrumb,
ResourceBreadcrumbItems,
} from '@react-admin/ra-navigation';
import { Admin, Resource, Layout } from 'react-admin';

import PostList from './PostList';
import PostEdit from './PostEdit';
import PostShow from './PostShow';
import PostCreate from './PostCreate';

const MyLayout = ({ children, ...props }) => (
<AppLocationContext>
<Layout {...props}>
<Breadcrumb {...props}>
<ResourceBreadcrumbItems />
</Breadcrumb>
{children}
</Layout>
</AppLocationContext>
);

const App = () => (
<Admin dataProvider={dataProvider} layout={MyLayout}>
<Resource
name="posts"
list={PostList}
edit={PostEdit}
show={PostShow}
create={PostCreate}
/>
</Admin>
);
```

Check [the `ra-navigation` documentation](https://marmelab.com/ra-enterprise/modules/ra-navigation) for more details.

## Customizing the AppBar Content

By default, the react-admin `<AppBar>` component displays the page title. You can override this default by passing children to `<AppBar>` - they will replace the default title. And if you still want to include the page title, make sure you include an element with id `react-admin-title` in the top bar (this uses [React Portals](https://reactjs.org/docs/portals.html)).
Expand Down Expand Up @@ -921,38 +878,6 @@ To make it easier to customize, we export some components and hooks used by the
- `<SidebarToggleButton>`: An `IconButton` used to toggle the `<Sidebar>`.
- `useSidebarState`: A hook that returns the sidebar open state and a function to toggle it. Used internally by `<SidebarToggleButton>`.

## Adding Dark Mode Support
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was explained twice...


The `<ToggleThemeButton>` component lets users switch from light to dark mode, and persists that choice by leveraging the [store](./Store.md).

![Dark Mode support](./img/ToggleThemeButton.gif)

You can add the `<ToggleThemeButton>` to a custom App Bar:

```jsx
import * as React from 'react';
import { defaultTheme, Layout, AppBar, ToggleThemeButton } from 'react-admin';
import { createTheme, Box, Typography } from '@mui/material';

const darkTheme = createTheme({
palette: { mode: 'dark' },
});

const MyAppBar = props => (
<AppBar {...props}>
<Box flex="1">
<Typography variant="h6" id="react-admin-title"></Typography>
</Box>
<ToggleThemeButton
lightTheme={defaultTheme}
darkTheme={darkTheme}
/>
</AppBar>
);

const MyLayout = props => <Layout {...props} appBar={MyAppBar} />;
```

## Using a Custom Menu

By default, React-admin uses the list of `<Resource>` components passed as children of `<Admin>` to build a menu to each resource with a `list` component. If you want to reorder, add or remove menu items, for instance to link to non-resources pages, you have to provide a custom `<Menu>` component to your `Layout`.
Expand Down
1 change: 1 addition & 0 deletions docs/navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
<li {% if page.path == 'MultiLevelMenu.md' %} class="active" {% endif %}><a class="nav-link" href="./MultiLevelMenu.html"><code>&lt;MultiLevelMenu&gt;</code><img class="premium" src="./img/premium.svg" /></a></li>
<li {% if page.path == 'IconMenu.md' %} class="active" {% endif %}><a class="nav-link" href="./IconMenu.html"><code>&lt;IconMenu&gt;</code><img class="premium" src="./img/premium.svg" /></a></li>
<li {% if page.path == 'Breadcrumb.md' %} class="active" {% endif %}><a class="nav-link" href="./Breadcrumb.html"><code>&lt;Breadcrumb&gt;</code><img class="premium" src="./img/premium.svg" /></a></li>
<li {% if page.path == 'Search.md' %} class="active" {% endif %}><a class="nav-link" href="./Search.html"><code>&lt;Search&gt;</code><img class="premium" src="./img/premium.svg" /></a></li>
<li {% if page.path == 'Buttons.md' %} class="active" {% endif %}><a class="nav-link" href="./Buttons.html">Buttons</a></li>
<li {% if page.path == 'Confirm.md' %} class="active" {% endif %}><a class="nav-link" href="./Confirm.html">Confirm</a></li>
</ul>
Expand Down