FormItemLabel.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  2. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  3. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  4. import { createVNode as _createVNode, Fragment as _Fragment } from "vue";
  5. import Col from '../grid/Col';
  6. import { useInjectForm } from './context';
  7. import { useLocaleReceiver } from '../locale-provider/LocaleReceiver';
  8. import defaultLocale from '../locale/default';
  9. import classNames from '../_util/classNames';
  10. var FormItemLabel = function FormItemLabel(props, _ref) {
  11. var _props$label, _slots$label, _slots$tooltip, _classNames2;
  12. var slots = _ref.slots,
  13. emit = _ref.emit,
  14. attrs = _ref.attrs;
  15. var _props$attrs = _objectSpread(_objectSpread({}, props), attrs),
  16. prefixCls = _props$attrs.prefixCls,
  17. htmlFor = _props$attrs.htmlFor,
  18. labelCol = _props$attrs.labelCol,
  19. labelAlign = _props$attrs.labelAlign,
  20. colon = _props$attrs.colon,
  21. required = _props$attrs.required,
  22. requiredMark = _props$attrs.requiredMark;
  23. var _useLocaleReceiver = useLocaleReceiver('Form'),
  24. _useLocaleReceiver2 = _slicedToArray(_useLocaleReceiver, 1),
  25. formLocale = _useLocaleReceiver2[0];
  26. var label = (_props$label = props.label) !== null && _props$label !== void 0 ? _props$label : (_slots$label = slots.label) === null || _slots$label === void 0 ? void 0 : _slots$label.call(slots);
  27. if (!label) return null;
  28. var _useInjectForm = useInjectForm(),
  29. vertical = _useInjectForm.vertical,
  30. contextLabelAlign = _useInjectForm.labelAlign,
  31. contextLabelCol = _useInjectForm.labelCol,
  32. labelWrap = _useInjectForm.labelWrap,
  33. contextColon = _useInjectForm.colon;
  34. var mergedLabelCol = labelCol || (contextLabelCol === null || contextLabelCol === void 0 ? void 0 : contextLabelCol.value) || {};
  35. var mergedLabelAlign = labelAlign || (contextLabelAlign === null || contextLabelAlign === void 0 ? void 0 : contextLabelAlign.value);
  36. var labelClsBasic = "".concat(prefixCls, "-item-label");
  37. var labelColClassName = classNames(labelClsBasic, mergedLabelAlign === 'left' && "".concat(labelClsBasic, "-left"), mergedLabelCol.class, _defineProperty({}, "".concat(labelClsBasic, "-wrap"), !!labelWrap.value));
  38. var labelChildren = label;
  39. // Keep label is original where there should have no colon
  40. var computedColon = colon === true || (contextColon === null || contextColon === void 0 ? void 0 : contextColon.value) !== false && colon !== false;
  41. var haveColon = computedColon && !vertical.value;
  42. // Remove duplicated user input colon
  43. if (haveColon && typeof label === 'string' && label.trim() !== '') {
  44. labelChildren = label.replace(/[:|:]\s*$/, '');
  45. }
  46. labelChildren = _createVNode(_Fragment, null, [labelChildren, (_slots$tooltip = slots.tooltip) === null || _slots$tooltip === void 0 ? void 0 : _slots$tooltip.call(slots, {
  47. class: "".concat(prefixCls, "-item-tooltip")
  48. })]);
  49. // Add required mark if optional
  50. if (requiredMark === 'optional' && !required) {
  51. var _formLocale$value, _defaultLocale$Form;
  52. labelChildren = _createVNode(_Fragment, null, [labelChildren, _createVNode("span", {
  53. "class": "".concat(prefixCls, "-item-optional")
  54. }, [((_formLocale$value = formLocale.value) === null || _formLocale$value === void 0 ? void 0 : _formLocale$value.optional) || ((_defaultLocale$Form = defaultLocale.Form) === null || _defaultLocale$Form === void 0 ? void 0 : _defaultLocale$Form.optional)])]);
  55. }
  56. var labelClassName = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-item-required"), required), _defineProperty(_classNames2, "".concat(prefixCls, "-item-required-mark-optional"), requiredMark === 'optional'), _defineProperty(_classNames2, "".concat(prefixCls, "-item-no-colon"), !computedColon), _classNames2));
  57. return _createVNode(Col, _objectSpread(_objectSpread({}, mergedLabelCol), {}, {
  58. "class": labelColClassName
  59. }), {
  60. default: function _default() {
  61. return [_createVNode("label", {
  62. "for": htmlFor,
  63. "class": labelClassName,
  64. "title": typeof label === 'string' ? label : '',
  65. "onClick": function onClick(e) {
  66. return emit('click', e);
  67. }
  68. }, [labelChildren])];
  69. }
  70. });
  71. };
  72. FormItemLabel.displayName = 'FormItemLabel';
  73. FormItemLabel.inheritAttrs = false;
  74. export default FormItemLabel;