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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | 2x 2x 54x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 25x 25x 25x 70x 70x 70x 66x 70x 66x 70x 70x 70x 25x 2x 2x 2x 2x 1x 25x 2x 2x 2x 1x | import _, { omit } from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import {
findTypes,
StandardProps,
Overwrite,
} from '../../util/component-types';
import { buildModernHybridComponent } from '../../util/state-management';
import * as reducers from './VerticalListMenu.reducers';
import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon';
import Collapsible, { ICollapsibleProps } from '../Collapsible/Collapsible';
const cx = lucidClassNames.bind('&-VerticalListMenu');
const { func, arrayOf, bool, string, number, node, object, shape } = PropTypes;
export interface IVerticalListMenuPropsRaw extends StandardProps {
/** Indicates which of the \`VerticalListMenu.Item\` children are currently
selected. You can also put the \`isSelected\` prop directly on the
\`VerticalListMenu.Item\`s if you wish. */
selectedIndices: number[];
/** Indicates which of the \`VerticalListMenu.Item\` children are currently
expanded. You can also put the \`isExpanded\` prop directly on the
\`VerticalListMenu.Item\`s if you wish. */
expandedIndices: number[];
/** Callback fired when the user selects a \`VerticalListMenu.Item\`.*/
onSelect: (
index: number,
{
event,
props,
}: {
event: React.MouseEvent;
props: IVerticalListMenuItemProps;
}
) => void;
/** Callback fired when the user expands or collapses an expandable
\`VerticalListMenu.Item\`. */
onToggle: (
index: number,
{
event,
props,
}: {
event: React.MouseEvent;
props: IVerticalListMenuItemProps;
}
) => void;
}
export type IVerticalListMenuProps = Overwrite<
React.DetailedHTMLProps<
React.HTMLAttributes<HTMLUListElement>,
HTMLUListElement
>,
IVerticalListMenuPropsRaw
>;
interface IVerticalListMenuItemPropsRaw extends StandardProps {
/**
* Show or hide the expand button. Should be \`true\` if you want to nest menus.
*/
hasExpander?: boolean;
/**
* Determines the visibility of nested menus.
*/
isExpanded?: boolean;
/**
* If \`true\` then a small bar on the left side of the item will be
* shown indicating this item is selected.
*/
isSelected?: boolean;
/**
* Determines the visibility of the small bar on the left when the user
* hovers over the item. This should indicate to the user that an item
* is clickable.
*/
isActionable?: boolean;
/** Called when the user clicks the main body of the item. */
onSelect?: (
index: number,
{
event,
props,
}: {
event: React.MouseEvent;
props: IVerticalListMenuItemProps;
}
) => void;
/** Called when the user clicks the expand button. */
onToggle?: (
index: number,
{
event,
props,
}: {
event: React.MouseEvent;
props: IVerticalListMenuItemProps;
}
) => void;
/** Props that are passed through to the underlying Collapsible component
if the item has children. */
Collapsible?: Partial<ICollapsibleProps>;
}
export type IVerticalListMenuItemProps = Overwrite<
React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>,
IVerticalListMenuItemPropsRaw
>;
const Item = (_props: IVerticalListMenuItemProps): null => null;
Item.peek = {
description: `
A child item that can contain content or another VerticalListMenu.
`,
};
Item.displayName = 'VerticalListMenu.Item';
Item.propTypes = {
/**
Show or hide the expand button. Should be \`true\` if you want to
nest menus.
*/
hasExpander: bool,
/**
Determines the visibility of nested menus.
*/
isExpanded: bool,
/**
If \`true\` then a small bar on the left side of the item will be
shown indicating this item is selected.
*/
isSelected: bool,
/**
Determines the visibility of the small bar on the left when the user
hovers over the item. This should indicate to the user that an item
is clickable.
*/
isActionable: bool,
/**
Called when the user clicks the main body of the item. Signature:
\`(index, { event, props}) => {}\`
*/
onSelect: func,
/**
Called when the user clicks the expand button. Signature:
\`(index, { event, props}) => {}\`
*/
onToggle: func,
/**
Props that are passed through to the underlying Collapsible component
if the item has children.
*/
Collapsible: shape(Collapsible.propTypes),
};
export interface IVerticalListMenuState {
selectedIndices?: number[];
expandedIndices?: number[];
}
const defaultProps = {
onSelect: _.noop,
onToggle: _.noop,
expandedIndices: [],
selectedIndices: [],
};
class VerticalListMenu extends React.Component<
IVerticalListMenuProps,
IVerticalListMenuState
> {
static displayName = 'VerticalListMenu';
static Item = Item;
static peek = {
description: `Used primarily for navigation lists. It supports nesting \`VerticalListMenu\`s below \`VerticalListMenu.Item\`s and animating expanding of those sub lists. The default reducer behavior is for only one \`VerticalListMenu.Item\` to be selected at any given time; that default is easily overridden by handling \`onSelect\` yourself.`,
categories: ['navigation'],
madeFrom: ['ChevronIcon'],
};
static reducers = reducers;
// TODO: remove this once we move to only buildModernHybridComponent
static definition = {
statics: {
Item,
reducers,
peek: {
description: `Used primarily for navigation lists. It supports nesting \`VerticalListMenu\`s below \`VerticalListMenu.Item\`s and animating expanding of those sub lists. The default reducer behavior is for only one \`VerticalListMenu.Item\` to be selected at any given time; that is easily overridden by handling \`onSelect\` yourself.`,
categories: ['navigation'],
madeFrom: ['ChevronIcon'],
},
},
};
static propTypes = {
/**
Regular \`children\` aren't really used in this component, but if you do
add them they will be placed at the end of the component. You should be
using \`VerticalListMenu.Item\`s instead of regular children.
*/
children: node,
/**
Appended to the component-specific class names set on the root element.
*/
className: string,
/**
Passed through to the root element.
*/
style: object,
/**
Indicates which of the \`VerticalListMenu.Item\` children are currently
selected. You can also put the \`isSelected\` prop directly on the
\`VerticalListMenu.Item\`s if you wish.
*/
selectedIndices: arrayOf(number),
/**
Indicates which of the \`VerticalListMenu.Item\` children are currently
expanded. You can also put the \`isExpanded\` prop directly on the
\`VerticalListMenu.Item\`s if you wish.
*/
expandedIndices: arrayOf(number),
/**
Callback fired when the user selects a \`VerticalListMenu.Item\`.
Signature: \`(index, { event, props }) => {}\`
*/
onSelect: func,
/**
Callback fired when the user expands or collapses an expandable
\`VerticalListMenu.Item\`. Signature:
\`(index, { event, props }) => {}\`
*/
onToggle: func,
};
static defaultProps = defaultProps;
render() {
const {
children,
className,
style,
selectedIndices,
expandedIndices,
...passThroughs
} = this.props;
const itemChildProps = _.map(
findTypes(this.props, VerticalListMenu.Item),
'props'
);
return (
<ul
{...(omit(passThroughs, [
'children',
'className',
'style',
'selectedIndices',
'expandedIndices',
'onSelect',
'onToggle',
'initialState',
'callbackId',
]) as any)}
className={cx('&', className)}
style={style}
>
{_.map(itemChildProps, (itemChildProp, index) => {
const {
hasExpander = false,
isActionable = true,
Collapsible: collapsibleProps = Collapsible.defaultProps,
} = itemChildProp;
const itemChildrenAsArray = React.Children.toArray(
itemChildProp.children
);
// Was not able to get `child.Type` to work correctly, I suspect this
// is due to the way we wrap components with createLucidComponentDefinition
const listChildren = _.filter(
itemChildrenAsArray,
(child) =>
_.get(child, 'type.displayName', '') === 'VerticalListMenu'
);
const otherChildren = _.filter(
itemChildrenAsArray,
(child) =>
_.get(child, 'type.displayName', '') !== 'VerticalListMenu'
);
// If the prop is found on the child, it should override what was
// passed in at the top level for selectedIndices and expandedIndices
const actualIsExpanded = _.has(itemChildProp, 'isExpanded')
? _.get(itemChildProp, 'isExpanded', true)
: _.includes(expandedIndices, index);
const actualIsSelected = _.has(itemChildProp, 'isSelected')
? _.get(itemChildProp, 'isSelected', false)
: _.includes(selectedIndices, index);
return (
<li
key={index}
{...itemChildProp.passThroughs}
className={cx('&-Item', itemChildProp.className)}
>
<div
className={cx('&-Item-content', {
'&-Item-content-is-selected': actualIsSelected,
'&-Item-content-is-not-selected': !actualIsSelected,
'&-Item-content-is-expanded': actualIsExpanded,
'&-Item-content-is-actionable': isActionable,
})}
onClick={_.partial(this.handleClickItem, index, itemChildProp)}
>
<div className={cx('&-Item-content-body')}>
<div className={cx('&-Item-content-text')}>
{otherChildren}
</div>
{hasExpander ? (
<div
className={cx('&-Item-expander')}
onClick={_.partial(
this.handleToggle,
index,
itemChildProp
)}
>
<ChevronIcon
size={12}
direction={actualIsExpanded ? 'up' : 'down'}
/>
</div>
) : null}
</div>
</div>
{!_.isEmpty(listChildren) ? (
<Collapsible
{...collapsibleProps}
className={cx('&-Item-nested-list')}
isExpanded={actualIsExpanded}
>
{listChildren}
</Collapsible>
) : null}
</li>
);
})}
{children}
</ul>
);
}
handleToggle = (
index: number,
itemChildProp: IVerticalListMenuItemProps,
event: React.MouseEvent
) => {
const { onToggle } = itemChildProp;
// Prevent the user from also selecting the current item.
event.stopPropagation();
this.props.onToggle(index, { event, props: itemChildProp });
if (onToggle) {
onToggle(index, { event, props: itemChildProp });
}
};
handleClickItem = (
index: number,
itemChildProp: IVerticalListMenuItemProps,
event: React.MouseEvent
) => {
const { onSelect } = itemChildProp;
this.props.onSelect(index, { event, props: itemChildProp });
if (onSelect) {
onSelect(index, { event, props: itemChildProp });
}
};
}
export default buildModernHybridComponent<
IVerticalListMenuProps,
IVerticalListMenuState,
typeof VerticalListMenu
>(VerticalListMenu as any, { reducers });
export { VerticalListMenu as VerticalListMenuDumb };
|