From b255406ee66804beff14871d7ce32d30db053f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Fri, 28 Feb 2025 11:57:00 +0800 Subject: [PATCH] chore: add UnstableContext (#290) * chore: add UnstableContext * chore: clean up * docs: fix lint * docs: fix lint --- docs/examples/debug.tsx | 47 +++++++++++++++++++------------------- docs/examples/onScroll.tsx | 25 ++++++++++---------- src/Mentions.tsx | 15 ++++-------- src/context.tsx | 8 +++++++ src/index.ts | 2 ++ tests/Open.spec.tsx | 42 ++++++++++++++++------------------ 6 files changed, 71 insertions(+), 68 deletions(-) create mode 100644 src/context.tsx diff --git a/docs/examples/debug.tsx b/docs/examples/debug.tsx index 2ae6f1c..a7a6c7f 100644 --- a/docs/examples/debug.tsx +++ b/docs/examples/debug.tsx @@ -1,28 +1,29 @@ import React from 'react'; -import Mentions from '@rc-component/mentions'; +import Mentions, { UnstableContext } from '@rc-component/mentions'; import '../../assets/index.less'; export default () => ( - { - console.log(e); - }} - open - options={[ - { - value: 'light', - label: 'Light', - }, - { - value: 'bamboo', - label: 'Bamboo', - }, - { - value: 'cat', - label: 'Cat', - }, - ]} - /> + + { + console.log(e); + }} + options={[ + { + value: 'light', + label: 'Light', + }, + { + value: 'bamboo', + label: 'Bamboo', + }, + { + value: 'cat', + label: 'Cat', + }, + ]} + /> + ); diff --git a/docs/examples/onScroll.tsx b/docs/examples/onScroll.tsx index ca565d7..9b9cbdb 100644 --- a/docs/examples/onScroll.tsx +++ b/docs/examples/onScroll.tsx @@ -1,18 +1,19 @@ import React from 'react'; -import Mentions from '@rc-component/mentions'; +import Mentions, { UnstableContext } from '@rc-component/mentions'; import '../../assets/index.less'; import './onScroll.less'; export default () => ( - ({ - value: `item-${index}`, - label: `item-${index}`, - }))} - /> + + ({ + value: `item-${index}`, + label: `item-${index}`, + }))} + /> + ); diff --git a/src/Mentions.tsx b/src/Mentions.tsx index 9c3c937..8c4494c 100644 --- a/src/Mentions.tsx +++ b/src/Mentions.tsx @@ -7,9 +7,9 @@ import TextArea from '@rc-component/textarea'; import toArray from '@rc-component/util/lib/Children/toArray'; import useMergedState from '@rc-component/util/lib/hooks/useMergedState'; import KeyCode from '@rc-component/util/lib/KeyCode'; -import warning from '@rc-component/util/lib/warning'; import React, { forwardRef, + useContext, useEffect, useImperativeHandle, useMemo, @@ -29,6 +29,7 @@ import { replaceWithMeasure, setInputSelection, } from './util'; +import { UnstableContext } from './context'; type BaseTextareaAttrs = Omit< TextAreaProps, @@ -65,8 +66,6 @@ export interface MentionsProps extends BaseTextareaAttrs { onBlur?: React.FocusEventHandler; getPopupContainer?: () => HTMLElement; popupClassName?: string; - /** @private Testing usage. Do not use in prod. It will not work as your expect. */ - open?: boolean; children?: React.ReactNode; options?: DataDrivenOptionProps[]; classNames?: CommonInputProps['classNames'] & { @@ -109,7 +108,6 @@ const InternalMentions = forwardRef( defaultValue, children, options, - open, allowClear, silent, @@ -179,6 +177,8 @@ const InternalMentions = forwardRef( }); // =============================== Open =============================== + const { open } = useContext(UnstableContext); + useEffect(() => { // Sync measure div top with textarea for rc-trigger usage if (measuring && measureRef.current) { @@ -200,13 +200,6 @@ const InternalMentions = forwardRef( ] >(() => { if (open) { - if (process.env.NODE_ENV !== 'production') { - warning( - false, - '`open` of Mentions is only used for debug usage. Do not use in you production.', - ); - } - for (let i = 0; i < mergedPrefix.length; i += 1) { const curPrefix = mergedPrefix[i]; const index = mergedValue.lastIndexOf(curPrefix); diff --git a/src/context.tsx b/src/context.tsx new file mode 100644 index 0000000..e021072 --- /dev/null +++ b/src/context.tsx @@ -0,0 +1,8 @@ +import * as React from 'react'; + +export interface UnstableContextProps { + /** Only used for antd site v6 preview usage. */ + open?: boolean; +} + +export const UnstableContext = React.createContext({}); diff --git a/src/index.ts b/src/index.ts index daf0fe8..ded665f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,4 +3,6 @@ import type { MentionsProps } from './Mentions'; export type { MentionsProps }; +export { UnstableContext } from './context'; + export default Mentions; diff --git a/tests/Open.spec.tsx b/tests/Open.spec.tsx index 3f5d634..91751cd 100644 --- a/tests/Open.spec.tsx +++ b/tests/Open.spec.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import Mentions from '../src'; +import Mentions, { UnstableContext } from '../src'; import { expectMeasuring } from './util'; import { render } from '@testing-library/react'; @@ -8,31 +8,29 @@ describe('Mentions.Open', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const { container } = render( - , + + + , ); expectMeasuring(container); - expect(errorSpy).toHaveBeenCalledWith( - 'Warning: `open` of Mentions is only used for debug usage. Do not use in you production.', - ); errorSpy.mockRestore(); }); });