All files / DropMenu DropMenu.stories.tsx

74.28% Statements 52/70
50% Branches 6/12
45.45% Functions 15/33
81.25% Lines 52/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298                                                    1x 1x       1x   1x       1x                 1x               6x             1x 1x       1x   1x   1x       1x                   3x             1x 1x   1x                     1x         1x 1x   1x       1x   1x       1x               3x                 1x 1x   1x                                                         1x 1x       1x   1x   1x       1x                   3x             1x 1x       1x   1x   1x     1x                 4x             1x 1x           1x   1x       1x       1x                       3x                   1x 1x   1x                                                  
import React, { useState } from 'react';
import _ from 'lodash';
import { Meta, Story } from '@storybook/react';
 
import DropMenu, { IDropMenuProps } from './DropMenu';
import TextField from '../TextField/TextField';
import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon';
import Button from '../Button/Button';
 
export default {
	title: 'Helpers/DropMenu',
	component: DropMenu,
	parameters: {
		docs: {
			description: {
				component: DropMenu.peek.description,
			},
		},
		layout: 'centered',
	},
	argTypes: {
		children: { control: false },
	},
} as Meta;
 
/* Basic */
export const Basic: Story<IDropMenuProps> = (args) => {
	const [selectedIndices, setSelectedIndices] = useState<Array<number | null>>(
		[]
	);
 
	const { Control, Option } = DropMenu;
 
	const handleSelect = (optionIndex: any) => {
		setSelectedIndices((selectedIndices) => [...selectedIndices, optionIndex]);
	};
 
	const options = [
		'MS-DOS',
		'Windows',
		'OS/2',
		'Windows Phone',
		'Windows Mobile',
		'Pocket PC',
	];
 
	return (
		<DropMenu {...args} onSelect={(i) => handleSelect(i)}>
			<Control>
				{_.isEmpty(selectedIndices)
					? 'Select OS'
					: options[_.last(selectedIndices) as any]}
			</Control>
			{_.map(options, (optionText, index) => (
				<Option key={'Option-' + index}>{optionText}</Option>
			))}
		</DropMenu>
	);
};
 
/* Button Menu */
export const ButtonMenu: Story<IDropMenuProps> = (args) => {
	const [selectedIndices, setSelectedIndices] = useState<Array<number | null>>(
		[]
	);
 
	const { Control, Option } = DropMenu;
 
	const options = ['Red', 'Green', 'Blue'];
 
	const handleSelect = (optionIndex: any) => {
		setSelectedIndices((selectedIndices) => [...selectedIndices, optionIndex]);
	};
 
	return (
		<DropMenu {...args} onSelect={(i) => handleSelect(i)}>
			<Control>
				<Button style={{ minWidth: '90px' }}>
					{_.isEmpty(selectedIndices)
						? 'Select Color'
						: options[_.last(selectedIndices) as any]}
				</Button>
			</Control>
			{_.map(options, (optionText, index) => (
				<Option key={'Option-' + index}>{optionText}</Option>
			))}
		</DropMenu>
	);
};
 
/* Disabled Control */
export const DisabledControl: Story<IDropMenuProps> = (args) => {
	const { Control, Option } = DropMenu;
 
	return (
		<DropMenu {...args}>
			<Control>
				<Button tabIndex={-1}>Select Color</Button>
			</Control>
			<Option>Red</Option>
			<Option>Green</Option>
			<Option>Blue</Option>
		</DropMenu>
	);
};
DisabledControl.args = {
	isDisabled: true,
};
 
/* Disabled Options */
export const DisabledOptions: Story<IDropMenuProps> = (args) => {
	const { Control, Option } = DropMenu;
 
	const [selectedIndices, setSelectedIndices] = useState<Array<number | null>>(
		[]
	);
 
	const options = ['Red', 'Green', 'Blue'];
 
	const handleSelect = (optionIndex: any) => {
		setSelectedIndices((selectedIndices) => [...selectedIndices, optionIndex]);
	};
 
	return (
		<DropMenu {...args} onSelect={(i) => handleSelect(i)}>
			<Control>
				{_.isEmpty(selectedIndices)
					? 'Select Color'
					: options[_.last(selectedIndices) as any]}
			</Control>
			{_.map(options, (optionText, index) => (
				<Option key={'Option-' + index} isDisabled>
					{optionText}
				</Option>
			))}
		</DropMenu>
	);
};
 
/* Grouped Options */
export const GroupedOptions: Story<IDropMenuProps> = (args) => {
	const { Control, Option, OptionGroup } = DropMenu;
 
	return (
		<DropMenu {...args}>
			<Control>
				<Button>Select Color</Button>
			</Control>
 
			<OptionGroup>
				<Option>Select Color</Option>
			</OptionGroup>
 
			<OptionGroup>
				Screen
				<Option>Red</Option>
				<Option>Green</Option>
				<Option>Blue</Option>
			</OptionGroup>
 
			<OptionGroup>
				Print
				<Option>Cyan</Option>
				<Option>Yellow</Option>
				<Option>Magenta</Option>
				<Option>Black</Option>
			</OptionGroup>
		</DropMenu>
	);
};
 
/* Text Menu */
export const TextMenu: Story<IDropMenuProps> = (args) => {
	const [selectedIndices, setSelectedIndices] = useState<Array<number | null>>(
		[]
	);
 
	const { Control, Option } = DropMenu;
 
	const options = ['Red', 'Green', 'Blue'];
 
	const handleSelect = (optionIndex: any) => {
		setSelectedIndices((selectedIndices) => [...selectedIndices, optionIndex]);
	};
 
	return (
		<DropMenu {...args} onSelect={(i) => handleSelect(i)}>
			<Control>
				{_.isEmpty(selectedIndices) ? (
					<TextField placeholder='Text DropMenu' />
				) : (
					options[_.last(selectedIndices) as any]
				)}
			</Control>
			{_.map(options, (optionText, index) => (
				<Option key={'Option-' + index}>{optionText}</Option>
			))}
		</DropMenu>
	);
};
 
/* Action Menu */
export const ActionMenu: Story<IDropMenuProps> = (args) => {
	const [selectedIndices, setSelectedIndices] = useState<Array<number | null>>(
		[]
	);
 
	const { Control, Option } = DropMenu;
 
	const options = ['Select Color', 'Red', 'Green', 'Blue'];
 
	const handleSelect = (optionIndex: any) => {
		setSelectedIndices((selectedIndices) => [...selectedIndices, optionIndex]);
	};
	return (
		<DropMenu {...args} onSelect={(i) => handleSelect(i)}>
			<Control>
				{_.isEmpty(selectedIndices)
					? 'Select Color'
					: options[_.last(selectedIndices) as any]}
				<ChevronIcon direction='down' style={{ marginLeft: '5px' }} />
			</Control>
			{_.map(options, (optionText, index) => (
				<Option key={'Option-' + index}>{optionText}</Option>
			))}
		</DropMenu>
	);
};
 
/* No Wrapping */
export const NoWrapping: Story<IDropMenuProps> = (args) => {
	const options = [
		'Intentionally run off screen -- Adipisicing totam saepe officia repellat quo cupiditate ducimus hic? Quod temporibus corrupti eaque ullam quo nulla corporis !',
		'Adipisicing totam provident excepturi officia non cum alias? Labore possimus adipisci id eveniet numquam tempora totam est. Explicabo recusandae quo tempore',
		'Consectetur doloribus dignissimos exercitationem vel tempora praesentium nostrum eveniet inventore. Odit inventore quas optio id eum nisi. Minima consequuntur',
	];
 
	const { Control, Option } = DropMenu;
 
	const [selectedIndices, setSelectedIndices] = useState<Array<number | null>>(
		[]
	);
 
	const handleSelect = (optionIndex: any) => {
		setSelectedIndices((selectedIndices) => [...selectedIndices, optionIndex]);
	};
 
	return (
		<div style={{ textAlign: 'right' }}>
			<DropMenu {...args} onSelect={(i) => handleSelect(i)} alignment='center'>
				<Control>
					<Button>
						{_.isEmpty(selectedIndices)
							? 'Click Me'
							: options[_.last(selectedIndices) as any]}
					</Button>
				</Control>
 
				{_.map(options, (optionText, index) => (
					<Option isWrapped={false} key={'Option-' + index}>
						{optionText}
					</Option>
				))}
			</DropMenu>
		</div>
	);
};
 
/* Stateless */
export const Stateless: Story<IDropMenuProps> = (args) => {
	const { Control, Option, OptionGroup } = DropMenu;
 
	return (
		<DropMenu
			{...args}
			selectedIndices={[0]}
			focusedIndex={3}
			isExpanded
			direction='down'
		>
			<Control>
				<Button>Select Color</Button>
			</Control>
 
			<OptionGroup>
				Preferred
				<Option>Red</Option>
				<Option>Green</Option>
				<Option>Blue</Option>
			</OptionGroup>
 
			<Option>Orange</Option>
			<Option isDisabled>Violet</Option>
			<Option isDisabled>Brown</Option>
		</DropMenu>
	);
};