-
-
Notifications
You must be signed in to change notification settings - Fork 191
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
[Feat]支持 Sender 组件 content 自定义渲染,以支持类似的 speech 交互变体需求或者支持其他的content自定义渲染需求 #354
Open
LofiSu
wants to merge
3
commits into
ant-design:main
Choose a base branch
from
LofiSu:main
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.
+180
−21
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,7 @@ | ||
## zh-CN | ||
|
||
通过 `content` 自定义内容组件,从而实现自定义内容渲染。 | ||
|
||
## en-US | ||
|
||
Customize the content component to achieve custom content rendering. |
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,149 @@ | ||
import { Sender } from '@ant-design/x'; | ||
import { App } from 'antd'; | ||
import React, { useState } from 'react'; | ||
import styled, { keyframes } from 'styled-components'; | ||
|
||
interface CustomContentProps { | ||
innerValue?: string; | ||
onValueChange?: (newValue: string) => void; | ||
onSubmit?: () => void; | ||
loading?: boolean; | ||
disabled?: boolean; | ||
} | ||
|
||
const waveAnimation = keyframes` | ||
0% { | ||
transform: scaleY(0.5); | ||
} | ||
50% { | ||
transform: scaleY(1.2); | ||
} | ||
100% { | ||
transform: scaleY(0.5); | ||
} | ||
`; | ||
|
||
const AudioWave = styled.div` | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
gap: 4px; | ||
`; | ||
|
||
const WaveBar = styled.div<{ delay: number }>` | ||
background-color: white; | ||
width: 6px; | ||
height: 24px; | ||
border-radius: 4px; | ||
animation: ${waveAnimation} 1s ease-in-out infinite; | ||
animation-delay: ${({ delay }) => delay}s; | ||
`; | ||
|
||
const RecordingText = styled.div` | ||
font-size: 14px; | ||
color: white; | ||
margin-top: 8px; | ||
text-align: center; | ||
`; | ||
|
||
const CustomSpeechBox = styled.div<{ recording: boolean }>` | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
padding: 12px; | ||
width: 100%; | ||
height: 50px; | ||
background: linear-gradient(180deg, #f7f7f7, #e5e5e5); | ||
border-radius: 12px; | ||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | ||
cursor: pointer; | ||
|
||
${({ recording }) => | ||
recording && | ||
` | ||
background: linear-gradient(180deg, #d3e5ff, #b3d4ff); | ||
`} | ||
`; | ||
|
||
const SpeechContent = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
flex-direction: column; | ||
`; | ||
|
||
const Dots = styled.div` | ||
font-size: 18px; | ||
color: black; | ||
font-weight: bold; | ||
text-align: center; | ||
`; | ||
|
||
const SpeechContentText = styled.p` | ||
margin: 4px 0 0; | ||
font-size: 14px; | ||
color: black; | ||
font-weight: 500; | ||
text-align: center; | ||
`; | ||
|
||
const Demo: React.FC = () => { | ||
const { message } = App.useApp(); | ||
const [recording, setRecording] = useState(false); | ||
const [value, setValue] = useState<string>(''); | ||
|
||
return ( | ||
<Sender | ||
value={value} | ||
components={{ | ||
content: ({ onValueChange, onSubmit }: CustomContentProps) => ( | ||
<CustomSpeechBox | ||
recording={recording} | ||
onClick={() => { | ||
if (recording) { | ||
message.success('录音结束'); | ||
onValueChange?.(''); | ||
onSubmit?.(); | ||
} else { | ||
message.info('开始录音'); | ||
onValueChange?.('录音内容...'); | ||
} | ||
setRecording(!recording); | ||
}} | ||
> | ||
<SpeechContent> | ||
{recording ? ( | ||
<div style={{ textAlign: 'center' }}> | ||
<AudioWave> | ||
<WaveBar delay={0} /> | ||
<WaveBar delay={0.2} /> | ||
<WaveBar delay={0.4} /> | ||
<WaveBar delay={0.6} /> | ||
<WaveBar delay={0.8} /> | ||
</AudioWave> | ||
<RecordingText>正在听...</RecordingText> | ||
</div> | ||
) : ( | ||
<> | ||
<Dots>· · · ·</Dots> | ||
<SpeechContentText>可以开始说话了</SpeechContentText> | ||
</> | ||
)} | ||
</SpeechContent> | ||
</CustomSpeechBox> | ||
), | ||
}} | ||
actions={() => null} | ||
onChange={setValue} | ||
onSubmit={() => { | ||
message.success('语音消息已发送!'); | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default () => ( | ||
<App> | ||
<Demo /> | ||
</App> | ||
); |
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 |
---|---|---|
|
@@ -21,6 +21,8 @@ type TextareaProps = GetProps<typeof Input.TextArea>; | |
|
||
export interface SenderComponents { | ||
input?: CustomizeComponent<TextareaProps>; | ||
/**Sender Content CustomizeComponent*/ | ||
content?: CustomizeComponent; | ||
} | ||
|
||
export type ActionsRender = ( | ||
|
@@ -261,6 +263,9 @@ function Sender(props: SenderProps, ref: React.Ref<HTMLDivElement>) { | |
} else if (actions) { | ||
actionNode = actions; | ||
} | ||
// ============================ CustomContent ============================ | ||
// 获取自定义的CustomContent组件(默认使用一个空的<div/>占位) | ||
const CustomContent = getComponent(components, ['content'], () => <div />); | ||
|
||
// ============================ Render ============================ | ||
return wrapCSSVar( | ||
|
@@ -289,26 +294,20 @@ function Sender(props: SenderProps, ref: React.Ref<HTMLDivElement>) { | |
</div> | ||
)} | ||
|
||
{/* Input */} | ||
<InputTextArea | ||
{...inputProps} | ||
disabled={disabled} | ||
style={{ ...contextConfig.styles.input, ...styles.input }} | ||
className={classnames(inputCls, contextConfig.classNames.input, classNames.input)} | ||
autoSize={{ maxRows: 8 }} | ||
value={innerValue} | ||
onChange={(e) => { | ||
triggerValueChange((e.target as HTMLTextAreaElement).value); | ||
triggerSpeech(true); | ||
}} | ||
onPressEnter={onInternalKeyPress} | ||
onCompositionStart={onInternalCompositionStart} | ||
onCompositionEnd={onInternalCompositionEnd} | ||
onKeyDown={onKeyDown} | ||
onPaste={onInternalPaste} | ||
variant="borderless" | ||
readOnly={readOnly} | ||
/> | ||
{/* 自定义 content 渲染 */} | ||
{CustomContent ? ( | ||
<CustomContent /> | ||
) : ( | ||
<InputTextArea | ||
{...inputProps} | ||
disabled={disabled} | ||
value={innerValue} | ||
onChange={(e) => triggerValueChange((e.target as HTMLTextAreaElement).value)} | ||
onPressEnter={onInternalKeyPress} | ||
variant="borderless" | ||
readOnly={readOnly} | ||
/> | ||
)} | ||
Comment on lines
+297
to
+310
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. 🛠️ Refactor suggestion 建议:改进条件渲染逻辑 当前的条件渲染逻辑可以更加简洁,同时建议添加错误边界处理。 - {/* 自定义 content 渲染 */}
- {CustomContent ? (
- <CustomContent />
- ) : (
- <InputTextArea
- {...inputProps}
- disabled={disabled}
- value={innerValue}
- onChange={(e) => triggerValueChange((e.target as HTMLTextAreaElement).value)}
- onPressEnter={onInternalKeyPress}
- variant="borderless"
- readOnly={readOnly}
- />
- )}
+ {/* 自定义 content 渲染 */}
+ <React.Suspense fallback={null}>
+ {CustomContent ? (
+ <ErrorBoundary>
+ <CustomContent />
+ </ErrorBoundary>
+ ) : (
+ <InputTextArea
+ {...inputProps}
+ disabled={disabled}
+ value={innerValue}
+ onChange={(e) => triggerValueChange((e.target as HTMLTextAreaElement).value)}
+ onPressEnter={onInternalKeyPress}
+ variant="borderless"
+ readOnly={readOnly}
+ />
+ )}
+ </React.Suspense>
|
||
|
||
{/* Action List */} | ||
<div | ||
|
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
Oops, something went wrong.
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.
需要增强错误处理和可访问性
主要问题:
建议进行以下改进: