-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
363 additions
and
21 deletions.
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
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,120 @@ | ||
import classNames from 'classnames'; | ||
import * as React from 'react'; | ||
|
||
import { Button, Flex, Input } from 'antd'; | ||
import { TextAreaRef } from 'antd/lib/input/TextArea'; | ||
import { EditConfig } from './interface'; | ||
import useStyle from './style'; | ||
|
||
const { TextArea } = Input; | ||
|
||
interface EditableProps { | ||
prefixCls: string; | ||
value: string; | ||
onChange?: (value: string) => void; | ||
onCancel?: () => void; | ||
onEnd?: (value: string) => void; | ||
editorClassName?: string; | ||
editorStyle?: React.CSSProperties; | ||
editorTextAreaConfig?: EditConfig['editorTextAreaConfig']; | ||
editorButtonConfig?: EditConfig['editorButtonConfig']; | ||
} | ||
|
||
const Editor: React.FC<EditableProps> = (props) => { | ||
const { | ||
prefixCls, | ||
editorClassName: className, | ||
editorStyle, | ||
value, | ||
onChange, | ||
onCancel, | ||
onEnd, | ||
editorTextAreaConfig, | ||
editorButtonConfig, | ||
} = props; | ||
const textAreaRef = React.useRef<TextAreaRef>(null); | ||
|
||
const [current, setCurrent] = React.useState(value); | ||
|
||
React.useEffect(() => { | ||
setCurrent(value); | ||
}, [value]); | ||
|
||
React.useEffect(() => { | ||
if (textAreaRef.current?.resizableTextArea) { | ||
const { textArea } = textAreaRef.current.resizableTextArea; | ||
textArea.focus(); | ||
const { length } = textArea.value; | ||
textArea.setSelectionRange(length, length); | ||
} | ||
}, []); | ||
|
||
const onTextAreaChange: React.ChangeEventHandler<HTMLTextAreaElement> = ({ target }) => { | ||
setCurrent(target.value.replace(/[\n\r]/g, '')); | ||
onChange?.(target.value.replace(/[\n\r]/g, '')); | ||
}; | ||
|
||
const confirmEnd = () => { | ||
onEnd?.(current.trim()); | ||
}; | ||
|
||
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls); | ||
|
||
const editorClassName = classNames( | ||
prefixCls, | ||
`${prefixCls}-editor`, | ||
className, | ||
hashId, | ||
cssVarCls, | ||
); | ||
|
||
const CancelButton = () => | ||
editorButtonConfig ? ( | ||
editorButtonConfig | ||
.filter((config) => config.type === 'cancel') | ||
.map((config, index) => ( | ||
<Button key={index} size="small" {...config.option} onClick={onCancel}> | ||
{config.text || 'Cancel'} | ||
</Button> | ||
)) | ||
) : ( | ||
<Button size="small" onClick={onCancel}> | ||
Cancel | ||
</Button> | ||
); | ||
const SaveButton = () => | ||
editorButtonConfig ? ( | ||
editorButtonConfig | ||
.filter((config) => config.type === 'save') | ||
.map((config, index) => ( | ||
<Button key={index} type="primary" size="small" {...config.option} onClick={confirmEnd}> | ||
{config.text || 'Save'} | ||
</Button> | ||
)) | ||
) : ( | ||
<Button type="primary" size="small" onClick={confirmEnd}> | ||
Save | ||
</Button> | ||
); | ||
|
||
return wrapCSSVar( | ||
<div className={editorClassName} style={editorStyle}> | ||
<Flex gap="small" vertical flex="auto"> | ||
<TextArea | ||
variant="borderless" | ||
ref={textAreaRef} | ||
value={current} | ||
onChange={onTextAreaChange} | ||
autoSize={{ minRows: 2, maxRows: 3 }} | ||
{...editorTextAreaConfig} | ||
/> | ||
<Flex gap="small" justify="end"> | ||
<CancelButton /> | ||
<SaveButton /> | ||
</Flex> | ||
</Flex> | ||
</div>, | ||
); | ||
}; | ||
|
||
export default Editor; |
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 | ||
|
||
通过设置 `editable` 属性,开启对 `content` 的编辑效果。 | ||
|
||
## en-US | ||
|
||
Enable the editing effect of `content` by setting the `editable` property. |
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,128 @@ | ||
import { | ||
DeleteOutlined, | ||
EditOutlined, | ||
LeftOutlined, | ||
RightOutlined, | ||
UserOutlined, | ||
} from '@ant-design/icons'; | ||
import { Bubble } from '@ant-design/x'; | ||
import { Button, Flex } from 'antd'; | ||
import React from 'react'; | ||
import { EditConfig } from '../interface'; | ||
|
||
const fooAvatar: React.CSSProperties = { | ||
color: '#f56a00', | ||
backgroundColor: '#fde3cf', | ||
}; | ||
|
||
const barAvatar: React.CSSProperties = { | ||
color: '#fff', | ||
backgroundColor: '#87d068', | ||
}; | ||
|
||
const hideAvatar: React.CSSProperties = { | ||
visibility: 'hidden', | ||
}; | ||
|
||
const App = () => { | ||
const [editing, setEditing] = React.useState(false); | ||
const [currentIndex, setCurrentIndex] = React.useState(0); | ||
const [editHistory, setEditHistory] = React.useState(['Good morning, how are you?']); // 编辑历史记录 | ||
|
||
const triggerEdit = () => { | ||
setEditing((prev) => !prev); | ||
}; | ||
|
||
const handleLeftClick = () => { | ||
if (currentIndex > 0) { | ||
setCurrentIndex((prev) => prev - 1); | ||
} | ||
}; | ||
|
||
const handleRightClick = () => { | ||
if (currentIndex < editHistory.length - 1) { | ||
setCurrentIndex((prev) => prev + 1); | ||
} | ||
}; | ||
|
||
const cancelEdit = () => { | ||
setEditing(false); | ||
}; | ||
|
||
const endEdit = (c: string) => { | ||
setEditing(false); | ||
setEditHistory((prev) => [...prev, c]); | ||
setCurrentIndex(editHistory.length); | ||
}; | ||
|
||
const editConfig: EditConfig = { | ||
editing, | ||
onCancel: cancelEdit, | ||
onEnd: endEdit, | ||
editorTextAreaConfig: { autoSize: { minRows: 2, maxRows: 4 } }, | ||
editorButtonConfig: [ | ||
{ | ||
type: 'cancel', | ||
text: 'Cancel', | ||
option: { size: 'small' }, | ||
}, | ||
{ | ||
type: 'save', | ||
text: 'Save', | ||
option: { size: 'small', type: 'primary' }, | ||
}, | ||
], | ||
}; | ||
|
||
return ( | ||
<Flex gap="middle" vertical> | ||
<Bubble | ||
placement="end" | ||
content={editHistory[currentIndex]} | ||
editable={editConfig} | ||
avatar={{ icon: <UserOutlined />, style: barAvatar }} | ||
header={ | ||
editHistory.length > 1 && | ||
!editing && ( | ||
<Flex justify="end"> | ||
<Button | ||
size="small" | ||
type="text" | ||
icon={<LeftOutlined />} | ||
onClick={handleLeftClick} | ||
disabled={currentIndex === 0} | ||
/> | ||
<span>{`${currentIndex + 1} / ${editHistory.length}`}</span> | ||
<Button | ||
size="small" | ||
type="text" | ||
icon={<RightOutlined />} | ||
onClick={handleRightClick} | ||
disabled={currentIndex === editHistory.length - 1} | ||
/> | ||
</Flex> | ||
) | ||
} | ||
footer={ | ||
<Flex justify="end"> | ||
<Button size="small" type="text" icon={<DeleteOutlined />} /> | ||
<Button size="small" type="text" icon={<EditOutlined />} onClick={triggerEdit} /> | ||
</Flex> | ||
} | ||
/> | ||
<Bubble | ||
placement="start" | ||
content="Hi, good morning, I'm fine!" | ||
avatar={{ icon: <UserOutlined />, style: fooAvatar }} | ||
/> | ||
<Bubble | ||
placement="start" | ||
content="What a beautiful day!" | ||
styles={{ avatar: hideAvatar }} | ||
avatar={{}} | ||
/> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
|
||
export default function useMergedConfig<Target>( | ||
propConfig: any, | ||
templateConfig?: Target, | ||
): readonly [boolean, Target] { | ||
return React.useMemo<readonly [boolean, Target]>(() => { | ||
const support = !!propConfig; | ||
|
||
return [ | ||
support, | ||
{ | ||
...templateConfig, | ||
...(support && typeof propConfig === 'object' ? propConfig : null), | ||
}, | ||
] as const; | ||
}, [propConfig]); | ||
} |
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.