import { useState } from 'react';
import { Command } from '../../src';
export default {
component: Command,
title: 'Tests behavior',
};
const fileOptions = [
{ id: 'opt-new-file', label: 'New file' },
{ id: 'opt-open-file', label: 'Open file' },
{ id: 'opt-save-file', label: 'Save file' },
];
const manyOptions = Array.from({ length: 40 }, (_, idx) => ({ id: `opt-${idx + 1}`, label: `Option ${idx + 1}` }));
export const KeyboardNavigation = () => (
{ fileOptions.map((opt) => (
{ opt.label }
)) }
);
export const OnSelect = () => {
const [selected, setSelected] = useState('');
return (
<>
{ selected }
{ fileOptions.map((opt) => (
setSelected(opt.id) }>
{ opt.label }
)) }
>
);
};
export const ConsumerKeyDown = () => {
const [lastKey, setLastKey] = useState('');
return (
<>
{ lastKey }
setLastKey(e.key) }>
{ fileOptions.map((opt) => (
{ opt.label }
)) }
>
);
};
export const Filter = () => (
New file
Save file
Copy
No results found.
);
export const ControlledFilter = () => {
const [filterValue, setFilterValue] = useState('');
const allOptions = [
{ id: 'opt-new-file', label: 'New file' },
{ id: 'opt-open-file', label: 'Open file' },
{ id: 'opt-copy', label: 'Copy' },
{ id: 'opt-paste', label: 'Paste' },
];
const visible = allOptions.filter((opt) =>
!filterValue || opt.label.toLowerCase().includes(filterValue.toLowerCase()),
);
return (
setFilterValue(e.target.value) }
placeholder="Search..."
value={ filterValue }
/>
{ visible.map((opt) => (
{ opt.label }
)) }
No results found.
);
};
export const DisabledOption = () => {
const [selected, setSelected] = useState('');
return (
<>
{ selected }
setSelected('enabled') }>
Enabled option
setSelected('disabled') }>
Disabled option
No results found.
>
);
};
export const OpenClose = () => (
Open command
{ fileOptions.map((opt) => (
{ opt.label }
)) }
);
export const Groups = () => {
const [selected, setSelected] = useState('');
return (
<>
{ selected }
setSelected('new-file') }>New file
setSelected('open-file') }>Open file
setSelected('copy') }>Copy
setSelected('paste') }>Paste
No results found.
>
);
};
export const ManyOptions = () => (
{ manyOptions.map((opt) => (
{ opt.label }
)) }
);