Skip to content

Commit b034492

Browse files
authored
Merge pull request #18 from imdevan/example-code-snippets
2 parents e3571f5 + f189d9a commit b034492

File tree

6 files changed

+675
-74
lines changed

6 files changed

+675
-74
lines changed

examples/babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module.exports = function (api) {
22
api.cache(true);
33
return {
44
presets: ['babel-preset-expo'],
5+
plugins: ['react-native-reanimated/plugin'],
56
};
67
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
import React, { useState } from 'react';
3+
import {
4+
LayoutChangeEvent,
5+
Pressable,
6+
StyleSheet,
7+
Text,
8+
View,
9+
useColorScheme,
10+
} from 'react-native';
11+
import Animated, {
12+
useAnimatedStyle,
13+
useSharedValue,
14+
withTiming,
15+
} from 'react-native-reanimated';
16+
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
17+
import { ghcolors, tomorrow } from 'react-syntax-highlighter/dist/esm/styles/prism';
18+
19+
interface CodeSnippetProps {
20+
code: string;
21+
}
22+
23+
export default function CodeSnippet({ code }: CodeSnippetProps): JSX.Element {
24+
const colorScheme = useColorScheme();
25+
const backgroundColor = colorScheme === 'dark' ? '#333' : '#f5f5f5';
26+
const color = colorScheme === 'dark' ? '#fff' : '#222';
27+
const syntaxStyle = colorScheme === 'dark' ? tomorrow : ghcolors;
28+
29+
const [isExpanded, setIsExpanded] = useState(false);
30+
const height = useSharedValue(0);
31+
const contentHeight = useSharedValue(0);
32+
33+
const animatedStyle = useAnimatedStyle(() => ({
34+
height: height.value,
35+
}));
36+
37+
const toggle = () => {
38+
height.value = withTiming(isExpanded ? 0 : contentHeight.value, {
39+
duration: 300,
40+
});
41+
setIsExpanded(!isExpanded);
42+
};
43+
44+
const onLayout = (event: LayoutChangeEvent) => {
45+
contentHeight.value = event.nativeEvent.layout.height;
46+
};
47+
48+
return (
49+
<View style={[styles.container, { backgroundColor }]}>
50+
<Pressable onPress={toggle} style={styles.header}>
51+
<Text style={[styles.headerText, { color }]}>
52+
{isExpanded ? 'Hide Code' : 'Show Code'}
53+
</Text>
54+
</Pressable>
55+
<Animated.View style={animatedStyle}>
56+
<View onLayout={onLayout} style={styles.codeContainer}>
57+
<SyntaxHighlighter language='jsx' style={syntaxStyle}>
58+
{code}
59+
</SyntaxHighlighter>
60+
</View>
61+
</Animated.View>
62+
</View>
63+
);
64+
}
65+
66+
const styles = StyleSheet.create({
67+
container: {
68+
marginVertical: 10,
69+
borderRadius: 5,
70+
overflow: 'hidden',
71+
},
72+
header: {
73+
padding: 10,
74+
},
75+
headerText: {
76+
fontWeight: 'bold',
77+
},
78+
codeContainer: {
79+
position: 'absolute',
80+
top: 0,
81+
left: 0,
82+
right: 0,
83+
},
84+
});

examples/example-src-files/example.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import DropDownPicker, {
66
ItemType,
77
ValueType,
88
} from 'react-native-dropdown-picker';
9+
import CodeSnippet from './CodeSnippet';
910

1011
// Omit these types from the example props
1112
type CommonOmitKeys = 'setValue' | 'value' | 'open' | 'items' | 'setOpen';
@@ -23,6 +24,7 @@ type MultipleDropdownProps<T extends ValueType> = Omit<
2324
export interface ExampleProps {
2425
title: string;
2526
description?: string;
27+
code?: string;
2628
placeholder?: string;
2729
multipleText?: string;
2830
items?: Array<ItemType<ValueType>>;
@@ -154,6 +156,31 @@ export default function DropDownPickerExample({
154156
else setSingleValue(null);
155157
}}
156158
/>
159+
160+
<CodeSnippet
161+
code={`<DropDownPicker
162+
${dropdownProps ?
163+
Object.entries(dropdownProps || {})
164+
.map(([key, value]) => {
165+
if (typeof value === 'string') {
166+
return ` ${key}='${value}'`;
167+
}
168+
return ` ${key}={${JSON.stringify(value)}}`;
169+
})
170+
.join('\n') + '\n\n' : ''} /* Boilerplate */
171+
open={open}
172+
items={items}
173+
setOpen={setOpen}
174+
setItems={setItems}
175+
theme={theme}
176+
placeholder={placeholder}
177+
${isMultiple ?
178+
` setValue={setMultiValue}
179+
value={multiValue}` :
180+
` setValue={setSingleValue}
181+
value={singleValue}`}
182+
/>`}
183+
/>
157184
</View>
158185
</View>
159186
);

0 commit comments

Comments
 (0)