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

docs(react-listbox): Adding various Storybook entries for Listbox as a standalone component #31204

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from 'react';
import { Listbox, makeStyles, Option, shorthands } from '@fluentui/react-components';
import type { ListboxProps } from '@fluentui/react-components';

const useStyles = makeStyles({
root: {
// Stack the label above the field with a gap
display: 'grid',
gridTemplateRows: 'repeat(1fr)',
justifyItems: 'start',
...shorthands.gap('2px'),
maxWidth: '400px',
},
});

export const Default = (props: Partial<ListboxProps>) => {
const options = ['Cat', 'Dog', 'Ferret', 'Fish', 'Hamster', 'Snake'];

const styles = useStyles();
return (
<div className={styles.root}>
<Listbox {...props} aria-label="Select a pet">
{options.map(option => (
<Option key={option} disabled={option === 'Ferret'}>
{option}
</Option>
))}
</Listbox>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A listbox (`Listbox`) provides people a way to select an option from an inline list.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as React from 'react';
import { Button, Input, Listbox, makeStyles, mergeClasses, Option, shorthands } from '@fluentui/react-components';
import type { ListboxProps } from '@fluentui/react-components';
import { DismissRegular } from '@fluentui/react-icons';

const useStyles = makeStyles({
root: {
// Stack the label above the field with a gap
display: 'grid',
gridTemplateRows: 'repeat(1fr)',
justifyItems: 'start',
...shorthands.gap('2px'),
width: '200px',
},
input: {
width: '100%',
marginBottom: '8px',
},
listbox: {
width: '100%',
},
});

export const Filtering = (props: Partial<ListboxProps>) => {
const [filter, setFilter] = React.useState('');

const filteredOptions = React.useMemo(() => {
return ['Cat', 'Dog', 'Ferret', 'Fish', 'Hamster', 'Snake'].filter(
o => o.toLowerCase().includes(filter.toLowerCase()) || filter.toLowerCase().includes(o.toLowerCase()),
);
}, [filter]);

const styles = useStyles();
return (
<div className={styles.root}>
<Input
className={styles.input}
placeholder="Filter pets"
aria-label="Filter pets"
value={filter}
onChange={(_, data) => setFilter(data.value)}
contentAfter={
filter.length > 0 ? (
<Button
appearance="transparent"
icon={<DismissRegular />}
title="Clear filter"
onClick={() => setFilter('')}
/>
) : undefined
}
/>
<Listbox {...props} aria-label="Select a pet" className={mergeClasses(styles.listbox, props.className)}>
{filteredOptions.map(option => (
<Option key={option} disabled={option === 'Ferret'}>
{option}
</Option>
))}
</Listbox>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import * as React from 'react';
import {
Button,
Checkbox,
Dialog,
DialogActions,
DialogBody,
DialogContent,
DialogSurface,
DialogTitle,
DialogTrigger,
Input,
Listbox,
makeStyles,
MenuButton,
mergeClasses,
Option,
shorthands,
tokens,
} from '@fluentui/react-components';
import type { ListboxProps } from '@fluentui/react-components';
import { DismissRegular } from '@fluentui/react-icons';

const useStyles = makeStyles({
root: {
// Stack the label above the field with a gap
display: 'grid',
gridTemplateRows: 'repeat(1fr)',
justifyItems: 'start',
...shorthands.gap('2px'),
maxWidth: '400px',
},
menuButton: {
justifyContent: 'space-between',
},
listboxWrapper: {
maxHeight: '200px',
...shorthands.overflow('hidden', 'auto'),
},
inputWrapper: {
display: 'flex',
alignItems: 'center',
position: 'sticky',
top: '-4px',
backgroundColor: tokens.colorNeutralBackground1,
zIndex: 1,
...shorthands.margin('-4px', '-4px', '0px'),
...shorthands.padding('4px', '4px', '8px'),
},
input: {
width: '100%',
},
listbox: {
width: '100%',
},
option: {
scrollMarginBlockStart: '48px',
},
});

export const FilteringInDialog = (props: Partial<ListboxProps>) => {
const inputRef = React.useRef<HTMLInputElement>(null);
const [isMultiselect, setIsMultiselect] = React.useState(props.multiselect);
const [selectedOptions, setSelectedOptions] = React.useState(props.selectedOptions ?? []);
const [pendingSelectedOptions, setPendingSelectedOptions] = React.useState(props.selectedOptions ?? []);

const [dialogOpen, setDialogOpen] = React.useState(false);
const [filter, setFilter] = React.useState('');

const filteredOptions = React.useMemo(() => {
return ['Bear', 'Cat', 'Dog', 'Ferret', 'Fish', 'Hamster', 'Monkey', 'Parrot', 'Snake', 'Zebra'].filter(
o => o.toLowerCase().includes(filter.toLowerCase()) || filter.toLowerCase().includes(o.toLowerCase()),
);
}, [filter]);

React.useEffect(() => {
setSelectedOptions(props.selectedOptions ?? []);
}, [props.selectedOptions]);

React.useEffect(() => {
setSelectedOptions(selection => (selection[0] ? [selection[0]] : []));
}, [isMultiselect]);

React.useEffect(() => {
if (dialogOpen) {
inputRef.current?.focus({ preventScroll: true });
}
}, [dialogOpen]);

const styles = useStyles();
return (
<div className={styles.root}>
<Checkbox
label="Multiselect"
checked={isMultiselect}
onChange={(_, data) => setIsMultiselect(data.checked !== false)}
/>
<Dialog open={dialogOpen} onOpenChange={(_, data) => setDialogOpen(data.open)}>
<DialogTrigger disableButtonEnhancement>
<MenuButton
appearance="outline"
className={styles.menuButton}
onClick={() => setPendingSelectedOptions(selectedOptions)}
>
{selectedOptions.length > 0 ? selectedOptions.join(', ') : 'Select a pet'}
</MenuButton>
</DialogTrigger>
<DialogSurface>
<DialogBody>
<DialogTitle>Select a pet</DialogTitle>
<DialogContent className={styles.listboxWrapper}>
<div className={styles.inputWrapper}>
<Input
ref={inputRef}
className={styles.input}
placeholder="Filter pets"
aria-label="Filter pets"
autoFocus={false}
value={filter}
onChange={(_, data) => setFilter(data.value)}
contentAfter={
filter.length > 0 ? (
<Button
appearance="transparent"
icon={<DismissRegular />}
title="Clear filter"
onClick={() => setFilter('')}
/>
) : undefined
}
/>
</div>
<Listbox
{...props}
aria-label="Select a pet"
className={mergeClasses(styles.listbox, props.className)}
multiselect={isMultiselect}
selectedOptions={pendingSelectedOptions}
onOptionSelect={(e, data) => {
setPendingSelectedOptions(data.selectedOptions);
}}
>
{filteredOptions.map(option => (
<Option key={option} className={styles.option} disabled={option === 'Ferret'}>
{option}
</Option>
))}
</Listbox>
</DialogContent>
<DialogActions>
<DialogTrigger>
<Button>Cancel</Button>
</DialogTrigger>
<Button
appearance="primary"
onClick={() => {
setSelectedOptions(pendingSelectedOptions);
setDialogOpen(false);
}}
disabled={!isMultiselect && pendingSelectedOptions.length === 0}
>
Done
</Button>
</DialogActions>
</DialogBody>
</DialogSurface>
</Dialog>
</div>
);
};