generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from eea/develop
Release
- Loading branch information
Showing
7 changed files
with
208 additions
and
11 deletions.
There are no files selected for viewing
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
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
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,6 +1,6 @@ | ||
{ | ||
"name": "@eeacms/volto-call-to-action-block", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "@eeacms/volto-call-to-action-block: Volto add-on", | ||
"main": "src/index.js", | ||
"author": "European Environment Agency: IDM2 A-Team", | ||
|
@@ -16,10 +16,9 @@ | |
"type": "git", | ||
"url": "[email protected]:eea/volto-call-to-action-block.git" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@plone/scripts": "*", | ||
"@cypress/code-coverage": "^3.10.0", | ||
"@plone/scripts": "*", | ||
"babel-plugin-transform-class-properties": "^6.24.1", | ||
"md5": "^2.3.0" | ||
}, | ||
|
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
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
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 |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/** | ||
* CallToActionWidget component. | ||
* @module components/Widget | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import { compose } from 'redux'; | ||
import PropTypes from 'prop-types'; | ||
import { Input, Button } from 'semantic-ui-react'; | ||
import { FormFieldWrapper, Icon } from '@plone/volto/components'; | ||
import { | ||
addAppURL, | ||
isInternalURL, | ||
flattenToAppURL, | ||
URLUtils, | ||
} from '@plone/volto/helpers'; | ||
import withObjectBrowser from '@plone/volto/components/manage/Sidebar/ObjectBrowser'; | ||
import clearSVG from '@plone/volto/icons/clear.svg'; | ||
import navTreeSVG from '@plone/volto/icons/nav.svg'; | ||
|
||
/** Widget to edit urls | ||
* | ||
* This is the default widget used for the `remoteUrl` field. You can also use | ||
* it by declaring a field like: | ||
* | ||
* ```jsx | ||
* { | ||
* title: "URL", | ||
* widget: 'url', | ||
* } | ||
* ``` | ||
*/ | ||
export const CallToActionWidget = (props) => { | ||
const { | ||
id, | ||
onChange, | ||
onBlur, | ||
onClick, | ||
minLength, | ||
maxLength, | ||
placeholder, | ||
isDisabled, | ||
} = props; | ||
const inputId = `field-${id}`; | ||
|
||
const [value, setValue] = useState( | ||
flattenToAppURL(props.value?.[0]?.['@id'] || props.value), | ||
); | ||
const [isInvalid, setIsInvalid] = useState(false); | ||
/** | ||
* Clear handler | ||
* @method clear | ||
* @param {Object} value Value | ||
* @returns {undefined} | ||
*/ | ||
const clear = () => { | ||
setValue(''); | ||
onChange(id, undefined); | ||
}; | ||
|
||
const onChangeValue = (_value) => { | ||
let newValue = _value; | ||
if (newValue?.length > 0) { | ||
if (isInvalid && URLUtils.isUrl(URLUtils.normalizeUrl(newValue))) { | ||
setIsInvalid(false); | ||
} | ||
|
||
if (isInternalURL(newValue)) { | ||
newValue = flattenToAppURL(newValue); | ||
} | ||
} | ||
|
||
setValue(newValue); | ||
|
||
newValue = isInternalURL(newValue) ? addAppURL(newValue) : newValue; | ||
|
||
if (!isInternalURL(newValue) && newValue.length > 0) { | ||
const checkedURL = URLUtils.checkAndNormalizeUrl(newValue); | ||
newValue = checkedURL.url; | ||
if (!checkedURL.isValid) { | ||
setIsInvalid(true); | ||
} | ||
} | ||
|
||
onChange(id, newValue === '' ? undefined : newValue); | ||
}; | ||
|
||
return ( | ||
<FormFieldWrapper {...props} className="url wide"> | ||
<div className="wrapper"> | ||
<Input | ||
id={inputId} | ||
name={id} | ||
type="url" | ||
value={value || ''} | ||
disabled={isDisabled} | ||
placeholder={placeholder || 'mailto: tel: href:...'} | ||
onChange={({ target }) => onChangeValue(target.value)} | ||
onBlur={({ target }) => | ||
onBlur(id, target.value === '' ? undefined : target.value) | ||
} | ||
onClick={() => onClick()} | ||
minLength={minLength || null} | ||
maxLength={maxLength || null} | ||
error={isInvalid} | ||
/> | ||
{value?.length > 0 ? ( | ||
<Button.Group> | ||
<Button | ||
basic | ||
className="cancel" | ||
aria-label="clearUrlBrowser" | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
clear(); | ||
}} | ||
> | ||
<Icon name={clearSVG} size="30px" /> | ||
</Button> | ||
</Button.Group> | ||
) : ( | ||
<Button.Group> | ||
<Button | ||
basic | ||
icon | ||
aria-label="openUrlBrowser" | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
props.openObjectBrowser({ | ||
mode: 'link', | ||
overlay: true, | ||
onSelectItem: (url) => { | ||
onChangeValue(url); | ||
}, | ||
}); | ||
}} | ||
> | ||
<Icon name={navTreeSVG} size="24px" /> | ||
</Button> | ||
</Button.Group> | ||
)} | ||
</div> | ||
</FormFieldWrapper> | ||
); | ||
}; | ||
|
||
/** | ||
* Property types | ||
* @property {Object} propTypes Property types. | ||
* @static | ||
*/ | ||
CallToActionWidget.propTypes = { | ||
id: PropTypes.string.isRequired, | ||
title: PropTypes.string.isRequired, | ||
description: PropTypes.string, | ||
required: PropTypes.bool, | ||
error: PropTypes.arrayOf(PropTypes.string), | ||
onChange: PropTypes.func.isRequired, | ||
onBlur: PropTypes.func, | ||
onClick: PropTypes.func, | ||
minLength: PropTypes.number, | ||
maxLength: PropTypes.number, | ||
openObjectBrowser: PropTypes.func.isRequired, | ||
placeholder: PropTypes.string, | ||
}; | ||
|
||
/** | ||
* Default properties. | ||
* @property {Object} defaultProps Default properties. | ||
* @static | ||
*/ | ||
CallToActionWidget.defaultProps = { | ||
description: null, | ||
required: false, | ||
error: [], | ||
value: null, | ||
onChange: () => {}, | ||
onBlur: () => {}, | ||
onClick: () => {}, | ||
minLength: null, | ||
maxLength: null, | ||
}; | ||
|
||
export default compose(withObjectBrowser)(CallToActionWidget); |
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