components#SearchBar TypeScript Examples
The following examples show how to use
components#SearchBar.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Header.tsx From crossfeed with Creative Commons Zero v1.0 Universal | 4 votes |
HeaderNoCtx: React.FC<ContextType> = (props) => {
const { searchTerm, setSearchTerm } = props;
const classes = useStyles();
const history = useHistory();
const location = useLocation();
const {
currentOrganization,
setOrganization,
showAllOrganizations,
setShowAllOrganizations,
user,
logout,
apiGet
} = useAuthContext();
const [navOpen, setNavOpen] = useState(false);
const [organizations, setOrganizations] = useState<
(Organization | OrganizationTag)[]
>([]);
const theme = useTheme();
const isSmall = useMediaQuery(theme.breakpoints.down('md'));
let userLevel = 0;
if (user && user.isRegistered) {
if (user.userType === 'standard') {
userLevel = STANDARD_USER;
} else {
userLevel = GLOBAL_ADMIN;
}
}
const fetchOrganizations = useCallback(async () => {
try {
const rows = await apiGet<Organization[]>('/organizations/');
let tags: (OrganizationTag | Organization)[] = [];
if (userLevel === GLOBAL_ADMIN) {
tags = await apiGet<OrganizationTag[]>('/organizations/tags');
}
setOrganizations(tags.concat(rows));
} catch (e) {
console.error(e);
}
}, [apiGet, setOrganizations, userLevel]);
React.useEffect(() => {
if (userLevel > 0) {
fetchOrganizations();
}
}, [fetchOrganizations, userLevel]);
const navItems: NavItemType[] = [
{
title: 'Overview',
path: '/',
users: ALL_USERS,
exact: true
},
{
title: 'Inventory',
path: '/inventory',
users: ALL_USERS,
exact: false
},
{ title: 'Feeds', path: '/feeds', users: ALL_USERS, exact: false },
{
title: 'Scans',
path: '/scans',
users: GLOBAL_ADMIN,
exact: true
}
].filter(({ users }) => (users & userLevel) > 0);
const userMenu: NavItemType = {
title: (
<div className={classes.userLink}>
<UserIcon /> My Account <ArrowDropDown />
</div>
),
path: '#',
exact: false,
nested: [
{
title: 'Manage Organizations',
path: '/organizations',
users: GLOBAL_ADMIN,
exact: true
},
{
title: 'My Organizations',
path: '/organizations',
users: STANDARD_USER,
exact: true
},
{
title: 'Manage Users',
path: '/users',
users: GLOBAL_ADMIN,
exact: true
},
{
title: 'My Settings',
path: '/settings',
users: ALL_USERS,
exact: true
},
{
title: 'Logout',
path: '/settings',
users: ALL_USERS,
onClick: logout,
exact: true
}
].filter(({ users }) => (users & userLevel) > 0)
};
const userItemsSmall: NavItemType[] = [
{
title: 'My Account',
path: '#',
users: ALL_USERS,
exact: true
},
{
title: 'Manage Organizations',
path: '/organizations',
users: GLOBAL_ADMIN,
exact: true
},
{
title: 'My Organizations',
path: '/organizations',
users: STANDARD_USER,
exact: true
},
{
title: 'Manage Users',
path: '/users',
users: GLOBAL_ADMIN,
exact: true
},
{
title: 'My Settings',
path: '/settings',
users: ALL_USERS,
exact: true
},
{
title: 'Logout',
path: '/',
users: ALL_USERS,
onClick: logout,
exact: true
}
].filter(({ users }) => (users & userLevel) > 0);
const desktopNavItems: JSX.Element[] = navItems.map((item) => (
<NavItem key={item.title.toString()} {...item} />
));
const navItemsToUse = () => {
if (isSmall) {
return userItemsSmall;
} else {
return navItems;
}
};
return (
<div>
<AppBar position="static" elevation={0}>
<div className={classes.inner}>
<Toolbar>
<Link to="/">
<img
src={logo}
className={classes.logo}
alt="Crossfeed Icon Navigate Home"
/>
</Link>
<div className={classes.lgNav}>{desktopNavItems.slice()}</div>
<div className={classes.spacing} />
{userLevel > 0 && (
<>
<SearchBar
initialValue={searchTerm}
value={searchTerm}
onChange={(value) => {
if (location.pathname !== '/inventory')
history.push('/inventory?q=' + value);
setSearchTerm(value, {
shouldClearFilters: false,
autocompleteResults: false
});
}}
/>
{organizations.length > 1 && (
<>
<div className={classes.spacing} />
<Autocomplete
options={[{ name: 'All Organizations' }].concat(
organizations
)}
autoComplete={false}
className={classes.selectOrg}
classes={{
option: classes.option
}}
value={
showAllOrganizations
? { name: 'All Organizations' }
: currentOrganization ?? undefined
}
filterOptions={(options, state) => {
// If already selected, show all
if (
options.find(
(option) =>
option.name.toLowerCase() ===
state.inputValue.toLowerCase()
)
) {
return options;
}
return options.filter((option) =>
option.name
.toLowerCase()
.includes(state.inputValue.toLowerCase())
);
}}
disableClearable
blurOnSelect
selectOnFocus
getOptionLabel={(option) => option.name}
renderOption={(option) => (
<React.Fragment>{option.name}</React.Fragment>
)}
onChange={(
event: any,
value:
| Organization
| {
name: string;
}
| undefined
) => {
if (value && 'id' in value) {
setOrganization(value);
setShowAllOrganizations(false);
} else {
setShowAllOrganizations(true);
}
}}
renderInput={(params) => (
<TextField
{...params}
variant="outlined"
inputProps={{
...params.inputProps,
id: 'autocomplete-input',
autoComplete: 'new-password' // disable autocomplete and autofill
}}
/>
)}
/>
</>
)}
{isSmall ? null : <NavItem {...userMenu} />}
</>
)}
<IconButton
edge="start"
className={classes.menuButton}
aria-label="toggle mobile menu"
color="inherit"
onClick={() => setNavOpen((open) => !open)}
>
<MenuIcon />
</IconButton>
</Toolbar>
</div>
</AppBar>
<Drawer
anchor="right"
open={navOpen}
onClose={() => setNavOpen(false)}
data-testid="mobilenav"
>
<List className={classes.mobileNav}>
{navItemsToUse().map(({ title, path, nested, onClick }) => (
<React.Fragment key={title.toString()}>
{path && (
<ListItem
button
exact
component={NavLink}
to={path}
activeClassName={classes.activeMobileLink}
onClick={onClick ? onClick : undefined}
>
{title}
</ListItem>
)}
{nested?.map((nested) => (
<ListItem
button
exact
key={nested.title.toString()}
component={NavLink}
to={nested.onClick ? '#' : nested.path}
activeClassName={classes.activeMobileLink}
onClick={nested.onClick ? nested.onClick : undefined}
>
{nested.title}
</ListItem>
))}
</React.Fragment>
))}
</List>
</Drawer>
</div>
);
}