Skip to content

Commit 40b1315

Browse files
authored
feat: support classnames and styles (#289)
* feat: support classnames and styles * add test * Update tests/Mentions.spec.tsx * update to update to popupClassName * fix * update snap
1 parent 843d868 commit 40b1315

File tree

6 files changed

+65
-13
lines changed

6 files changed

+65
-13
lines changed

docs/examples/onScroll.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default () => (
88
rows={3}
99
defaultValue="Hello @ World @"
1010
onPopupScroll={console.log}
11-
dropdownClassName="on-scroll"
11+
popupClassName="on-scroll"
1212
open
1313
options={Array.from({ length: 1000 }).map((_, index) => ({
1414
value: `item-${index}`,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
]
4646
},
4747
"dependencies": {
48-
"@rc-component/input": "~1.0.0",
48+
"@rc-component/input": "~1.0.1",
4949
"@rc-component/menu": "~1.0.0",
5050
"@rc-component/textarea": "~1.0.0",
5151
"@rc-component/trigger": "^3.0.0",

src/KeywordTrigger.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ interface KeywordTriggerProps {
5050
transitionName?: string;
5151
children?: React.ReactElement;
5252
getPopupContainer?: () => HTMLElement;
53-
dropdownClassName?: string;
53+
popupClassName?: string;
54+
popupStyle?: React.CSSProperties;
5455
}
5556

5657
const KeywordTrigger: FC<KeywordTriggerProps> = props => {
@@ -61,7 +62,8 @@ const KeywordTrigger: FC<KeywordTriggerProps> = props => {
6162
visible,
6263
transitionName,
6364
getPopupContainer,
64-
dropdownClassName,
65+
popupClassName,
66+
popupStyle,
6567
direction,
6668
placement,
6769
} = props;
@@ -91,7 +93,8 @@ const KeywordTrigger: FC<KeywordTriggerProps> = props => {
9193
popupMotion={{ motionName: transitionName }}
9294
builtinPlacements={BUILT_IN_PLACEMENTS}
9395
getPopupContainer={getPopupContainer}
94-
popupClassName={dropdownClassName}
96+
popupClassName={popupClassName}
97+
popupStyle={popupStyle}
9598
>
9699
{children}
97100
</Trigger>

src/Mentions.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,19 @@ export interface MentionsProps extends BaseTextareaAttrs {
6464
onFocus?: React.FocusEventHandler<HTMLTextAreaElement>;
6565
onBlur?: React.FocusEventHandler<HTMLTextAreaElement>;
6666
getPopupContainer?: () => HTMLElement;
67-
dropdownClassName?: string;
67+
popupClassName?: string;
6868
/** @private Testing usage. Do not use in prod. It will not work as your expect. */
6969
open?: boolean;
7070
children?: React.ReactNode;
7171
options?: DataDrivenOptionProps[];
7272
classNames?: CommonInputProps['classNames'] & {
7373
mentions?: string;
74+
textarea?: string;
75+
popup?: string;
76+
};
77+
styles?: {
78+
textarea?: React.CSSProperties;
79+
popup?: React.CSSProperties;
7480
};
7581
onPopupScroll?: (event: React.UIEvent<HTMLDivElement>) => void;
7682
}
@@ -92,6 +98,8 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
9298
prefixCls,
9399
className,
94100
style,
101+
classNames: mentionClassNames,
102+
styles,
95103

96104
// Misc
97105
prefix = '@',
@@ -123,7 +131,7 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
123131
placement,
124132
direction,
125133
getPopupContainer,
126-
dropdownClassName,
134+
popupClassName,
127135

128136
rows = 1,
129137

@@ -473,6 +481,8 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
473481
ref={containerRef}
474482
>
475483
<TextArea
484+
classNames={{ textarea: mentionClassNames?.textarea }}
485+
styles={{ textarea: styles?.textarea }}
476486
ref={textareaRef}
477487
value={mergedValue}
478488
{...restProps}
@@ -506,7 +516,11 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
506516
options={mergedOptions}
507517
visible
508518
getPopupContainer={getPopupContainer}
509-
dropdownClassName={dropdownClassName}
519+
popupClassName={classNames(
520+
popupClassName,
521+
mentionClassNames?.popup,
522+
)}
523+
popupStyle={styles?.popup}
510524
>
511525
<span>{mergedMeasurePrefix}</span>
512526
</KeywordTrigger>
@@ -530,7 +544,8 @@ const Mentions = forwardRef<MentionsRef, MentionsProps>(
530544
value: customValue,
531545
allowClear,
532546
onChange,
533-
classNames: classes,
547+
classNames: mentionsClassNames,
548+
styles,
534549
className,
535550
disabled,
536551
onClear,
@@ -573,13 +588,15 @@ const Mentions = forwardRef<MentionsRef, MentionsProps>(
573588
allowClear={allowClear}
574589
handleReset={handleReset}
575590
className={className}
576-
classNames={classes}
591+
classNames={mentionsClassNames}
577592
disabled={disabled}
578593
ref={holderRef}
579594
onClear={onClear}
580595
>
581596
<InternalMentions
582-
className={classes?.mentions}
597+
className={mentionsClassNames?.mentions}
598+
styles={styles}
599+
classNames={mentionsClassNames}
583600
prefixCls={prefixCls}
584601
ref={mentionRef}
585602
onChange={triggerChange}

tests/Mentions.spec.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,41 @@ describe('Mentions', () => {
258258
});
259259
});
260260

261-
it('dropdownClassName should work', () => {
261+
it('popupClassName should work', () => {
262262
const { container, baseElement } = renderMentions({
263-
dropdownClassName: 'my-dropdown',
263+
popupClassName: 'my-dropdown',
264264
});
265265
simulateInput(container, '@');
266266
expect(
267267
baseElement.querySelector('.my-dropdown.rc-mentions-dropdown'),
268268
).toBeTruthy();
269269
});
270+
it('classNames and styles should work', () => {
271+
const { container, baseElement } = renderMentions({
272+
classNames: {
273+
popup: 'test-popup',
274+
textarea: 'test-textarea',
275+
},
276+
styles: {
277+
popup: { background: 'red' },
278+
textarea: { background: 'blue' },
279+
},
280+
});
281+
simulateInput(container, '@');
282+
expect(
283+
baseElement.querySelector('.test-popup.rc-mentions-dropdown'),
284+
).toBeTruthy();
285+
expect(
286+
(
287+
baseElement.querySelector(
288+
'.test-popup.rc-mentions-dropdown',
289+
) as HTMLElement
290+
).style.background,
291+
).toBe('red');
292+
const textarea = baseElement.querySelector('.rc-textarea');
293+
expect(textarea).toHaveClass('test-textarea');
294+
expect(textarea).toHaveStyle('background: blue');
295+
});
270296

271297
it('should support direction', () => {
272298
const { container, baseElement } = renderMentions({ direction: 'rtl' });

tests/__snapshots__/AllowClear.spec.tsx.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ exports[`should support allowClear should not show icon if defaultValue is undef
1717
>
1818
<button
1919
class="rc-mentions-clear-icon rc-mentions-clear-icon-hidden"
20+
tabindex="-1"
2021
type="button"
2122
>
2223
@@ -42,6 +43,7 @@ exports[`should support allowClear should not show icon if defaultValue is undef
4243
>
4344
<button
4445
class="rc-mentions-clear-icon rc-mentions-clear-icon-hidden"
46+
tabindex="-1"
4547
type="button"
4648
>
4749
@@ -67,6 +69,7 @@ exports[`should support allowClear should not show icon if defaultValue is undef
6769
>
6870
<button
6971
class="rc-mentions-clear-icon rc-mentions-clear-icon-hidden"
72+
tabindex="-1"
7073
type="button"
7174
>
7275
@@ -92,6 +95,7 @@ exports[`should support allowClear should not show icon if value is undefined, n
9295
>
9396
<button
9497
class="rc-mentions-clear-icon rc-mentions-clear-icon-hidden"
98+
tabindex="-1"
9599
type="button"
96100
>
97101
@@ -117,6 +121,7 @@ exports[`should support allowClear should not show icon if value is undefined, n
117121
>
118122
<button
119123
class="rc-mentions-clear-icon rc-mentions-clear-icon-hidden"
124+
tabindex="-1"
120125
type="button"
121126
>
122127
@@ -142,6 +147,7 @@ exports[`should support allowClear should not show icon if value is undefined, n
142147
>
143148
<button
144149
class="rc-mentions-clear-icon rc-mentions-clear-icon-hidden"
150+
tabindex="-1"
145151
type="button"
146152
>
147153

0 commit comments

Comments
 (0)