@@ -60,6 +60,7 @@ export interface PortableTextMemberItem {
60
60
*/
61
61
export function PortableTextInput ( props : PortableTextInputProps ) {
62
62
const {
63
+ editorRef : editorRefProp ,
63
64
elementProps,
64
65
hotkeys,
65
66
markers = EMPTY_ARRAY ,
@@ -78,8 +79,12 @@ export function PortableTextInput(props: PortableTextInputProps) {
78
79
} = props
79
80
80
81
const { onBlur} = elementProps
82
+ const defaultEditorRef = useRef < PortableTextEditor | null > ( null )
83
+ const editorRef = editorRefProp || defaultEditorRef
81
84
82
- // Make the PTE focusable from the outside
85
+ // This handle will allow for natively calling .focus
86
+ // on the returned component and have the PortableTextEditor focused,
87
+ // simulating a native input element (like with an string input)
83
88
useImperativeHandle ( elementProps . ref , ( ) => ( {
84
89
focus ( ) {
85
90
if ( editorRef . current ) {
@@ -89,7 +94,6 @@ export function PortableTextInput(props: PortableTextInputProps) {
89
94
} ) )
90
95
91
96
const { subscribe} = usePatches ( { path} )
92
- const editorRef = useRef < PortableTextEditor | null > ( null )
93
97
const [ ignoreValidationError , setIgnoreValidationError ] = useState ( false )
94
98
const [ invalidValue , setInvalidValue ] = useState < InvalidValue | null > ( null )
95
99
const [ isFullscreen , setIsFullscreen ] = useState ( false )
@@ -110,17 +114,15 @@ export function PortableTextInput(props: PortableTextInputProps) {
110
114
const innerElementRef = useRef < HTMLDivElement | null > ( null )
111
115
112
116
const handleToggleFullscreen = useCallback ( ( ) => {
113
- if ( editorRef . current ) {
114
- setIsFullscreen ( ( v ) => {
115
- const next = ! v
116
- if ( next ) {
117
- telemetry . log ( PortableTextInputExpanded )
118
- } else {
119
- telemetry . log ( PortableTextInputCollapsed )
120
- }
121
- return next
122
- } )
123
- }
117
+ setIsFullscreen ( ( v ) => {
118
+ const next = ! v
119
+ if ( next ) {
120
+ telemetry . log ( PortableTextInputExpanded )
121
+ } else {
122
+ telemetry . log ( PortableTextInputCollapsed )
123
+ }
124
+ return next
125
+ } )
124
126
} , [ telemetry ] )
125
127
126
128
// Reset invalidValue if new value is coming in from props
@@ -253,7 +255,7 @@ export function PortableTextInput(props: PortableTextInputProps) {
253
255
PortableTextEditor . focus ( editorRef . current )
254
256
}
255
257
}
256
- } , [ isActive ] )
258
+ } , [ editorRef , isActive ] )
257
259
258
260
return (
259
261
< Box ref = { innerElementRef } >
@@ -262,10 +264,10 @@ export function PortableTextInput(props: PortableTextInputProps) {
262
264
< PortableTextMarkersProvider markers = { markers } >
263
265
< PortableTextMemberItemsProvider memberItems = { portableTextMemberItems } >
264
266
< PortableTextEditor
265
- ref = { editorRef }
266
267
patches$ = { patches$ }
267
268
onChange = { handleEditorChange }
268
269
maxBlocks = { undefined } // TODO: from schema?
270
+ ref = { editorRef }
269
271
readOnly = { isOffline || readOnly }
270
272
schemaType = { schemaType }
271
273
value = { value }
0 commit comments