ClearableLabeledInput.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { createVNode as _createVNode } from "vue";
  2. import classNames from '../_util/classNames';
  3. import CloseCircleFilled from "@ant-design/icons-vue/es/icons/CloseCircleFilled";
  4. import PropTypes from '../_util/vue-types';
  5. import { cloneElement } from '../_util/vnode';
  6. import { defineComponent } from 'vue';
  7. import { anyType, tuple } from '../_util/type';
  8. import { hasAddon } from './util';
  9. import { FormItemInputContext } from '../form/FormItemContext';
  10. import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
  11. const ClearableInputType = ['text', 'input'];
  12. export default defineComponent({
  13. compatConfig: {
  14. MODE: 3
  15. },
  16. name: 'ClearableLabeledInput',
  17. inheritAttrs: false,
  18. props: {
  19. prefixCls: String,
  20. inputType: PropTypes.oneOf(tuple('text', 'input')),
  21. value: anyType(),
  22. defaultValue: anyType(),
  23. allowClear: {
  24. type: Boolean,
  25. default: undefined
  26. },
  27. element: anyType(),
  28. handleReset: Function,
  29. disabled: {
  30. type: Boolean,
  31. default: undefined
  32. },
  33. direction: {
  34. type: String
  35. },
  36. size: {
  37. type: String
  38. },
  39. suffix: anyType(),
  40. prefix: anyType(),
  41. addonBefore: anyType(),
  42. addonAfter: anyType(),
  43. readonly: {
  44. type: Boolean,
  45. default: undefined
  46. },
  47. focused: {
  48. type: Boolean,
  49. default: undefined
  50. },
  51. bordered: {
  52. type: Boolean,
  53. default: true
  54. },
  55. triggerFocus: {
  56. type: Function
  57. },
  58. hidden: Boolean,
  59. status: String,
  60. hashId: String
  61. },
  62. setup(props, _ref) {
  63. let {
  64. slots,
  65. attrs
  66. } = _ref;
  67. const statusContext = FormItemInputContext.useInject();
  68. const renderClearIcon = prefixCls => {
  69. const {
  70. value,
  71. disabled,
  72. readonly,
  73. handleReset,
  74. suffix = slots.suffix
  75. } = props;
  76. const needClear = !disabled && !readonly && value;
  77. const className = `${prefixCls}-clear-icon`;
  78. return _createVNode(CloseCircleFilled, {
  79. "onClick": handleReset,
  80. "onMousedown": e => e.preventDefault(),
  81. "class": classNames({
  82. [`${className}-hidden`]: !needClear,
  83. [`${className}-has-suffix`]: !!suffix
  84. }, className),
  85. "role": "button"
  86. }, null);
  87. };
  88. const renderTextAreaWithClearIcon = (prefixCls, element) => {
  89. const {
  90. value,
  91. allowClear,
  92. direction,
  93. bordered,
  94. hidden,
  95. status: customStatus,
  96. addonAfter = slots.addonAfter,
  97. addonBefore = slots.addonBefore,
  98. hashId
  99. } = props;
  100. const {
  101. status: contextStatus,
  102. hasFeedback
  103. } = statusContext;
  104. if (!allowClear) {
  105. return cloneElement(element, {
  106. value,
  107. disabled: props.disabled
  108. });
  109. }
  110. const affixWrapperCls = classNames(`${prefixCls}-affix-wrapper`, `${prefixCls}-affix-wrapper-textarea-with-clear-btn`, getStatusClassNames(`${prefixCls}-affix-wrapper`, getMergedStatus(contextStatus, customStatus), hasFeedback), {
  111. [`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl',
  112. [`${prefixCls}-affix-wrapper-borderless`]: !bordered,
  113. // className will go to addon wrapper
  114. [`${attrs.class}`]: !hasAddon({
  115. addonAfter,
  116. addonBefore
  117. }) && attrs.class
  118. }, hashId);
  119. return _createVNode("span", {
  120. "class": affixWrapperCls,
  121. "style": attrs.style,
  122. "hidden": hidden
  123. }, [cloneElement(element, {
  124. style: null,
  125. value,
  126. disabled: props.disabled
  127. }), renderClearIcon(prefixCls)]);
  128. };
  129. return () => {
  130. var _a;
  131. const {
  132. prefixCls,
  133. inputType,
  134. element = (_a = slots.element) === null || _a === void 0 ? void 0 : _a.call(slots)
  135. } = props;
  136. if (inputType === ClearableInputType[0]) {
  137. return renderTextAreaWithClearIcon(prefixCls, element);
  138. }
  139. return null;
  140. };
  141. }
  142. });