Skip to content

Commit

Permalink
Merge branch 'staging' of https://github.com/brainstormforce/force-ui
Browse files Browse the repository at this point in the history
…into uat_final_fixes
  • Loading branch information
ravindra114 committed Oct 15, 2024
2 parents 4c42e17 + 3c87bf3 commit 652933d
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/components/editor-input/editor-input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ const EditorInputComponent = (
menuComponent,
menuItemComponent,
className,
wrapperClassName,
disabled = false,
autoSpaceAfterMention = false,
},
ref
) => {
Expand Down Expand Up @@ -94,7 +96,8 @@ const EditorInputComponent = (
'relative w-full',
editorCommonClassNames,
editorInputClassNames[ size ],
disabled && editorDisabledClassNames
disabled && editorDisabledClassNames,
wrapperClassName
) }
>
<LexicalComposer initialConfig={ initialConfig }>
Expand Down Expand Up @@ -123,6 +126,7 @@ const EditorInputComponent = (
by={ by }
optionsArray={ options }
trigger={ trigger }
autoSpace={ autoSpaceAfterMention }
/>
<OnChangePlugin
onChange={ handleOnChange }
Expand Down
29 changes: 29 additions & 0 deletions src/components/editor-input/editor-input.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ export default {
defaultValue: { summary: false },
},
},
autoSpaceAfterMention: {
name: 'autoSpaceAfterMention',
description:
'Defines if the editor input should add a space after selecting a mention/tag option.',
control: 'boolean',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: false },
},
},
onChange: {
name: 'On Change',
description:
Expand Down Expand Up @@ -118,6 +128,15 @@ export default {
defaultValue: { summary: '' },
},
},
wrapperClassName: {
name: 'wrapperClassName',
description: 'Custom class name to be added to the editor input wrapper.',
control: 'text',
table: {
type: { summary: 'string' },
defaultValue: { summary: '' },
},
},
},
decorators: [
( Story ) => (
Expand All @@ -139,6 +158,16 @@ const options = [
'Pink',
];

export const Default = {
args: {
size: 'md',
autoSpaceAfterMention: false,
autoFocus: false,
options,
onChange: ( editorState ) => editorState.toJSON(),
},
};

// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
export const Small = {
args: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const MentionComponent = ( { data, by, size, nodeKey } ) => {

return (
<Badge
className="inline-flex"
className="inline-flex mx-0.5"
type="rounded"
size={ mapSizeToBadgeSize( size ) }
label={ renderLabel }
Expand Down
84 changes: 82 additions & 2 deletions src/components/editor-input/mention-plugin/mention-plugin.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { useCallback, useState, useMemo } from 'react';
import { useCallback, useState, useMemo, useEffect, useRef } from 'react';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { LexicalTypeaheadMenuPlugin } from '@lexical/react/LexicalTypeaheadMenuPlugin';
import { $createMentionNode } from './mention-node';
import { $createMentionNode, $isMentionNode } from './mention-node';
import OptionItem from './mention-option-item';
import useMentionLookupService from './mention-hooks';
import EditorCombobox from './mention-combobox';
import {
$createTextNode,
$getSelection,
COMMAND_PRIORITY_LOW,
KEY_DOWN_COMMAND,
KEY_BACKSPACE_COMMAND,
} from 'lexical';
import { mergeRegister } from '@lexical/utils';

const MentionPlugin = ( {
optionsArray,
Expand All @@ -13,7 +21,9 @@ const MentionPlugin = ( {
trigger = '@', // Default trigger value
menuComponent: MenuComponent = EditorCombobox,
menuItemComponent: MenuItemComponent = EditorCombobox.Item,
autoSpace = true,
} ) => {
const autoSpaceTempOff = useRef( false );
// Define PUNCTUATION and other necessary variables inside the component
const PUNCTUATION =
'\\.,\\+\\*\\?\\$\\@\\|#{}\\(\\)\\^\\-\\[\\]\\\\/!%\'"~=<>_:;';
Expand Down Expand Up @@ -96,6 +106,76 @@ const MentionPlugin = ( {
return results.map( ( result ) => new OptionItem( result ) );
}, [ editor, results ] );

const handleAutoSpaceAfterMention = useCallback(
( event ) => {
if ( ! autoSpace ) {
return false;
}
const { key, ctrlKey, metaKey } = event;

if (
ctrlKey ||
metaKey ||
key === ' ' ||
key.length > 1 ||
autoSpaceTempOff.current
) {
if ( autoSpaceTempOff.current ) {
autoSpaceTempOff.current = false;
}
return false;
}
const selection = $getSelection( editor );
const { focus, anchor } = selection;
const [ node ] = selection.getNodes();

if (
! anchor ||
! focus ||
anchor?.key !== focus?.key ||
anchor?.offset !== focus?.offset ||
! node
) {
return false;
}

if ( $isMentionNode( node ) ) {
const textNode = $createTextNode( ' ' );
node.insertAfter( textNode );
}
},
[ editor, trigger, autoSpace ]
);

const turnOffAutoSpaceIfNecessary = useCallback(
( event ) => {
const { key } = event;
if ( key === 'Backspace' ) {
autoSpaceTempOff.current = true;
}
},
[ autoSpaceTempOff ]
);

useEffect( () => {
if ( ! editor ) {
return;
}

return mergeRegister(
editor.registerCommand(
KEY_DOWN_COMMAND,
handleAutoSpaceAfterMention,
COMMAND_PRIORITY_LOW
),
editor.registerCommand(
KEY_BACKSPACE_COMMAND,
turnOffAutoSpaceIfNecessary,
COMMAND_PRIORITY_LOW
)
);
}, [ editor, handleAutoSpaceAfterMention ] );

return (
<LexicalTypeaheadMenuPlugin
onQueryChange={ setQueryString }
Expand Down
10 changes: 10 additions & 0 deletions src/components/editor-input/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ The Editor Input component is a text input field that allows users to input text
- **Default:** `false`
- **Description:** If `true`, the editor input field will be focused when the component is mounted.

### autoSpaceAfterMention
- **Type:** `boolean`
- **Default:** `true`
- **Description:** If `true`, a space will be added after the mention/tag node. If any other character is pressed after selecting a mention/tag option, the space will be added automatically.

### className
- **Type:** `string`
- **Default:** `""`
Expand All @@ -98,6 +103,11 @@ The Editor Input component is a text input field that allows users to input text
/>
```

### wrapperClassName
- **Type:** `string`
- **Default:** `""`
- **Description:** Additional classes to be added to the editor input wrapper.

### disabled
- **Type:** `boolean`
- **Default:** `false`
Expand Down
11 changes: 7 additions & 4 deletions src/components/radio-button/radio-button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,12 @@ const RadioButtonComponent = (
if ( ! isDisabled ) {
if ( multiSelection ) {
// In multi-selection, toggle each individual selection
onChange( value, ! checkedValue ); // Pass the toggled value
if ( useSwitch ) {
onChange( value, ! checkedValue );
}
} else {
// In single selection, toggle on and off
onChange( checkedValue ? null : value ); // If it's selected, deselect it; otherwise, select it
onChange( value ); // If it's selected, deselect it; otherwise, select it
}
}
};
Expand Down Expand Up @@ -325,6 +327,7 @@ const RadioButtonComponent = (
isDisabled && 'cursor-not-allowed',
inlineIcon && 'mr-3'
) }
onClick={ handleLabelClick }
>
{ !! badgeItem && badgeItem }
{ ! hideSelection &&
Expand All @@ -335,7 +338,7 @@ const RadioButtonComponent = (
onChange={ () => {
if ( ! multiSelection ) {
// Toggle the switch on or off
onChange( checkedValue ? null : value );
onChange( value );
} else {
// In multi-selection, toggle the current state
onChange( value, ! checkedValue );
Expand Down Expand Up @@ -380,7 +383,7 @@ const RadioButtonComponent = (
<div
className={ cn(
'rounded-full bg-current',
size === 'sm' && 'mt-[2px]',
size === 'sm' && 'mt-[0.5px]',
sizeClassNames[ size ]?.icon
) }
/>
Expand Down

0 comments on commit 652933d

Please sign in to comment.