-
Notifications
You must be signed in to change notification settings - Fork 15
Override text and textarea widget to show warnings for soft_max_length #747
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
Open
Tishasoumya-02
wants to merge
7
commits into
main
Choose a base branch
from
seo-text-textarea-widget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
230372f
overide text and textarea widget to show warnings for softmaxlength
Tishasoumya-02 0d96912
fix lint
Tishasoumya-02 a234235
changelog
Tishasoumya-02 0399944
Update TextWidget.jsx
Tishasoumya-02 fc650f9
Update TextareaWidget.jsx
Tishasoumya-02 abf520f
move the files to correct place and configure widget for kitconcept.seo
iFlameing 536e9f1
Merge branch 'main' into seo-text-textarea-widget
iFlameing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
frontend/packages/volto-light-theme/news/+softMaxLengthWarning.feature
This file contains hidden or 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 @@ | ||
| Add soft_max_length warnings for text/textarea widget @Tishasoumya-02 |
136 changes: 136 additions & 0 deletions
136
frontend/packages/volto-light-theme/src/components/Widgets/SoftTextWidget.tsx
This file contains hidden or 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,136 @@ | ||
| /** | ||
| * OVERRIDE TextWidget.jsx | ||
| * REASON: Add softMaxLength warning for TextWidget. | ||
| * FILE: https://github.com/plone/volto/blob/ede0335834445988dd0639ab2361180251c97e4e/packages/volto/src/components/manage/Widgets/TextWidget.jsx | ||
| * FILE VERSION: Volto 19.0.0-alpha.20 | ||
| * DATE: 2025-12-16 | ||
| * DEVELOPER: @Tishasoumya-02 | ||
| */ | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { Input, Label } from 'semantic-ui-react'; | ||
| import Icon from '@plone/volto/components/theme/Icon/Icon'; | ||
| import FormFieldWrapper from '@plone/volto/components/manage/Widgets/FormFieldWrapper'; | ||
| const SoftTextWidget = (props) => { | ||
| const { | ||
| id, | ||
| value, | ||
| onChange, | ||
| onBlur, | ||
| onClick, | ||
| icon, | ||
| iconAction, | ||
| minLength, | ||
| maxLength, | ||
| softMaxLength, | ||
| placeholder, | ||
| isDisabled, | ||
| focus, | ||
| } = props; | ||
| const ref = useRef(); | ||
| const [softLengthWarning, setSoftLengthWarning] = useState(''); | ||
| useEffect(() => { | ||
| if (focus) { | ||
| ref.current.focus(); | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, []); | ||
| // START CUSTOMIZATION | ||
| const handleChange = (id, newValue) => { | ||
| if (softMaxLength && newValue?.length) { | ||
| const remaining = softMaxLength - newValue.length; | ||
| if (remaining < 0) { | ||
| setSoftLengthWarning( | ||
| `You have exceeded the recommended limit by ${Math.abs(remaining)}`, | ||
| ); | ||
| } else { | ||
| setSoftLengthWarning(''); | ||
| } | ||
| } | ||
| onChange(id, newValue); | ||
| }; | ||
| // END CUSTOMIZATION | ||
| return ( | ||
| <FormFieldWrapper {...props} className="text"> | ||
| <Input | ||
| id={`field-${id}`} | ||
| name={id} | ||
| value={value || ''} | ||
| disabled={isDisabled} | ||
| icon={icon || null} | ||
| placeholder={placeholder} | ||
| // START CUSTOMIZATION | ||
| onChange={({ target }) => | ||
| handleChange(id, target.value === '' ? undefined : target.value) | ||
| } | ||
| // END CUSTOMIZATION | ||
| ref={ref} | ||
| onBlur={({ target }) => | ||
| onBlur(id, target.value === '' ? undefined : target.value) | ||
| } | ||
| onClick={() => onClick()} | ||
| minLength={minLength || null} | ||
| maxLength={maxLength || null} | ||
| /> | ||
| {/* START CUSTOMIZATION */} | ||
| {softLengthWarning.length > 0 && ( | ||
| <Label key={softLengthWarning} basic color="yellow" pointing> | ||
| {softLengthWarning} | ||
| </Label> | ||
| )} | ||
| {/* END CUSTOMIZATION */} | ||
| {icon && iconAction && ( | ||
| <button className={`field-${id}-action-button`} onClick={iconAction}> | ||
| <Icon name={icon} size="18px" /> | ||
| </button> | ||
| )} | ||
| </FormFieldWrapper> | ||
| ); | ||
| }; | ||
| export default SoftTextWidget; | ||
| SoftTextWidget.propTypes = { | ||
| id: PropTypes.string.isRequired, | ||
| title: PropTypes.string.isRequired, | ||
| description: PropTypes.string, | ||
| required: PropTypes.bool, | ||
| error: PropTypes.arrayOf(PropTypes.string), | ||
| value: PropTypes.string, | ||
| focus: PropTypes.bool, | ||
| onChange: PropTypes.func, | ||
| onBlur: PropTypes.func, | ||
| onClick: PropTypes.func, | ||
| onEdit: PropTypes.func, | ||
| onDelete: PropTypes.func, | ||
| icon: PropTypes.shape({ | ||
| xmlns: PropTypes.string, | ||
| viewBox: PropTypes.string, | ||
| content: PropTypes.string, | ||
| }), | ||
| iconAction: PropTypes.func, | ||
| minLength: PropTypes.number, | ||
| maxLength: PropTypes.number, | ||
| // START CUSTOMIZATION | ||
| softMaxLength: PropTypes.number, | ||
| // END CUSTOMIZATION | ||
| wrapped: PropTypes.bool, | ||
| placeholder: PropTypes.string, | ||
| }; | ||
| SoftTextWidget.defaultProps = { | ||
| description: null, | ||
| required: false, | ||
| error: [], | ||
| value: null, | ||
| onChange: () => {}, | ||
| onBlur: () => {}, | ||
| onClick: () => {}, | ||
| onEdit: null, | ||
| onDelete: null, | ||
| focus: false, | ||
| icon: null, | ||
| iconAction: null, | ||
| minLength: null, | ||
| maxLength: null, | ||
| // START CUSTOMIZATION | ||
| softMaxLength: null, | ||
| // END CUSTOMIZATION | ||
| }; | ||
126 changes: 126 additions & 0 deletions
126
frontend/packages/volto-light-theme/src/components/Widgets/SoftTextareaWidget.tsx
This file contains hidden or 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,126 @@ | ||
| /** | ||
| * OVERRIDE TextareaWidget.jsx | ||
| * REASON: Add softMaxLength warning for TextareaWidget. | ||
| * FILE: https://github.com/plone/volto/blob/ede0335834445988dd0639ab2361180251c97e4e/packages/volto/src/components/manage/Widgets/TextareaWidget.jsx | ||
| * FILE VERSION: Volto 19.0.0-alpha.20 | ||
| * DATE: 2025-12-16 | ||
| * DEVELOPER: @Tishasoumya-02 | ||
| */ | ||
| import { useState } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { Label, TextArea } from 'semantic-ui-react'; | ||
| import { injectIntl } from 'react-intl'; | ||
| import FormFieldWrapper from '@plone/volto/components/manage/Widgets/FormFieldWrapper'; | ||
| /** | ||
| * TextareaWidget, a widget for multiple lines text | ||
| * | ||
| * To use it, in schema properties, declare a field like: | ||
| * | ||
| * ```jsx | ||
| * { | ||
| * title: "Text", | ||
| * widget: 'textarea', | ||
| * } | ||
| * ``` | ||
| */ | ||
| const SoftTextareaWidget = (props) => { | ||
| const { | ||
| id, | ||
| maxLength, | ||
| value, | ||
| onChange, | ||
| placeholder, | ||
| isDisabled, | ||
| softMaxLength, | ||
| } = props; | ||
| const [lengthError, setlengthError] = useState(''); | ||
| const [softLengthWarning, setSoftLengthWarning] = useState(''); | ||
| const onhandleChange = (id, value) => { | ||
| if (maxLength && value?.length) { | ||
| let remlength = maxLength - value.length; | ||
| if (remlength < 0) { | ||
| setlengthError( | ||
| `You have exceeded word limit by ${Math.abs(remlength)}`, | ||
| ); | ||
| } else { | ||
| setlengthError(''); | ||
| } | ||
| } | ||
| //START CUSTOMIZATION | ||
| if (softMaxLength && value?.length) { | ||
| let remaining = softMaxLength - value.length; | ||
| if (remaining < 0) { | ||
| setSoftLengthWarning( | ||
| `You have exceeded the recommended limit by ${Math.abs(remaining)}`, | ||
| ); | ||
| } else { | ||
| setSoftLengthWarning(''); | ||
| } | ||
| } | ||
| //END CUSTOMIZATION | ||
| onChange(id, value); | ||
| }; | ||
| return ( | ||
| <FormFieldWrapper {...props} className="textarea"> | ||
| <TextArea | ||
| id={`field-${id}`} | ||
| name={id} | ||
| value={value || ''} | ||
| disabled={isDisabled} | ||
| placeholder={placeholder} | ||
| onChange={({ target }) => | ||
| onhandleChange(id, target.value === '' ? undefined : target.value) | ||
| } | ||
| /> | ||
| {/* START CUSTOMIZATION */} | ||
| {softLengthWarning.length > 0 && ( | ||
| <Label key={softLengthWarning} basic color="yellow" pointing> | ||
| {softLengthWarning} | ||
| </Label> | ||
| )} | ||
| {/* END CUSTOMIZATION */} | ||
| {lengthError.length > 0 && ( | ||
| <Label key={lengthError} basic color="red" pointing> | ||
| {lengthError} | ||
| </Label> | ||
| )} | ||
| </FormFieldWrapper> | ||
| ); | ||
| }; | ||
| /** | ||
| * Property types. | ||
| * @property {Object} propTypes Property types. | ||
| * @static | ||
| */ | ||
| SoftTextareaWidget.propTypes = { | ||
| id: PropTypes.string.isRequired, | ||
| title: PropTypes.string.isRequired, | ||
| description: PropTypes.string, | ||
| maxLength: PropTypes.number, | ||
| softMaxLength: PropTypes.number, | ||
| required: PropTypes.bool, | ||
| error: PropTypes.arrayOf(PropTypes.string), | ||
| value: PropTypes.string, | ||
| onChange: PropTypes.func, | ||
| onEdit: PropTypes.func, | ||
| onDelete: PropTypes.func, | ||
| wrapped: PropTypes.bool, | ||
| placeholder: PropTypes.string, | ||
| }; | ||
| /** | ||
| * Default properties. | ||
| * @property {Object} defaultProps Default properties. | ||
| * @static | ||
| */ | ||
| SoftTextareaWidget.defaultProps = { | ||
| description: null, | ||
| maxLength: null, | ||
| softMaxLength: null, | ||
| required: false, | ||
| error: [], | ||
| value: null, | ||
| onChange: null, | ||
| onEdit: null, | ||
| onDelete: null, | ||
| }; | ||
| export default injectIntl(SoftTextareaWidget); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ import { footerLogosSchema } from '../components/Widgets/schema/footerLogosSchem | |
| import { footerLinksSchema } from '../components/Widgets/schema/footerLinksSchema'; | ||
| import { iconLinkListSchema } from '../components/Widgets/schema/iconLinkListSchema'; | ||
| import ModalJSONEditor from '../components/Widgets/ModalJSONEditor'; | ||
| import SoftTextWidget from '../components/Widgets/SoftTextWidget'; | ||
| import SoftTextareaWidget from '../components/Widgets/SoftTextareaWidget'; | ||
|
|
||
| export default function install(config: ConfigType) { | ||
| // Color picker widget override - use our own non-semanticUI widget | ||
|
|
@@ -27,6 +29,8 @@ export default function install(config: ConfigType) { | |
|
|
||
| config.widgets.widget.colorPicker = ColorPicker; | ||
| config.widgets.widget.themeColorSwatch = ThemeColorSwatch; | ||
| config.widgets.widget.soft_text_widget = SoftTextWidget; | ||
|
Member
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. I know the pickyness, but in VLT we are using camelCase widget names. |
||
| config.widgets.widget.soft_text_area_widget = SoftTextareaWidget; | ||
|
|
||
| config.widgets.widget.modalJSONEditor = ModalJSONEditor; | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Tishasoumya-02 @iFlameing this is no longer a customization, so we don't need the comments.