-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add useQueryEditor
hook to @graphiql/react
#2408
Changes from all commits
2094bb0
ebac24b
ccd6b78
523b0db
f8821c1
8b3dee7
33f674f
d227964
c60f809
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'graphiql': minor | ||
'@graphiql/react': minor | ||
--- | ||
|
||
Move the logic of the query editor from the `graphiql` package into a hook `useQueryEditor` provided by `@graphiql/react` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const base = require('../../jest.config.base')(__dirname); | ||
|
||
module.exports = { | ||
...base, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { invalidCharacters, normalizeWhitespace } from '../whitespace'; | ||
|
||
describe('normalizeWhitespace', () => { | ||
it('removes unicode characters', () => { | ||
const result = normalizeWhitespace(invalidCharacters.join('')); | ||
expect(result).toEqual(' '.repeat(invalidCharacters.length)); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
import type { Editor } from 'codemirror'; | ||
import { createContext, ReactNode, useState } from 'react'; | ||
|
||
import { CodeMirrorEditor } from './types'; | ||
|
||
export type EditorContextType = { | ||
headerEditor: Editor | null; | ||
setHeaderEditor(newEditor: Editor): void; | ||
headerEditor: CodeMirrorEditor | null; | ||
queryEditor: CodeMirrorEditor | null; | ||
setHeaderEditor(newEditor: CodeMirrorEditor): void; | ||
setQueryEditor(newEditor: CodeMirrorEditor): void; | ||
}; | ||
|
||
export const EditorContext = createContext<EditorContextType>({ | ||
headerEditor: null, | ||
queryEditor: null, | ||
setHeaderEditor() {}, | ||
setQueryEditor() {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just out of curiosity - why are these empty functions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypeScript wants you to provide a default value when creating a context. When actually rendering the context provider we'll pass in the actual value that contains the functions to set the editors in state. |
||
}); | ||
|
||
export function EditorContextProvider(props: { | ||
children: ReactNode; | ||
initialValue?: string; | ||
}) { | ||
const [editor, setEditor] = useState<Editor | null>(null); | ||
const [headerEditor, setHeaderEditor] = useState<CodeMirrorEditor | null>( | ||
null, | ||
); | ||
const [queryEditor, setQueryEditor] = useState<CodeMirrorEditor | null>(null); | ||
return ( | ||
<EditorContext.Provider | ||
value={{ headerEditor: editor, setHeaderEditor: setEditor }}> | ||
value={{ headerEditor, queryEditor, setHeaderEditor, setQueryEditor }}> | ||
{props.children} | ||
</EditorContext.Provider> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { EditorContext, EditorContextProvider } from './context'; | ||
import { useHeaderEditor } from './header-editor'; | ||
import { useQueryEditor } from './query-editor'; | ||
|
||
import type { EditorContextType } from './context'; | ||
import type { UseHeaderEditorArgs } from './header-editor'; | ||
import type { UseQueryEditorArgs } from './query-editor'; | ||
|
||
export { EditorContext, EditorContextProvider, useHeaderEditor }; | ||
export { | ||
EditorContext, | ||
EditorContextProvider, | ||
useHeaderEditor, | ||
useQueryEditor, | ||
}; | ||
|
||
export type { EditorContextType, UseHeaderEditorArgs }; | ||
export type { EditorContextType, UseHeaderEditorArgs, UseQueryEditorArgs }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to make sure the builds are executed in correct order since we use Vite for
@graphiql/react
:@graphiql/react
graphiql