fix: TextArea value get cut if ime mode (#28456)

close #27686
close #28417
close #27116
This commit is contained in:
偏右 2020-12-21 16:22:43 +08:00 committed by GitHub
parent 0721ef3cfb
commit 8f1466a30f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 5 deletions

View File

@ -92,12 +92,10 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>(
/> />
); );
let val = fixControlledValue(value) as string; const val = fixControlledValue(value) as string;
// Max length value // Max length value
const hasMaxLength = Number(maxLength) > 0; const hasMaxLength = Number(maxLength) > 0;
// fix #27612 将value转为数组进行截取解决 '😂'.length === 2 等emoji表情导致的截取乱码的问题
val = hasMaxLength ? [...val].slice(0, maxLength).join('') : val;
// TextArea // TextArea
const textareaNode = ( const textareaNode = (
@ -116,7 +114,7 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>(
// Only show text area wrapper when needed // Only show text area wrapper when needed
if (showCount) { if (showCount) {
const valueLength = [...val].length; const valueLength = hasMaxLength ? Math.min(Number(maxLength), [...val].length) : [...val].length;
const dataCount = `${valueLength}${hasMaxLength ? ` / ${maxLength}` : ''}`; const dataCount = `${valueLength}${hasMaxLength ? ` / ${maxLength}` : ''}`;
return ( return (

View File

@ -140,7 +140,7 @@ describe('TextArea', () => {
it('maxLength', () => { it('maxLength', () => {
const wrapper = mount(<TextArea maxLength={5} showCount value="12345678" />); const wrapper = mount(<TextArea maxLength={5} showCount value="12345678" />);
const textarea = wrapper.find('.ant-input-textarea'); const textarea = wrapper.find('.ant-input-textarea');
expect(wrapper.find('textarea').prop('value')).toBe('12345'); expect(wrapper.find('textarea').prop('value')).toBe('12345678');
expect(textarea.prop('data-count')).toBe('5 / 5'); expect(textarea.prop('data-count')).toBe('5 / 5');
}); });