Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 0d29ba2

Browse files
committed
fixes and simplification
1 parent 9e087a5 commit 0d29ba2

File tree

4 files changed

+31
-66
lines changed

4 files changed

+31
-66
lines changed

docs/src/examples/components/List/Types/ListExample.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import { useBooleanKnob } from '@fluentui/docs-components'
2-
import { List, Image } from '@fluentui/react'
2+
import { ContextList as List, Image } from '@fluentui/react'
33
import * as React from 'react'
44

55
const ListExampleSelectable = () => {
66
const [debug] = useBooleanKnob({ name: 'debug' })
77

88
return (
9-
<List debug={debug}>
9+
<List defaultSelectedIndex={1} debug={debug} selectable>
1010
<List.Item
1111
media={<Image src="public/images/avatar/small/matt.jpg" avatar />}
1212
header="Irving Kuhic"
1313
headerMedia="7:26:56 AM"
1414
content="Program the sensor to the SAS alarm through the haptic SQL card!"
15+
index={0}
1516
/>
1617
<List.Item
1718
media={<Image src="public/images/avatar/small/steve.jpg" avatar />}
1819
header="Skyler Parks"
1920
headerMedia="11:30:17 PM"
2021
content="Use the online FTP application to input the multi-byte application!"
22+
index={1}
2123
/>
2224
<List.Item
2325
media={<Image src="public/images/avatar/small/nom.jpg" avatar />}
2426
header="Dante Schneider"
2527
headerMedia="5:22:40 PM"
2628
content="The GB pixel is down, navigate the virtual interface!"
29+
index={2}
2730
/>
2831
</List>
2932
)

packages/react/src/components/ContextList/List.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
useStateManager,
2727
} from '@fluentui/react-bindings'
2828
import useStyles from './useStyles'
29-
import { useListProvider } from './ListContext'
29+
import { ListContext } from './ListContext'
3030

3131
export interface ListSlotClassNames {
3232
item: string
@@ -125,15 +125,8 @@ const List: ListComponent = props => {
125125
mapPropsToStyles: () => props,
126126
})
127127

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) => {
128+
const onItemClick = React.useCallback(
129+
(e, index) => {
137130
if (selectable) {
138131
actions.select(index)
139132
_.invoke(props, 'onSelectedIndexChange', e, {
@@ -142,8 +135,20 @@ const List: ListComponent = props => {
142135
})
143136
}
144137
},
138+
[actions, selectable],
139+
)
140+
141+
const value = {
142+
debug: props.debug,
143+
selectable: props.selectable,
144+
navigable: props.navigable,
145+
truncateContent: props.truncateContent,
146+
truncateHeader: props.truncateHeader,
147+
variables: props.variables,
148+
149+
onItemClick,
145150
selectedIndex: state.selectedIndex,
146-
})
151+
}
147152

148153
return (
149154
<ElementType
@@ -153,9 +158,9 @@ const List: ListComponent = props => {
153158
...unhandledProps,
154159
})}
155160
>
156-
<Provider value={value}>
161+
<ListContext.Provider value={value}>
157162
{childrenExist(children) ? children : _.map(items, ListItem.create)}
158-
</Provider>
163+
</ListContext.Provider>
159164
</ElementType>
160165
)
161166
}
Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { ListItemProps } from '@fluentui/react'
12
import * as React from 'react'
3+
24
import { createContext, useContextSelector } from './context'
3-
import { ListItemProps } from '@fluentui/react'
45

56
type ListContextValue = {
67
debug: ListItemProps['variables']
@@ -10,55 +11,14 @@ type ListContextValue = {
1011
truncateHeader: ListItemProps['truncateHeader']
1112
variables: ListItemProps['variables']
1213

13-
onItemClick: (index: number) => void
14+
onItemClick: (e: React.MouseEvent, index: number) => void
1415
selectedIndex: number
1516
}
1617

17-
const ListContext = createContext<ListContextValue>(null)
18-
19-
export const useListProvider = props => {
20-
const registeredItems = React.useRef([])
21-
22-
const registerChild = React.useCallback(child => {
23-
let index = registeredItems.current.indexOf(child)
24-
25-
if (index === -1) {
26-
index = registeredItems.current.push(child) - 1
27-
}
28-
29-
return index
30-
}, [])
31-
32-
const unregisterChild = React.useCallback(child => {
33-
const index = registeredItems.current.indexOf(child)
34-
35-
registeredItems.current.splice(index, -1)
36-
}, [])
37-
38-
const value = {
39-
...props,
40-
registerChild,
41-
unregisterChild,
42-
}
43-
44-
return [ListContext.Provider, value]
45-
}
46-
47-
export const useListConsumer = () => {
48-
const ref = React.useRef(null)
18+
export const ListContext = createContext<ListContextValue>(null)
4919

50-
const registerChild = useContextSelector(ListContext, v => v.registerChild)
51-
const unregisterChild = useContextSelector(ListContext, v => v.unregisterChild)
52-
53-
const currentIndex = registerChild(ref)
54-
55-
React.useEffect(() => {
56-
return () => {
57-
unregisterChild(ref)
58-
}
59-
}, [unregisterChild])
60-
61-
const selected = useContextSelector(ListContext, v => v.selectedIndex === currentIndex)
20+
export const useListConsumer = (index: number) => {
21+
const selected = useContextSelector(ListContext, v => v.selectedIndex === index)
6222
const onClick = useContextSelector(ListContext, v => v.onItemClick)
6323
const debug = useContextSelector(ListContext, v => v.debug)
6424
const selectable = useContextSelector(ListContext, v => v.selectable)
@@ -67,19 +27,16 @@ export const useListConsumer = () => {
6727
const truncateHeader = useContextSelector(ListContext, v => v.truncateHeader)
6828
const variables = useContextSelector(ListContext, v => v.variables)
6929

70-
const selectedIndex = useContextSelector(ListContext, v => v.selectedIndex)
71-
7230
return {
7331
selected,
7432
onClick: e => {
75-
onClick(e, currentIndex)
33+
onClick(e, index)
7634
},
7735
debug,
7836
selectable,
7937
navigable,
8038
truncateContent,
8139
truncateHeader,
8240
variables,
83-
selectedIndex,
8441
}
8542
}

packages/react/src/components/ContextList/ListItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const ListItem: ListItemComponent = props => {
8787
const ElementType = getElementType(props)
8888
const unhandledProps = getUnhandledProps(Object.keys(ListItem.propTypes) as any, props)
8989

90-
const parentProps = useListConsumer()
90+
const parentProps = useListConsumer(props.index)
9191
const handleClick = (e: React.MouseEvent | React.KeyboardEvent) => {
9292
_.invoke(props, 'onClick', e, props)
9393
parentProps.onClick(e)

0 commit comments

Comments
 (0)