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 | 44x 510x 510x 510x 510x 510x 255x 255x 255x 203x 35x 35x 203x 203x 6x 203x 510x 510x 510x 510x 510x 44x | // @flow strict
import React, {
useRef,
useEffect,
useMemo,
useReducer,
useImperativeHandle,
} from 'react';
import cx from 'classnames';
import type { ElementType } from 'react';
import { useRipple } from '../ripple';
import { mtAttrsReducer, mtClassNamesReducer } from '../reducers';
import type { Style } from '../mt-types';
type Props = {
as?: ElementType,
className?: string,
style?: Style,
role?: 'option' | 'checkbox' | 'radio' | 'menuitem',
disabled?: boolean,
};
const ListItem = React.forwardRef<Props, _>(
(
{
as: Component = 'li',
className,
style,
role,
disabled = false,
...rest
}: Props,
ref,
) => {
const rootRef = useRef();
const [mtAttrs, mtAttrsDistpach] = useReducer(mtAttrsReducer, {});
const [mtClassName, mtClassNameDispatch] = useReducer(
mtClassNamesReducer,
new Set<string>(),
);
const { rippleStyle, rippleClassName } = useRipple({
activator: rootRef,
});
useEffect(() => {
const root = rootRef.current;
mtAttrsDistpach({ type: 'set', name: 'tabIndex', value: -1 });
if (root) {
root.setMtAttribute = (attr, value) => {
const name = { tabindex: 'tabIndex' }[attr] || attr;
mtAttrsDistpach({ type: 'set', name, value });
};
root.removeMtAttribute = attr => {
const name = { tabindex: 'tabIndex' }[attr] || attr;
mtAttrsDistpach({ type: 'unset', name });
};
root.addMtClassName = mClassName => {
mtClassNameDispatch({ type: 'add', className: mClassName });
};
root.removeMtClassName = mClassName => {
mtClassNameDispatch({ type: 'remove', className: mClassName });
};
}
}, [Component]);
useImperativeHandle(ref, () => rootRef.current, []);
const memoizedStyle = useMemo(() => ({ ...style, ...rippleStyle }), [
style,
rippleStyle,
]);
const memoizedClassName = useMemo(
() =>
cx(
'mdc-list-item',
className,
...Array.from(rippleClassName),
...Array.from(mtClassName),
{
'mdc-list-item--disabled': disabled,
},
),
[className, disabled, mtClassName, rippleClassName],
);
return (
<Component
{...(rest: mixed)}
{...mtAttrs}
role={role}
ref={rootRef}
style={memoizedStyle}
className={memoizedClassName}
/>
);
},
);
ListItem.displayName = 'ListItem';
export default ListItem;
|