All files / src/list-examples WithActivedItem.js

90.91% Statements 10/11
100% Branches 3/3
66.67% Functions 2/3
90.91% Lines 10/11

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                    2x                                                                                   2x                           2x   2x 4x   4x       4x       4x       4x                 16x                      
// @flow strict
import React, { useCallback, useState } from 'react';
import cx from 'classnames';
 
import List from '../material-toolbox/list';
import ListItem from '../material-toolbox/list/Item';
import ListItemDetail from '../material-toolbox/list/ItemDetail';
 
import Example from '../Example';
 
const source = `import React, { useCallback, useState } from 'react';
import cx from 'classnames';
 
import List from 'material-toolbox/list';
import ListItem from 'material-toolbox/list/Item';
import ListItemDetail from 'material-toolbox/list/ItemDetail';
 
const menuItems = ['inbox', 'star', 'send', 'drafts'];
 
const WithActivedItem = ({ shaped = false }) => {
  const [selected, setSelected] = useState();
 
  const handleSelected = useCallback(index => {
    setSelected(index);
  }, []);
 
  const title = shaped
    ? 'List with shaped activated item'
    : 'List with activated item';
 
  const className = cx('demo-list', {
    'demo-list-item-shaped': shaped,
  });
 
  return (
    <List
      selected={selected}
      onSelected={handleSelected}
      className={className}
      role="listbox">
      {menuItems.map(item => (
        <ListItem role="option" data-item={item} key={item}>
          <ListItemDetail className="material-icons">{item}</ListItemDetail>
          {item}
        </ListItem>
      ))}
    </List>
  );
};
 
export default WithActivedItem;`;
 
const scssSource = `.demo-list-item-shaped .mdc-list-item {
  border-radius: 0 32px 32px 0;
}
 
[dir="rtl"] .demo-list-item-shaped .mdc-list-item,
.demo-list-item-shaped .mdc-list-item[dir="rtl"] {
  border-radius: 32px 0 0 32px;
}
`;
 
type Props = {
  shaped?: boolean,
};
 
const menuItems = ['inbox', 'star', 'send', 'drafts'];
 
const WithActivedItem = ({ shaped = false }: Props) => {
  const [selected, setSelected] = useState();
 
  const handleSelected = useCallback(index => {
    setSelected(index);
  }, []);
 
  const title = shaped
    ? 'List with shaped activated item'
    : 'List with activated item';
 
  const className = cx('demo-list', {
    'demo-list-item-shaped': shaped,
  });
 
  return (
    <Example title={title} source={source} scss={scssSource}>
      <List
        selected={selected}
        onSelected={handleSelected}
        className={className}
        role="listbox"
      >
        {menuItems.map(item => (
          <ListItem role="option" data-item={item} key={item}>
            <ListItemDetail className="material-icons">{item}</ListItemDetail>
            {item}
          </ListItem>
        ))}
      </List>
    </Example>
  );
};
 
export default WithActivedItem;