mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(Field): fix the length of emoji (#10033)
This commit is contained in:
parent
d5fdab55cc
commit
b893f911a9
@ -27,12 +27,14 @@ import {
|
|||||||
createNamespace,
|
createNamespace,
|
||||||
} from '../utils';
|
} from '../utils';
|
||||||
import {
|
import {
|
||||||
|
cutString,
|
||||||
runSyncRule,
|
runSyncRule,
|
||||||
endComposing,
|
endComposing,
|
||||||
mapInputType,
|
mapInputType,
|
||||||
startComposing,
|
startComposing,
|
||||||
getRuleMessage,
|
getRuleMessage,
|
||||||
resizeTextarea,
|
resizeTextarea,
|
||||||
|
getStringLength,
|
||||||
runRuleValidator,
|
runRuleValidator,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
import { cellSharedProps } from '../cell/Cell';
|
import { cellSharedProps } from '../cell/Cell';
|
||||||
@ -255,12 +257,12 @@ export default defineComponent({
|
|||||||
// see: https://github.com/youzan/vant/issues/5033
|
// see: https://github.com/youzan/vant/issues/5033
|
||||||
const limitValueLength = (value: string) => {
|
const limitValueLength = (value: string) => {
|
||||||
const { maxlength } = props;
|
const { maxlength } = props;
|
||||||
if (isDef(maxlength) && value.length > maxlength) {
|
if (isDef(maxlength) && getStringLength(value) > maxlength) {
|
||||||
const modelValue = getModelValue();
|
const modelValue = getModelValue();
|
||||||
if (modelValue && modelValue.length === +maxlength) {
|
if (modelValue && getStringLength(modelValue) === +maxlength) {
|
||||||
return modelValue;
|
return modelValue;
|
||||||
}
|
}
|
||||||
return value.slice(0, +maxlength);
|
return cutString(value, +maxlength);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
@ -462,7 +464,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
const renderWordLimit = () => {
|
const renderWordLimit = () => {
|
||||||
if (props.showWordLimit && props.maxlength) {
|
if (props.showWordLimit && props.maxlength) {
|
||||||
const count = getModelValue().length;
|
const count = getStringLength(getModelValue());
|
||||||
return (
|
return (
|
||||||
<div class={bem('word-limit')}>
|
<div class={bem('word-limit')}>
|
||||||
<span class={bem('word-num')}>{count}</span>/{props.maxlength}
|
<span class={bem('word-num')}>{count}</span>/{props.maxlength}
|
||||||
|
@ -62,21 +62,11 @@ exports[`should render textarea when type is textarea 1`] = `
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`should render word limit correctly 1`] = `
|
exports[`should render word limit correctly 1`] = `
|
||||||
<div class="van-cell van-field">
|
<div class="van-field__word-limit">
|
||||||
<div class="van-cell__value van-cell__value--alone van-field__value">
|
<span class="van-field__word-num">
|
||||||
<div class="van-field__body">
|
3
|
||||||
<input type="text"
|
</span>
|
||||||
id="van-field-input"
|
/3
|
||||||
class="van-field__control"
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
<div class="van-field__word-limit">
|
|
||||||
<span class="van-field__word-num">
|
|
||||||
3
|
|
||||||
</span>
|
|
||||||
/3
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
@ -117,3 +107,12 @@ exports[`should render word limit correctly when modelValue is undefined 1`] = `
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`should render word limit with emoji correctly 1`] = `
|
||||||
|
<div class="van-field__word-limit">
|
||||||
|
<span class="van-field__word-num">
|
||||||
|
2
|
||||||
|
</span>
|
||||||
|
/3
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
@ -343,7 +343,7 @@ test('should render word limit correctly', () => {
|
|||||||
showWordLimit: true,
|
showWordLimit: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
expect(wrapper.html()).toMatchSnapshot();
|
expect(wrapper.find('.van-field__word-limit').html()).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should render word limit correctly when modelValue is undefined', () => {
|
test('should render word limit correctly when modelValue is undefined', () => {
|
||||||
@ -475,3 +475,26 @@ test('should render error-message slot correctly', async () => {
|
|||||||
|
|
||||||
expect(wrapper.find('.van-field__error-message').html()).toMatchSnapshot();
|
expect(wrapper.find('.van-field__error-message').html()).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should limit maxlength with emoji correctly', async () => {
|
||||||
|
const wrapper = mount(Field, {
|
||||||
|
props: {
|
||||||
|
maxlength: 3,
|
||||||
|
modelValue: '😀😀😀😀',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const input = wrapper.find('input');
|
||||||
|
expect(input.element.value).toEqual('😀😀😀');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should render word limit with emoji correctly', () => {
|
||||||
|
const wrapper = mount(Field, {
|
||||||
|
props: {
|
||||||
|
modelValue: '😀😀',
|
||||||
|
maxlength: 3,
|
||||||
|
showWordLimit: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(wrapper.find('.van-field__word-limit').html()).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
@ -107,3 +107,14 @@ export function mapInputType(type: FieldType): {
|
|||||||
|
|
||||||
return { type };
|
return { type };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get correct length of emoji
|
||||||
|
// https://github.com/youzan/vant/issues/10032
|
||||||
|
export function getStringLength(str: string) {
|
||||||
|
return [...str].length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// cut string with emoji
|
||||||
|
export function cutString(str: string, maxlength: number) {
|
||||||
|
return [...str].slice(0, maxlength).join('');
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user