index.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import classNames from 'classnames';
  12. import RcSegmented from 'rc-segmented';
  13. import useId from "rc-util/es/hooks/useId";
  14. import { useComponentConfig } from '../config-provider/context';
  15. import useSize from '../config-provider/hooks/useSize';
  16. import useStyle from './style';
  17. function isSegmentedLabeledOptionWithIcon(option) {
  18. return typeof option === 'object' && !!(option === null || option === void 0 ? void 0 : option.icon);
  19. }
  20. const InternalSegmented = /*#__PURE__*/React.forwardRef((props, ref) => {
  21. const defaultName = useId();
  22. const {
  23. prefixCls: customizePrefixCls,
  24. className,
  25. rootClassName,
  26. block,
  27. options = [],
  28. size: customSize = 'middle',
  29. style,
  30. vertical,
  31. shape = 'default',
  32. name = defaultName
  33. } = props,
  34. restProps = __rest(props, ["prefixCls", "className", "rootClassName", "block", "options", "size", "style", "vertical", "shape", "name"]);
  35. const {
  36. getPrefixCls,
  37. direction,
  38. className: contextClassName,
  39. style: contextStyle
  40. } = useComponentConfig('segmented');
  41. const prefixCls = getPrefixCls('segmented', customizePrefixCls);
  42. // Style
  43. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  44. // ===================== Size =====================
  45. const mergedSize = useSize(customSize);
  46. // syntactic sugar to support `icon` for Segmented Item
  47. const extendedOptions = React.useMemo(() => options.map(option => {
  48. if (isSegmentedLabeledOptionWithIcon(option)) {
  49. const {
  50. icon,
  51. label
  52. } = option,
  53. restOption = __rest(option, ["icon", "label"]);
  54. return Object.assign(Object.assign({}, restOption), {
  55. label: (/*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
  56. className: `${prefixCls}-item-icon`
  57. }, icon), label && /*#__PURE__*/React.createElement("span", null, label)))
  58. });
  59. }
  60. return option;
  61. }), [options, prefixCls]);
  62. const cls = classNames(className, rootClassName, contextClassName, {
  63. [`${prefixCls}-block`]: block,
  64. [`${prefixCls}-sm`]: mergedSize === 'small',
  65. [`${prefixCls}-lg`]: mergedSize === 'large',
  66. [`${prefixCls}-vertical`]: vertical,
  67. [`${prefixCls}-shape-${shape}`]: shape === 'round'
  68. }, hashId, cssVarCls);
  69. const mergedStyle = Object.assign(Object.assign({}, contextStyle), style);
  70. return wrapCSSVar(/*#__PURE__*/React.createElement(RcSegmented, Object.assign({}, restProps, {
  71. name: name,
  72. className: cls,
  73. style: mergedStyle,
  74. options: extendedOptions,
  75. ref: ref,
  76. prefixCls: prefixCls,
  77. direction: direction,
  78. vertical: vertical
  79. })));
  80. });
  81. const Segmented = InternalSegmented;
  82. if (process.env.NODE_ENV !== 'production') {
  83. Segmented.displayName = 'Segmented';
  84. }
  85. export default Segmented;