@contentful/react-apps-toolkit 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -22
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -4,9 +4,9 @@ This library is still in development and should not be used in production.
|
|
|
4
4
|
|
|
5
5
|
# React Toolkit for Contentful Apps
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
React Hooks for the App Framework offer a simple way to bring frequently needed functionality into your react based [Contentful apps](/developers/docs/extensibility/app-framework/).
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
They can be used in apps created with [`create-contentful-app`](https://www.npmjs.com/package/@contentful/create-contentful-app), as well as any other React app using Contentful's [App SDK](https://www.npmjs.com/package/@contentful/app-sdk).
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
@@ -20,52 +20,115 @@ yarn add @contentful/react-apps-toolkit
|
|
|
20
20
|
|
|
21
21
|
The following hooks and utilities are exported from the package:
|
|
22
22
|
|
|
23
|
-
###
|
|
23
|
+
### SDKProvider
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
The `SDKProvider` is a wrapper component, which automatically makes the Contentful [App SDK](https://www.npmjs.com/package/@contentful/app-sdk) available to any child components using React Context. To use any of the hooks contained in this package, they must be wrapped in the `<SDKProvider>`, because all of the hooks depend on the App SDK.
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
If it is not, the hook will throw an error.
|
|
29
|
-
|
|
30
|
-
Here is an example of how you can use it:
|
|
27
|
+
Usage:
|
|
31
28
|
|
|
32
29
|
```tsx
|
|
33
|
-
import { useSDK } from '@contentful/react-apps-toolkit'
|
|
30
|
+
import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';
|
|
34
31
|
|
|
35
|
-
function
|
|
32
|
+
function ChildComponentUsingHook() {
|
|
36
33
|
const sdk = useSDK<FieldExtensionSDK>();
|
|
37
34
|
|
|
38
|
-
return <>App Id: {sdk.ids.app}
|
|
35
|
+
return <>App Id: {sdk.ids.app}</>;
|
|
39
36
|
}
|
|
40
37
|
|
|
38
|
+
function App() {
|
|
39
|
+
return (
|
|
40
|
+
<SDKProvider>
|
|
41
|
+
<ChildComponentUsingHook />
|
|
42
|
+
</SDKProvider>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
41
45
|
```
|
|
42
46
|
|
|
43
|
-
###
|
|
47
|
+
### useSDK
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
`useSDK` returns an instance of the Contentful [App SDK](https://www.npmjs.com/package/@contentful/app-sdk).
|
|
46
50
|
|
|
51
|
+
It must be wrapped it within the `SDKProvider`, otherwise, it will throw an error.
|
|
52
|
+
|
|
53
|
+
Usage:
|
|
47
54
|
|
|
48
55
|
```tsx
|
|
49
|
-
import {
|
|
56
|
+
import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';
|
|
57
|
+
|
|
58
|
+
function ComponentUsingSDK() {
|
|
59
|
+
const sdk = useSDK<FieldExtensionSDK>();
|
|
60
|
+
|
|
61
|
+
return <>App Id: {sdk.ids.app}</>;
|
|
62
|
+
}
|
|
50
63
|
|
|
51
64
|
function App() {
|
|
65
|
+
return (
|
|
66
|
+
<SDKProvider>
|
|
67
|
+
<ChildComponentUsingSDK />
|
|
68
|
+
</SDKProvider>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### useCMA
|
|
74
|
+
|
|
75
|
+
`useCMA` returns an initialized [client for the Contentful Management API](https://www.npmjs.com/package/contentful-management). This can be used immediately to communicate with the environment the app is rendered in. [Contentful Management API docs](/developers/docs/references/content-management-api/).
|
|
76
|
+
|
|
77
|
+
**Note**: The CMA client instance returned by this hook is automatically scoped to the contentful space and environment in which it is called.
|
|
78
|
+
|
|
79
|
+
Usage:
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
import { SDKProvider, useCMA } from '@contentful/react-apps-toolkit';
|
|
83
|
+
|
|
84
|
+
function ComponentUsingCMA() {
|
|
52
85
|
const cma = useCMA();
|
|
86
|
+
const [entries, setEntries] = useState();
|
|
53
87
|
|
|
54
88
|
useEffect(() => {
|
|
55
|
-
cma.
|
|
56
|
-
|
|
57
|
-
});
|
|
58
|
-
}, []);
|
|
89
|
+
cma.entries.getMany().then(setEntries);
|
|
90
|
+
}, [cma]);
|
|
59
91
|
|
|
60
|
-
return <>
|
|
92
|
+
return <>{entries?.length}</>;
|
|
61
93
|
}
|
|
62
94
|
|
|
95
|
+
function App() {
|
|
96
|
+
return (
|
|
97
|
+
<SDKProvider>
|
|
98
|
+
<ComponentUsingCMA />
|
|
99
|
+
</SDKProvider>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
63
102
|
```
|
|
64
103
|
|
|
65
|
-
|
|
104
|
+
### useFieldValue
|
|
105
|
+
|
|
106
|
+
`useFieldValue` provides the current value, and a setter function for updating the current value, of a given field in Contentful. If used in the [field location](/developers/docs/extensibility/app-framework/locations/#entry-field), it will initialize using the current field id by default.
|
|
107
|
+
|
|
108
|
+
If used in the [entry sidebar location](/developers/docs/extensibility/app-framework/locations/#entry-sidebar), or the [entry editor location](/developers/docs/extensibility/app-framework/locations/#entry-editor), it must be passed a field ID to initialize.
|
|
109
|
+
|
|
110
|
+
`useFieldValue` also optionally accepts a locale, if the field has multiple locales. If no locale is passed, it will use the environment's default locale.
|
|
111
|
+
|
|
112
|
+
Usage:
|
|
66
113
|
|
|
67
|
-
|
|
114
|
+
```tsx
|
|
115
|
+
import { SDKProvider, useFieldValue } from '@contentful/react-apps-toolkit';
|
|
116
|
+
|
|
117
|
+
function ComponentUsingFieldValue() {
|
|
118
|
+
const [value, setValue] = useFieldValue('slug', 'en-US');
|
|
119
|
+
|
|
120
|
+
return <input value={value} onChange={(e) => setValue(e.target.value)} />;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function App() {
|
|
124
|
+
return (
|
|
125
|
+
<SDKProvider>
|
|
126
|
+
<ComponentUsingFieldValue />
|
|
127
|
+
</SDKProvider>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
68
131
|
|
|
69
132
|
### Resources
|
|
70
133
|
|
|
71
|
-
- [
|
|
134
|
+
- [create-contentful-app](https://www.npmjs.com/package/create-contentful-app): A starter that makes it easy to bootstrap apps for Contentful.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/react-apps-toolkit",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Toolkit for building a Contentful app in React",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "Contentful GmbH",
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"@types/react": "^17.0.39",
|
|
41
41
|
"jest": "^27.5.0",
|
|
42
42
|
"react": "^17.0.2",
|
|
43
|
+
"react-test-renderer": "17.0.2",
|
|
43
44
|
"ts-jest": "^27.1.3",
|
|
44
45
|
"typescript": "^4.5.5"
|
|
45
46
|
},
|
|
@@ -50,5 +51,5 @@
|
|
|
50
51
|
"@contentful/app-sdk": "^4.0.0",
|
|
51
52
|
"contentful-management": ">=7.30.0"
|
|
52
53
|
},
|
|
53
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "92d4d8ae34401669f4548d408f7168837a30a539"
|
|
54
55
|
}
|