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

[EUI+] Fix/update various docs examples to match production #8118

Merged
merged 23 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
21feb82
Update getting started + theming pages to remove references to Sass a…
cee-chen Nov 4, 2024
dc97d41
Fix EuiFormControlLayoutDelimited docs
cee-chen Nov 4, 2024
700879f
Update EuiDraggable portal/popover docs
cee-chen Nov 4, 2024
578be7d
Update EuiResizable docs to match prod/use `accountForScrollbars`
cee-chen Nov 4, 2024
5ba0f55
Update EuiSuperDatePicker from prod
cee-chen Nov 4, 2024
230715d
Update EuiCallOut docs to match prod
cee-chen Nov 4, 2024
7efa516
EuiIcons updates/fixes
cee-chen Nov 4, 2024
f58a363
Update/fix EuiToast docs
cee-chen Nov 4, 2024
e1437d4
Delete EuiPopover `panelClassName` example
cee-chen Nov 4, 2024
218ac57
Fix broken EuiDatePicker `className` demo
cee-chen Nov 4, 2024
ea436d2
Update EuiDatePicker display toggles to match prod
cee-chen Nov 4, 2024
3473798
Update first `EuiComboBox` example to not autoFocus/attempt to open o…
cee-chen Nov 4, 2024
4485dbb
Update various form row `display` props to match propd
cee-chen Nov 4, 2024
54344d0
Update/fix markdown links
cee-chen Nov 4, 2024
ca9ae70
Fix EuiSearchBar demos
cee-chen Nov 4, 2024
74c399a
Fix EuiDatePicker `customInput` example
cee-chen Nov 4, 2024
5dc1233
Fix EuiSuperDatePicker demos
cee-chen Nov 4, 2024
39e88b6
Fix EuiText sizing example + custom CSS
cee-chen Nov 4, 2024
deee3ec
Fix EuiAvatar icon example
cee-chen Nov 4, 2024
5b8baa9
Fix broken EuiContextMenu example
cee-chen Nov 4, 2024
750e7f2
Fix EuiEmptyPrompt multiple types example
cee-chen Nov 4, 2024
1d6a216
Update table selection demo to allow toggling uncontrolled + controll…
cee-chen Nov 5, 2024
b56e233
[PR feedback] Typo
cee-chen Nov 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -153,262 +153,9 @@ For uncontrolled usage, where selection changes are determined entirely to be se

To completely control table selection, use `selection.selected` instead (which requires passing `selected.onSelectionChange`). This can be useful if you want to handle table selections based on user interaction with another part of the UI.

```tsx interactive
import React, { useState, ReactNode } from 'react';
import {
EuiInMemoryTable,
EuiBasicTableColumn,
EuiTableSelectionType,
EuiSearchBarProps,
EuiHealth,
EuiButton,
EuiEmptyPrompt,
} from '@elastic/eui';
import { faker } from '@faker-js/faker';

type User = {
id: number;
firstName: string | null | undefined;
lastName: string;
online: boolean;
location: {
city: string;
country: string;
};
};

const userData: User[] = [];

for (let i = 0; i < 20; i++) {
userData.push({
id: i + 1,
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
online: faker.datatype.boolean(),
location: {
city: faker.location.city(),
country: faker.location.country(),
},
});
}

const onlineUsers = userData.filter((user) => user.online);
import InMemoryTableSelection from './table_selection';

const columns: Array<EuiBasicTableColumn<User>> = [
{
field: 'firstName',
name: 'First Name',
sortable: true,
truncateText: true,
mobileOptions: {
render: (user: User) => (
<>
{user.firstName} {user.lastName}
</>
),
header: false,
truncateText: false,
enlarge: true,
width: '100%',
},
},
{
field: 'lastName',
name: 'Last Name',
truncateText: true,
mobileOptions: {
show: false,
},
},
{
field: 'location',
name: 'Location',
truncateText: true,
textOnly: true,
render: (location: User['location']) => {
return `${location.city}, ${location.country}`;
},
},
{
field: 'online',
name: 'Online',
dataType: 'boolean',
render: (online: User['online']) => {
const color = online ? 'success' : 'danger';
const label = online ? 'Online' : 'Offline';
return <EuiHealth color={color}>{label}</EuiHealth>;
},
sortable: true,
mobileOptions: {
show: false,
},
},
];

const noItemsFoundMsg = 'No users match search criteria';

export default () => {
const [loading, setLoading] = useState(false);
const [users, setUsers] = useState<User[]>([]);
const [message, setMessage] = useState<ReactNode>(
<EuiEmptyPrompt
title={<h3>No users</h3>}
titleSize="xs"
body="Looks like you don&rsquo;t have any users. Let&rsquo;s create some!"
actions={
<EuiButton
size="s"
key="loadUsers"
onClick={() => {
loadUsers();
}}
>
Load Users
</EuiButton>
}
/>
);

const [selection, setSelection] = useState<User[]>([]);
const [error, setError] = useState<string | undefined>();

const loadUsers = () => {
setMessage('Loading users...');
setLoading(true);
setUsers([]);
setError(undefined);
setTimeout(() => {
setLoading(false);
setMessage(noItemsFoundMsg);
setError(undefined);
setUsers(userData);
}, faker.number.int({ min: 0, max: 3000 }));
};

const loadUsersWithError = () => {
setMessage('Loading users...');
setLoading(true);
setUsers([]);
setError(undefined);
setTimeout(() => {
setLoading(false);
setMessage(noItemsFoundMsg);
setError('ouch!... again... ');
setUsers([]);
}, faker.number.int({ min: 0, max: 3000 }));
};

const renderToolsLeft = () => {
if (selection.length === 0) {
return;
}

const onClick = () => {
const deleteUsersByIds = (users: User[], ids: number[]) => {
const updatedUsers = [...users];
ids.forEach((id) => {
const index = updatedUsers.findIndex((user) => user.id === id);
if (index >= 0) {
updatedUsers.splice(index, 1);
}
});
return updatedUsers;
};

setUsers((users) =>
deleteUsersByIds(
users,
selection.map((user) => user.id)
)
);
setSelection([]);
};

return (
<EuiButton color="danger" iconType="trash" onClick={onClick}>
Delete {selection.length} Users
</EuiButton>
);
};

const renderToolsRight = () => {
return [
<EuiButton
key="loadUsers"
onClick={() => {
loadUsers();
}}
isDisabled={loading}
>
Load Users
</EuiButton>,
<EuiButton
key="loadUsersError"
onClick={() => {
loadUsersWithError();
}}
isDisabled={loading}
>
Load Users (Error)
</EuiButton>,
];
};

const search: EuiSearchBarProps = {
toolsLeft: renderToolsLeft(),
toolsRight: renderToolsRight(),
box: {
incremental: true,
},
filters: [
{
type: 'is',
field: 'online',
name: 'Online',
negatedName: 'Offline',
},
{
type: 'field_value_selection',
field: 'location.country',
name: 'Country',
multiSelect: false,
options: userData.map(({ location: { country } }) => ({
value: country,
})),
},
],
};

const pagination = {
initialPageSize: 5,
pageSizeOptions: [3, 5, 8],
};

const selectionValue: EuiTableSelectionType<User> = {
selectable: (user) => user.online,
selectableMessage: (selectable) =>
!selectable ? 'User is currently offline' : '',
onSelectionChange: (selection) => setSelection(selection),
initialSelected: onlineUsers,
};

return (
<EuiInMemoryTable
tableCaption="Demo of EuiInMemoryTable with uncontrolled selection"
items={users}
itemId="id"
error={error}
loading={loading}
message={message}
columns={columns}
search={search}
pagination={pagination}
sorting={true}
selection={selectionValue}
/>
);
};
```
<InMemoryTableSelection type="inMemory" />

## In-memory table with search

Expand Down
Loading
Loading