2024-08-20 23:25:37 +04:00

92 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export function hasAddon(props) {
return !!(props.addonBefore || props.addonAfter);
}
export function hasPrefixSuffix(props) {
return !!(props.prefix || props.suffix || props.allowClear);
}
// TODO: It's better to use `Proxy` replace the `element.value`. But we still need support IE11.
function cloneEvent(event, target, value) {
// A bug report filed on WebKit's Bugzilla tracker, dating back to 2009, specifically addresses the issue of cloneNode() not copying files of <input type="file"> elements.
// As of the last update, this bug was still marked as "NEW," indicating that it might not have been resolved yet.
// https://bugs.webkit.org/show_bug.cgi?id=28123
var currentTarget = target.cloneNode(true);
// click clear icon
var newEvent = Object.create(event, {
target: {
value: currentTarget
},
currentTarget: {
value: currentTarget
}
});
// Fill data
currentTarget.value = value;
// Fill selection. Some type like `email` not support selection
// https://github.com/ant-design/ant-design/issues/47833
if (typeof target.selectionStart === 'number' && typeof target.selectionEnd === 'number') {
currentTarget.selectionStart = target.selectionStart;
currentTarget.selectionEnd = target.selectionEnd;
}
currentTarget.setSelectionRange = function () {
target.setSelectionRange.apply(target, arguments);
};
return newEvent;
}
export function resolveOnChange(target, e, onChange, targetValue) {
if (!onChange) {
return;
}
var event = e;
if (e.type === 'click') {
// Clone a new target for event.
// Avoid the following usage, the setQuery method gets the original value.
//
// const [query, setQuery] = React.useState('');
// <Input
// allowClear
// value={query}
// onChange={(e)=> {
// setQuery((prevStatus) => e.target.value);
// }}
// />
event = cloneEvent(e, target, '');
onChange(event);
return;
}
// Trigger by composition event, this means we need force change the input value
// https://github.com/ant-design/ant-design/issues/45737
// https://github.com/ant-design/ant-design/issues/46598
if (target.type !== 'file' && targetValue !== undefined) {
event = cloneEvent(e, target, targetValue);
onChange(event);
return;
}
onChange(event);
}
export function triggerFocus(element, option) {
if (!element) return;
element.focus(option);
// Selection content
var _ref = option || {},
cursor = _ref.cursor;
if (cursor) {
var len = element.value.length;
switch (cursor) {
case 'start':
element.setSelectionRange(0, 0);
break;
case 'end':
element.setSelectionRange(len, len);
break;
default:
element.setSelectionRange(0, len);
}
}
}