-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
PreferencesEditorContextProvider.tsx
59 lines (55 loc) · 1.64 KB
/
PreferencesEditorContextProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as React from 'react';
import { ReactElement, useCallback, useMemo, useState } from 'react';
import {
PreferencesEditorContext,
PreferencesEditorContextValue,
} from './PreferencesEditorContext';
export const PreferencesEditorContextProvider = ({ children }) => {
const [isEnabled, setIsEnabled] = useState(false);
const [editor, setEditor] = useState<ReactElement>(null);
const [preferenceKey, setPreferenceKey] = useState<string>();
const [path, setPath] = useState<string>(null);
const [title, setTitleString] = useState<string>(null);
const [titleOptions, setTitleOptions] = useState<any>();
const enable = useCallback(() => setIsEnabled(true), []);
const disable = useCallback(() => {
setIsEnabled(false);
setEditor(null);
}, []);
const setTitle = useCallback((title: string, titleOptions?: any) => {
setTitleString(title);
setTitleOptions(titleOptions);
}, []);
const context = useMemo<PreferencesEditorContextValue>(() => {
return {
editor,
setEditor,
preferenceKey,
setPreferenceKey,
title,
titleOptions,
setTitle,
isEnabled,
disable,
enable,
path,
setPath,
};
}, [
disable,
enable,
editor,
preferenceKey,
isEnabled,
path,
setPath,
title,
titleOptions,
setTitle,
]);
return (
<PreferencesEditorContext.Provider value={context}>
{children}
</PreferencesEditorContext.Provider>
);
};