-
-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use context to make unitary list style renderers
- Loading branch information
Showing
4 changed files
with
100 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/render-html/src/context/ListStyleSepcsProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* eslint-disable react-native/no-inline-styles */ | ||
import { MarkerBoxProps } from '@jsamr/react-native-li'; | ||
import { mapObjIndexed } from 'ramda'; | ||
import React, { | ||
createContext, | ||
PropsWithChildren, | ||
useContext, | ||
useMemo | ||
} from 'react'; | ||
import { Text, View } from 'react-native'; | ||
import defaultListStyleSpecs from '../elements/defaultListStyleSpecs'; | ||
import { ListStyleSpec, UnitaryListStyleSpec } from '../shared-types'; | ||
import { useSharedProps } from './SharedPropsProvider'; | ||
|
||
const listStyleSpecsContext = createContext< | ||
Record< | ||
string, | ||
ListStyleSpec & { renderMarker?: (props: MarkerBoxProps) => any } | ||
> | ||
>(defaultListStyleSpecs); | ||
|
||
export function useListStyleSpecs() { | ||
return useContext(listStyleSpecsContext); | ||
} | ||
|
||
function createSymbolicMarkerRenderer({ | ||
Component, | ||
counterStyleRenderer | ||
}: UnitaryListStyleSpec) { | ||
const prefix = counterStyleRenderer.renderPrefix(); | ||
const suffix = counterStyleRenderer.renderSuffix(); | ||
return ({ | ||
style, | ||
markerTextStyle, | ||
counterIndex, | ||
rtlMarkerReversed | ||
}: MarkerBoxProps) => { | ||
return ( | ||
<View | ||
style={[ | ||
style, | ||
{ | ||
flexDirection: rtlMarkerReversed ? 'row-reverse' : 'row', | ||
justifyContent: 'flex-end' | ||
} | ||
]}> | ||
{!!prefix && <Text style={markerTextStyle}>{prefix}</Text>} | ||
{React.createElement(Component, { | ||
...(markerTextStyle as any), | ||
index: counterIndex | ||
})} | ||
{!!suffix && <Text style={markerTextStyle}>{suffix}</Text>} | ||
</View> | ||
); | ||
}; | ||
} | ||
|
||
const makeMarkerRenderers = mapObjIndexed((value: ListStyleSpec) => { | ||
if (value.type === 'unitary') { | ||
return { | ||
...value, | ||
renderMarker: createSymbolicMarkerRenderer(value) | ||
}; | ||
} | ||
return value; | ||
}); | ||
|
||
export default function ListStyleSpecsProvider({ | ||
children | ||
}: PropsWithChildren<{}>) { | ||
const { customListStyleSpecs } = useSharedProps(); | ||
const mergedListStyleSpecs = useMemo(() => { | ||
return makeMarkerRenderers( | ||
customListStyleSpecs != null | ||
? { ...defaultListStyleSpecs, ...customListStyleSpecs } | ||
: defaultListStyleSpecs | ||
); | ||
}, [customListStyleSpecs]); | ||
return ( | ||
<listStyleSpecsContext.Provider value={mergedListStyleSpecs}> | ||
{children} | ||
</listStyleSpecsContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters