-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: form builder page slug setting (#6942)
- Loading branch information
1 parent
e07af08
commit 94ae8bb
Showing
1 changed file
with
71 additions
and
48 deletions.
There are no files selected for viewing
119 changes: 71 additions & 48 deletions
119
src/FormBuilder/resources/js/form-builder/src/settings/form-summary/page-slug.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,83 @@ | ||
import {Button, Dropdown, ExternalLink, TextControl} from "@wordpress/components"; | ||
import {close, Icon} from "@wordpress/icons"; | ||
import {setFormSettings, useFormState, useFormStateDispatch} from "@givewp/form-builder/stores/form-state"; | ||
import {Button, Dropdown, ExternalLink, TextControl} from '@wordpress/components'; | ||
import {close, Icon} from '@wordpress/icons'; | ||
import {setFormSettings, useFormState, useFormStateDispatch} from '@givewp/form-builder/stores/form-state'; | ||
|
||
import {getWindowData} from "@givewp/form-builder/common"; | ||
import {getWindowData} from '@givewp/form-builder/common'; | ||
import {cleanForSlug, safeDecodeURIComponent} from '@wordpress/url'; | ||
import {useCallback, useState} from '@wordpress/element'; | ||
|
||
const { formPage: { isEnabled, permalink, rewriteSlug } } = getWindowData(); | ||
const { | ||
formPage: {isEnabled, permalink, rewriteSlug}, | ||
} = getWindowData(); | ||
|
||
/** | ||
* @since 3.0.0 | ||
* @see https://github.com/WordPress/gutenberg/blob/a8c5605f5dd077a601aefce6f58409f54d7d4447/packages/editor/src/components/post-slug/index.js | ||
*/ | ||
const PageSlugControl = () => { | ||
|
||
const { | ||
settings: {pageSlug}, | ||
} = useFormState(); | ||
const dispatch = useFormStateDispatch(); | ||
const [editedSlug, setEditedSlug] = useState(safeDecodeURIComponent(pageSlug)); | ||
|
||
const updateSlug = useCallback(() => { | ||
const cleanEditedSlug = cleanForSlug(editedSlug); | ||
setEditedSlug(cleanEditedSlug); | ||
|
||
if (cleanEditedSlug !== pageSlug) { | ||
dispatch(setFormSettings({pageSlug: cleanEditedSlug})); | ||
} | ||
}, [pageSlug, editedSlug, dispatch]); | ||
|
||
return !! isEnabled && <Dropdown | ||
className="my-container-class-name" | ||
contentClassName="givewp-sidebar-dropdown-content" | ||
popoverProps={ { placement: 'bottom-start' } } | ||
focusOnMount={"container"} | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<TextControl | ||
label={'URL'} | ||
value={'/donations/' + pageSlug} | ||
onChange={() => null} | ||
onClick={ onToggle } | ||
aria-expanded={ isOpen } | ||
/> | ||
) } | ||
renderContent={ ({onClose}) => ( | ||
<div style={{minWidth: 'calc(var(--givewp-sidebar-width) - 48px)'}}> | ||
<div style={{display: 'flex', justifyContent: 'space-between', alignItems: 'center'}}> | ||
<strong style={{fontSize: '14px'}}>{'URL'}</strong> | ||
<Button onClick={onClose}> | ||
<Icon icon={close} size={14}></Icon> | ||
</Button> | ||
</div> | ||
<TextControl | ||
label={'Permalink'} | ||
value={pageSlug} | ||
onChange={(pageSlug) => dispatch(setFormSettings({pageSlug}))} | ||
help={'The last part of the URL.'} | ||
/> | ||
<div> | ||
<strong style={{fontSize: '14px'}}>View Page</strong> | ||
</div> | ||
<ExternalLink href={permalink} style={{fontSize: '14px'}}> | ||
{sprintf('%s/%s', rewriteSlug, pageSlug)} | ||
</ExternalLink> | ||
</div> | ||
) } | ||
/> | ||
} | ||
return ( | ||
!!isEnabled && ( | ||
<Dropdown | ||
className="my-container-class-name" | ||
contentClassName="givewp-sidebar-dropdown-content" | ||
popoverProps={{placement: 'bottom-start'}} | ||
focusOnMount={'container'} | ||
renderToggle={({isOpen, onToggle}) => ( | ||
<TextControl | ||
label={'URL'} | ||
value={'/donations/' + editedSlug} | ||
onChange={() => null} | ||
onClick={onToggle} | ||
aria-expanded={isOpen} | ||
/> | ||
)} | ||
renderContent={({onClose}) => ( | ||
<div style={{minWidth: 'calc(var(--givewp-sidebar-width) - 48px)'}}> | ||
<div style={{display: 'flex', justifyContent: 'space-between', alignItems: 'center'}}> | ||
<strong style={{fontSize: '14px'}}>{'URL'}</strong> | ||
<Button onClick={onClose}> | ||
<Icon icon={close} size={14}></Icon> | ||
</Button> | ||
</div> | ||
<TextControl | ||
label={'Permalink'} | ||
value={editedSlug} | ||
autoComplete="off" | ||
spellCheck="false" | ||
onChange={(newPageSlug) => { | ||
setEditedSlug(newPageSlug); | ||
}} | ||
help={'The last part of the URL.'} | ||
onBlur={() => updateSlug()} | ||
/> | ||
<div> | ||
<strong style={{fontSize: '14px'}}>View Page</strong> | ||
</div> | ||
<ExternalLink href={permalink} style={{fontSize: '14px'}}> | ||
{sprintf('%s/%s', rewriteSlug, editedSlug)} | ||
</ExternalLink> | ||
</div> | ||
)} | ||
/> | ||
) | ||
); | ||
}; | ||
|
||
export default PageSlugControl; | ||
|
||
export { | ||
isEnabled as isFormPageEnabled, | ||
PageSlugControl, | ||
} | ||
export {isEnabled as isFormPageEnabled, PageSlugControl}; |