Skip to content

Commit c18d668

Browse files
committed
Syntax highlighting
1 parent f133a3f commit c18d668

File tree

4 files changed

+183
-27
lines changed

4 files changed

+183
-27
lines changed

articles/Array-fields.md

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# Array fields
2+
13
To create dynamic array fields, you should use the [`ArrayForm`](https://github.com/CodeStix/typed-react-form/wiki/ArrayForm) component or [`useArrayForm`](https://github.com/CodeStix/typed-react-form/wiki/useArrayForm) hook. These are wrappers around [`useChildForm`](https://github.com/CodeStix/typed-react-form/wiki/useChildForm) which provide useful functions and optimizations for arrays.
24

3-
- [ArrayForm](https://github.com/CodeStix/typed-react-form/wiki/ArrayForm)
4-
- [useArrayForm](https://github.com/CodeStix/typed-react-form/wiki/useArrayForm)
5+
- [ArrayForm](https://github.com/CodeStix/typed-react-form/wiki/ArrayForm)
6+
- [useArrayForm](https://github.com/CodeStix/typed-react-form/wiki/useArrayForm)
57

68
If you have an array field with a constant size, you should probably just use [`ChildForm`](https://github.com/CodeStix/typed-react-form/wiki/ChildForm). (See bottom for examples)
79

@@ -10,24 +12,27 @@ If you have an array field with a constant size, you should probably just use [`
1012
## Dynamic array examples
1113

1214
✔️ **Dynamic string array field using `ArrayForm`**
15+
1316
```jsx
1417
function NotesList() {
1518
const form = useForm({
16-
notes: ["Do the dishes", "Go outside", "Drink some water"]
19+
notes: ["Do the dishes", "Go outside", "Drink some water"],
1720
});
1821
return (
1922
<form
2023
onSubmit={(ev) => {
2124
ev.preventDefault();
2225
console.log("submit", form.values);
23-
}}
24-
>
26+
}}>
2527
<ArrayForm
2628
parent={form}
2729
name="notes"
2830
render={({ form, values, append, remove }) => (
2931
<>
30-
{values.map((_, i) => ( // You SHOULD use index as key. See top for info.
32+
{values.map((
33+
_,
34+
i // You SHOULD use index as key. See top for info.
35+
) => (
3136
<div key={i}>
3237
<FormInput form={form} name={i} />
3338
<button type="button" onClick={() => remove(i)}>
@@ -55,16 +60,15 @@ Remember: this is all type checked!
5560
function ShoppingListForm() {
5661
const form = useForm({
5762
title: "My shopping list",
58-
items: [{ name: "Coffee", amount: 1 }]
63+
items: [{ name: "Coffee", amount: 1 }],
5964
});
6065

6166
return (
6267
<form
6368
onSubmit={(ev) => {
6469
ev.preventDefault();
6570
console.log("submit", form.values);
66-
}}
67-
>
71+
}}>
6872
<h2>Title</h2>
6973
<FormInput form={form} type="text" name="title" />
7074

@@ -108,6 +112,7 @@ function ShoppingListForm() {
108112
```
109113

110114
✔️ **Dynamic object array field with seperate component for each child form and using `useArrayForm`**
115+
111116
```jsx
112117
interface ShoppingListItem {
113118
name: string;
@@ -119,17 +124,19 @@ interface ShoppingList {
119124
}
120125

121126
function ShoppingListForm() {
122-
const form = useForm<ShoppingList>({
123-
title: "My shopping list",
124-
items: [{ name: "Coffee", amount: 1 }]
125-
});
127+
const form =
128+
useForm <
129+
ShoppingList >
130+
{
131+
title: "My shopping list",
132+
items: [{ name: "Coffee", amount: 1 }],
133+
};
126134
return (
127135
<form
128136
onSubmit={(ev) => {
129137
ev.preventDefault();
130138
console.log("submit", form.values);
131-
}}
132-
>
139+
}}>
133140
<h2>Title</h2>
134141
<FormInput form={form} type="text" name="title" />
135142
<h2>Items</h2>
@@ -154,7 +161,7 @@ function ShoppingListItemsForm(props: { parent: FormState<ShoppingList> }) {
154161
);
155162
}
156163

157-
function ShoppingListItemForm(props: { parent: FormState<ShoppingListItem[]>; index: number; remove: (i: number) => void }) {
164+
function ShoppingListItemForm(props: { parent: FormState<ShoppingListItem[]>, index: number, remove: (i: number) => void }) {
158165
const form = useChildForm(props.parent, props.index);
159166
return (
160167
<div>
@@ -173,19 +180,19 @@ function ShoppingListItemForm(props: { parent: FormState<ShoppingListItem[]>; in
173180
A fixed array always has the same size, [`ChildForm`](https://github.com/CodeStix/typed-react-form/wiki/ChildForm) is used, and the index into the array is given using the name prop.
174181

175182
✔️ **Fixed array field containing strings**
183+
176184
```jsx
177185
function AnswerForm() {
178186
const form = useForm({
179187
// Always 3 items in array
180-
answers: ["", "", ""]
188+
answers: ["", "", ""],
181189
});
182190
return (
183191
<form
184192
onSubmit={(ev) => {
185193
ev.preventDefault();
186194
console.log("submit", form.values);
187-
}}
188-
>
195+
}}>
189196
<ChildForm
190197
parent={form}
191198
name="answers"
@@ -208,27 +215,27 @@ function AnswerForm() {
208215
```
209216

210217
✔️ **Fixed array field containing objects**
218+
211219
```jsx
212220
function SettingsForm() {
213221
const form = useForm({
214222
settings: [
215223
{ name: "Enable email", value: true },
216-
{ name: "Enable notifications", value: false }
217-
]
224+
{ name: "Enable notifications", value: false },
225+
],
218226
});
219227
return (
220228
<form
221229
onSubmit={(ev) => {
222230
ev.preventDefault();
223231
console.log("submit", form.values);
224-
}}
225-
>
232+
}}>
226233
<ChildForm // First child form is array
227234
parent={form}
228235
name="settings"
229236
render={(form) =>
230237
form.values.map((_, i) => (
231-
<ChildForm // Second child form is object in array
238+
<ChildForm // Second child form is object in array
232239
key={i}
233240
parent={form}
234241
name={i}
@@ -246,4 +253,4 @@ function SettingsForm() {
246253
</form>
247254
);
248255
}
249-
```
256+
```

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
"react": "17.0.1",
1313
"react-dom": "17.0.1",
1414
"react-markdown": "^5.0.3",
15+
"react-syntax-highlighter": "^15.4.3",
1516
"styled-components": "^5.2.1"
1617
},
1718
"devDependencies": {
1819
"@types/node": "^14.14.34",
1920
"@types/react": "^17.0.3",
21+
"@types/react-syntax-highlighter": "^13.5.0",
2022
"@types/styled-components": "^5.1.8",
2123
"typescript": "^4.2.3"
2224
}

pages/docs/[doc].tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { CenterContainer } from "../../components/CenterContainer";
77
import { NavBar } from "../../components/NavBar";
88
import { SideBar } from "../../components/SideBar";
99
import styled from "styled-components";
10+
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
11+
import { materialOceanic } from "react-syntax-highlighter/dist/cjs/styles/prism";
1012

1113
const ARTICLES_PATH = path.join(process.cwd(), "articles");
1214

@@ -30,7 +32,14 @@ export default function DocPage(props: Props) {
3032
<SideBar />
3133
</div>
3234
<div style={{ overflow: "hidden" }}>
33-
<ReactMarkdown>{props.content}</ReactMarkdown>;
35+
<ReactMarkdown
36+
renderers={{
37+
code: ({ language, value }) => {
38+
return <SyntaxHighlighter style={materialOceanic} language={language} children={value} />;
39+
},
40+
}}>
41+
{props.content}
42+
</ReactMarkdown>
3443
</div>
3544
</Container>
3645
</CenterContainer>
@@ -39,7 +48,8 @@ export default function DocPage(props: Props) {
3948
}
4049

4150
export const getStaticProps: GetStaticProps<Props> = async function (props) {
42-
let file = path.join(ARTICLES_PATH, props.params!.doc + ".md");
51+
let title = props.params!.doc as string;
52+
let file = path.join(ARTICLES_PATH, title + ".md");
4353
return {
4454
props: {
4555
content: await fs.readFile(file, "utf-8"),

0 commit comments

Comments
 (0)