|
| 1 | +import { Accessibility, listBehavior } from '@fluentui/accessibility' |
| 2 | +import * as customPropTypes from '@fluentui/react-proptypes' |
| 3 | +import * as _ from 'lodash' |
| 4 | +import * as React from 'react' |
| 5 | +import * as PropTypes from 'prop-types' |
| 6 | + |
| 7 | +import { |
| 8 | + childrenExist, |
| 9 | + UIComponentProps, |
| 10 | + ChildrenComponentProps, |
| 11 | + commonPropTypes, |
| 12 | + rtlTextContainer, |
| 13 | +} from '../../utils' |
| 14 | +import ListItem, { ListItemProps } from './ListItem' |
| 15 | +import { |
| 16 | + WithAsProp, |
| 17 | + ComponentEventHandler, |
| 18 | + withSafeTypeForAs, |
| 19 | + ShorthandCollection, |
| 20 | +} from '../../types' |
| 21 | +import { createManager, ManagerFactory } from '@fluentui/state' |
| 22 | +import { |
| 23 | + getElementType, |
| 24 | + getUnhandledProps, |
| 25 | + useAccessibility, |
| 26 | + useStateManager, |
| 27 | +} from '@fluentui/react-bindings' |
| 28 | +import useStyles from './useStyles' |
| 29 | +import { useListProvider } from './ListContext' |
| 30 | + |
| 31 | +export interface ListSlotClassNames { |
| 32 | + item: string |
| 33 | +} |
| 34 | + |
| 35 | +export interface ListProps extends UIComponentProps, ChildrenComponentProps { |
| 36 | + /** Accessibility behavior if overridden by the user. */ |
| 37 | + accessibility?: Accessibility |
| 38 | + |
| 39 | + /** Toggle debug mode */ |
| 40 | + debug?: boolean |
| 41 | + |
| 42 | + /** Shorthand array of props for ListItem. */ |
| 43 | + items?: ShorthandCollection<ListItemProps> |
| 44 | + |
| 45 | + /** A selectable list formats list items as possible choices. */ |
| 46 | + selectable?: boolean |
| 47 | + |
| 48 | + /** A navigable list allows user to navigate through items. */ |
| 49 | + navigable?: boolean |
| 50 | + |
| 51 | + /** Index of the currently selected item. */ |
| 52 | + selectedIndex?: number |
| 53 | + |
| 54 | + /** Initial selectedIndex value. */ |
| 55 | + defaultSelectedIndex?: number |
| 56 | + |
| 57 | + /** |
| 58 | + * Event for request to change 'selectedIndex' value. |
| 59 | + * @param event - React's original SyntheticEvent. |
| 60 | + * @param data - All props and proposed value. |
| 61 | + */ |
| 62 | + onSelectedIndexChange?: ComponentEventHandler<ListProps> |
| 63 | + |
| 64 | + /** Truncates content */ |
| 65 | + truncateContent?: boolean |
| 66 | + |
| 67 | + /** Truncates header */ |
| 68 | + truncateHeader?: boolean |
| 69 | + |
| 70 | + /** A horizontal list displays elements horizontally. */ |
| 71 | + horizontal?: boolean |
| 72 | +} |
| 73 | + |
| 74 | +export interface ListState { |
| 75 | + selectedIndex?: number |
| 76 | +} |
| 77 | + |
| 78 | +type ListActions = { |
| 79 | + select: (index: number) => void |
| 80 | +} |
| 81 | + |
| 82 | +type ListComponent = React.FC<WithAsProp<ListProps>> & { |
| 83 | + className: string |
| 84 | + slotClassNames: ListSlotClassNames |
| 85 | + |
| 86 | + Item: typeof ListItem |
| 87 | +} |
| 88 | + |
| 89 | +const createListManager: ManagerFactory<ListState, ListActions> = config => |
| 90 | + createManager<ListState, ListActions>({ |
| 91 | + ...config, |
| 92 | + actions: { |
| 93 | + select: index => () => ({ selectedIndex: index }), |
| 94 | + }, |
| 95 | + state: { |
| 96 | + selectedIndex: -1, |
| 97 | + ...config.state, |
| 98 | + }, |
| 99 | + }) |
| 100 | + |
| 101 | +const List: ListComponent = props => { |
| 102 | + const { children, selectable, navigable, horizontal, items } = props |
| 103 | + |
| 104 | + const ElementType = getElementType(props) |
| 105 | + const unhandledProps = getUnhandledProps(Object.keys(List.propTypes) as any, props) |
| 106 | + |
| 107 | + const [state, actions] = useStateManager(createListManager, { |
| 108 | + mapPropsToInitialState: () => ({ |
| 109 | + selectedIndex: props.defaultSelectedIndex, |
| 110 | + }), |
| 111 | + mapPropsToState: () => ({ |
| 112 | + selectedIndex: props.selectedIndex, |
| 113 | + }), |
| 114 | + }) |
| 115 | + const getA11Props = useAccessibility(props.accessibility, { |
| 116 | + debugName: List.displayName, |
| 117 | + mapPropsToBehavior: () => ({ |
| 118 | + selectable, |
| 119 | + navigable, |
| 120 | + horizontal, |
| 121 | + }), |
| 122 | + }) |
| 123 | + const [classes] = useStyles(List.displayName, { |
| 124 | + className: List.className, |
| 125 | + mapPropsToStyles: () => props, |
| 126 | + }) |
| 127 | + |
| 128 | + const [Provider, value] = useListProvider({ |
| 129 | + debug: props.debug, |
| 130 | + selectable: props.selectable, |
| 131 | + navigable: props.navigable, |
| 132 | + truncateContent: props.truncateContent, |
| 133 | + truncateHeader: props.truncateHeader, |
| 134 | + variables: props.variables, |
| 135 | + |
| 136 | + onItemClick: (e, index) => { |
| 137 | + if (selectable) { |
| 138 | + actions.select(index) |
| 139 | + _.invoke(props, 'onSelectedIndexChange', e, { |
| 140 | + ...props, |
| 141 | + selectedIndex: index, |
| 142 | + }) |
| 143 | + } |
| 144 | + }, |
| 145 | + selectedIndex: state.selectedIndex, |
| 146 | + }) |
| 147 | + |
| 148 | + return ( |
| 149 | + <ElementType |
| 150 | + {...getA11Props('root', { |
| 151 | + className: classes.root, |
| 152 | + ...rtlTextContainer.getAttributes({ forElements: [children] }), |
| 153 | + ...unhandledProps, |
| 154 | + })} |
| 155 | + > |
| 156 | + <Provider value={value}> |
| 157 | + {childrenExist(children) ? children : _.map(items, ListItem.create)} |
| 158 | + </Provider> |
| 159 | + </ElementType> |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +List.displayName = 'List' |
| 164 | +List.className = 'ui-list' |
| 165 | +List.slotClassNames = { |
| 166 | + item: `${List.className}__item`, |
| 167 | +} |
| 168 | +List.propTypes = { |
| 169 | + ...commonPropTypes.createCommon({ |
| 170 | + content: false, |
| 171 | + }), |
| 172 | + debug: PropTypes.bool, |
| 173 | + items: customPropTypes.collectionShorthand, |
| 174 | + selectable: customPropTypes.every([customPropTypes.disallow(['navigable']), PropTypes.bool]), |
| 175 | + navigable: customPropTypes.every([customPropTypes.disallow(['selectable']), PropTypes.bool]), |
| 176 | + truncateContent: PropTypes.bool, |
| 177 | + truncateHeader: PropTypes.bool, |
| 178 | + selectedIndex: PropTypes.number, |
| 179 | + defaultSelectedIndex: PropTypes.number, |
| 180 | + onSelectedIndexChange: PropTypes.func, |
| 181 | + horizontal: PropTypes.bool, |
| 182 | +} as any |
| 183 | +List.defaultProps = { |
| 184 | + as: 'ul', |
| 185 | + accessibility: listBehavior as Accessibility, |
| 186 | +} |
| 187 | + |
| 188 | +List.Item = ListItem |
| 189 | + |
| 190 | +/** |
| 191 | + * A List displays a group of related sequential items. |
| 192 | + * |
| 193 | + * @accessibility |
| 194 | + * List may follow one of the following accessibility semantics: |
| 195 | + * - Static non-navigable list. Implements [ARIA list](https://www.w3.org/TR/wai-aria-1.1/#list) role. |
| 196 | + * - Selectable list: allows the user to select item from a list of choices. Implements [ARIA Listbox](https://www.w3.org/TR/wai-aria-practices-1.1/#Listbox) design pattern. |
| 197 | + */ |
| 198 | +export default withSafeTypeForAs<typeof List, ListProps, 'ul'>(List) |
0 commit comments