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

[EuiSelectable] Enable CodeSandbox links and misc documentation improvements #6074

Merged
merged 13 commits into from
Jul 27, 2022
Merged
2 changes: 1 addition & 1 deletion src-docs/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {

import { PageTemplateExample } from './views/page/page_template_example';

import { SitewideSearchExample } from './views/selectable/selectable_sitewide_template_example';
import { SitewideSearchExample } from './views/selectable/selectable_templates/sitewide_example';

// Services

Expand Down
2 changes: 1 addition & 1 deletion src-docs/src/views/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $guideDemoHighlightColor: transparentize($euiColorPrimary, .9);
@import './horizontal_rule/horizontal_rule';
@import './page/page';
@import './notification_event/notification_event';
@import './selectable/search';
@import './selectable/selectable_templates/sitewide';
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional:

Should we move these styles to Emotion so they render in the code sandbox?

Copy link
Contributor Author

@cee-chen cee-chen Jul 25, 2022

Choose a reason for hiding this comment

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

I gave this a shot but unfortunately there are a few limitations:

  • The styles being set are using EUI theme variables, meaning I have to call useEuiTheme(), meaning I can't just pass a static css prop into the list of searchData array variables
  • Because the options are being rendered in a popover, I can't pass the css={} prop into <EuiSelectableTemplateSitewide> directly, I have to pass it into listProps={{}}
  • listProps={{ css: css`...` }} works in actual UI, but Typescript complains about the css prop not existing on our types, which is odd. We may need to add css in our CommonProps declaration. Which I'm fine to do, but I was really hoping to keep this PR as src-doc changes only, so I'll check in with Greg about the implications of that and then tackle it in another PR, if that's cool!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, that's fine. 👍

@import './spacer/spacer';
@import './suggest/index';
@import './text/text_scaling';
Expand Down
39 changes: 0 additions & 39 deletions src-docs/src/views/selectable/data.ts

This file was deleted.

44 changes: 41 additions & 3 deletions src-docs/src/views/selectable/selectable.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
import React, { useState } from 'react';

import { EuiSelectable } from '../../../../src/components/selectable';
import { Options } from './data';
import {
EuiSelectable,
EuiSelectableOption,
} from '../../../../src/components/selectable';
cee-chen marked this conversation as resolved.
Show resolved Hide resolved

export default () => {
const [options, setOptions] = useState(Options);
const [options, setOptions] = useState<EuiSelectableOption[]>([
{
label: 'Titan',
'data-test-subj': 'titanOption',
},
{
label: 'Enceladus is disabled',
disabled: true,
},
{
label: 'Mimas',
checked: 'on',
},
{
label: 'Dione',
},
{
label: 'Iapetus',
checked: 'on',
},
{
label: 'Phoebe',
},
{
label: 'Rhea',
},
{
label:
"Pandora is one of Saturn's moons, named for a Titaness of Greek mythology",
},
{
label: 'Tethys',
},
{
label: 'Hyperion',
},
]);

return (
<EuiSelectable
Expand Down
112 changes: 0 additions & 112 deletions src-docs/src/views/selectable/selectable_custom_render.js

This file was deleted.

122 changes: 122 additions & 0 deletions src-docs/src/views/selectable/selectable_custom_render.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import React, { useState } from 'react';

import {
EuiBadge,
EuiHighlight,
EuiSpacer,
EuiText,
EuiSwitch,
EuiSelectable,
EuiSelectableOption,
} from '../../../../src/components';

const countryData = [
{ code: 'NL', name: 'Netherlands', flag: '🇳🇱' },
{ code: 'CZ', name: 'Czech Republic', flag: '🇨🇿' },
{ code: 'ZA', name: 'South Africa', flag: '🇿🇦' },
{ code: 'US', name: 'United States', flag: '🇺🇲' },
{ code: 'AU', name: 'Australia', flag: '🇦🇺' },
{ code: 'IL', name: 'Israel', flag: '🇮🇱' },
{ code: 'NO', name: 'Norway', flag: '🇳🇴' },
{ code: 'IT', name: 'Italy', flag: '🇮🇹' },
{ code: 'CA', name: 'Canada', flag: '🇨🇦' },
{ code: 'CG', name: 'Congo', flag: '🇨🇬' },
{ code: 'CL', name: 'Chile', flag: '🇨🇱' },
{ code: 'FJ', name: 'Fiji', flag: '🇫🇯' },
{ code: 'GB', name: 'United Kingdom', flag: '🇬🇧' },
{ code: 'GR', name: 'Greece', flag: '🇬🇷' },
{ code: 'HT', name: 'Haiti', flag: '🇭🇹' },
{ code: 'LB', name: 'Lebanon', flag: '🇱🇧' },
{ code: 'MM', name: 'Myanmar', flag: '🇲🇲' },
{ code: 'MX', name: 'Mexico', flag: '🇲🇽' },
{ code: 'NG', name: 'Nigeria', flag: '🇳🇬' },
{ code: 'SG', name: 'Singapore', flag: '🇸🇬' },
{ code: 'SO', name: 'Somalia', flag: '🇸🇴' },
{ code: 'TN', name: 'Tunisia', flag: '🇹🇳' },
{ code: 'VE', name: 'Venezuela', flag: '🇻🇪' },
{ code: 'ZM', name: 'Zambia', flag: '🇿🇲' },
];

interface OptionData {
secondaryContent?: string;
}

export default () => {
const [options, setOptions] = useState<
Array<EuiSelectableOption<OptionData>>
>([
{
label: 'Country options',
isGroupLabel: true,
},
...countryData.map(
(country): EuiSelectableOption => ({
label: `${country.name}`,
searchableLabel: `${country.name} ${'I am secondary content, I am!'}`,
prepend: country.flag,
append: <EuiBadge>{country.code}</EuiBadge>,
data: {
secondaryContent: 'I am secondary content, I am!',
},
})
),
]);

const renderCountryOption = (
option: EuiSelectableOption<OptionData>,
searchValue: string
) => {
return (
<>
<EuiHighlight search={searchValue}>{option.label}</EuiHighlight>
<EuiText size="xs" color="subdued" className="eui-displayBlock">
<small>
<EuiHighlight search={searchValue}>
{option.secondaryContent || ''}
</EuiHighlight>
</small>
</EuiText>
</>
);
};

const [useCustomContent, setUseCustomContent] = useState(true);
const [isVirtualized, setIsVirtualized] = useState(true);

return (
<>
<EuiSwitch
label="Virtualized"
checked={isVirtualized}
onChange={(e) => setIsVirtualized(e.target.checked)}
/>
&emsp;&emsp;
<EuiSwitch
label="Custom content"
checked={useCustomContent}
onChange={(e) => setUseCustomContent(e.target.checked)}
/>
<EuiSpacer />
<EuiSelectable
aria-label="Selectable example with custom list items"
searchable
options={options}
onChange={(options) => setOptions(options)}
listProps={{
isVirtualized,
rowHeight: useCustomContent ? 50 : undefined,
showIcons: false,
}}
renderOption={useCustomContent ? renderCountryOption : undefined}
height={240}
>
{(list, search) => (
<>
{search}
{list}
</>
)}
</EuiSelectable>
</>
);
};
Loading