index.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  3. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  4. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  5. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  6. import _typeof from "@babel/runtime/helpers/esm/typeof";
  7. var _excluded = ["prefixCls", "direction", "vertical", "options", "disabled", "defaultValue", "value", "name", "onChange", "className", "motionName"];
  8. import classNames from 'classnames';
  9. import useMergedState from "rc-util/es/hooks/useMergedState";
  10. import omit from "rc-util/es/omit";
  11. import { composeRef } from "rc-util/es/ref";
  12. import * as React from 'react';
  13. import MotionThumb from "./MotionThumb";
  14. function getValidTitle(option) {
  15. if (typeof option.title !== 'undefined') {
  16. return option.title;
  17. }
  18. // read `label` when title is `undefined`
  19. if (_typeof(option.label) !== 'object') {
  20. var _option$label;
  21. return (_option$label = option.label) === null || _option$label === void 0 ? void 0 : _option$label.toString();
  22. }
  23. }
  24. function normalizeOptions(options) {
  25. return options.map(function (option) {
  26. if (_typeof(option) === 'object' && option !== null) {
  27. var validTitle = getValidTitle(option);
  28. return _objectSpread(_objectSpread({}, option), {}, {
  29. title: validTitle
  30. });
  31. }
  32. return {
  33. label: option === null || option === void 0 ? void 0 : option.toString(),
  34. title: option === null || option === void 0 ? void 0 : option.toString(),
  35. value: option
  36. };
  37. });
  38. }
  39. var InternalSegmentedOption = function InternalSegmentedOption(_ref) {
  40. var prefixCls = _ref.prefixCls,
  41. className = _ref.className,
  42. disabled = _ref.disabled,
  43. checked = _ref.checked,
  44. label = _ref.label,
  45. title = _ref.title,
  46. value = _ref.value,
  47. name = _ref.name,
  48. onChange = _ref.onChange,
  49. onFocus = _ref.onFocus,
  50. onBlur = _ref.onBlur,
  51. onKeyDown = _ref.onKeyDown,
  52. onKeyUp = _ref.onKeyUp,
  53. onMouseDown = _ref.onMouseDown;
  54. var handleChange = function handleChange(event) {
  55. if (disabled) {
  56. return;
  57. }
  58. onChange(event, value);
  59. };
  60. return /*#__PURE__*/React.createElement("label", {
  61. className: classNames(className, _defineProperty({}, "".concat(prefixCls, "-item-disabled"), disabled)),
  62. onMouseDown: onMouseDown
  63. }, /*#__PURE__*/React.createElement("input", {
  64. name: name,
  65. className: "".concat(prefixCls, "-item-input"),
  66. type: "radio",
  67. disabled: disabled,
  68. checked: checked,
  69. onChange: handleChange,
  70. onFocus: onFocus,
  71. onBlur: onBlur,
  72. onKeyDown: onKeyDown,
  73. onKeyUp: onKeyUp
  74. }), /*#__PURE__*/React.createElement("div", {
  75. className: "".concat(prefixCls, "-item-label"),
  76. title: title,
  77. "aria-selected": checked
  78. }, label));
  79. };
  80. var Segmented = /*#__PURE__*/React.forwardRef(function (props, ref) {
  81. var _segmentedOptions$, _classNames2;
  82. var _props$prefixCls = props.prefixCls,
  83. prefixCls = _props$prefixCls === void 0 ? 'rc-segmented' : _props$prefixCls,
  84. direction = props.direction,
  85. vertical = props.vertical,
  86. _props$options = props.options,
  87. options = _props$options === void 0 ? [] : _props$options,
  88. disabled = props.disabled,
  89. defaultValue = props.defaultValue,
  90. value = props.value,
  91. name = props.name,
  92. onChange = props.onChange,
  93. _props$className = props.className,
  94. className = _props$className === void 0 ? '' : _props$className,
  95. _props$motionName = props.motionName,
  96. motionName = _props$motionName === void 0 ? 'thumb-motion' : _props$motionName,
  97. restProps = _objectWithoutProperties(props, _excluded);
  98. var containerRef = React.useRef(null);
  99. var mergedRef = React.useMemo(function () {
  100. return composeRef(containerRef, ref);
  101. }, [containerRef, ref]);
  102. var segmentedOptions = React.useMemo(function () {
  103. return normalizeOptions(options);
  104. }, [options]);
  105. // Note: We should not auto switch value when value not exist in options
  106. // which may break single source of truth.
  107. var _useMergedState = useMergedState((_segmentedOptions$ = segmentedOptions[0]) === null || _segmentedOptions$ === void 0 ? void 0 : _segmentedOptions$.value, {
  108. value: value,
  109. defaultValue: defaultValue
  110. }),
  111. _useMergedState2 = _slicedToArray(_useMergedState, 2),
  112. rawValue = _useMergedState2[0],
  113. setRawValue = _useMergedState2[1];
  114. // ======================= Change ========================
  115. var _React$useState = React.useState(false),
  116. _React$useState2 = _slicedToArray(_React$useState, 2),
  117. thumbShow = _React$useState2[0],
  118. setThumbShow = _React$useState2[1];
  119. var handleChange = function handleChange(event, val) {
  120. setRawValue(val);
  121. onChange === null || onChange === void 0 || onChange(val);
  122. };
  123. var divProps = omit(restProps, ['children']);
  124. // ======================= Focus ========================
  125. var _React$useState3 = React.useState(false),
  126. _React$useState4 = _slicedToArray(_React$useState3, 2),
  127. isKeyboard = _React$useState4[0],
  128. setIsKeyboard = _React$useState4[1];
  129. var _React$useState5 = React.useState(false),
  130. _React$useState6 = _slicedToArray(_React$useState5, 2),
  131. isFocused = _React$useState6[0],
  132. setIsFocused = _React$useState6[1];
  133. var handleFocus = function handleFocus() {
  134. setIsFocused(true);
  135. };
  136. var handleBlur = function handleBlur() {
  137. setIsFocused(false);
  138. };
  139. var handleMouseDown = function handleMouseDown() {
  140. setIsKeyboard(false);
  141. };
  142. // capture keyboard tab interaction for correct focus style
  143. var handleKeyUp = function handleKeyUp(event) {
  144. if (event.key === 'Tab') {
  145. setIsKeyboard(true);
  146. }
  147. };
  148. // ======================= Keyboard ========================
  149. var onOffset = function onOffset(offset) {
  150. var currentIndex = segmentedOptions.findIndex(function (option) {
  151. return option.value === rawValue;
  152. });
  153. var total = segmentedOptions.length;
  154. var nextIndex = (currentIndex + offset + total) % total;
  155. var nextOption = segmentedOptions[nextIndex];
  156. if (nextOption) {
  157. setRawValue(nextOption.value);
  158. onChange === null || onChange === void 0 || onChange(nextOption.value);
  159. }
  160. };
  161. var handleKeyDown = function handleKeyDown(event) {
  162. switch (event.key) {
  163. case 'ArrowLeft':
  164. case 'ArrowUp':
  165. onOffset(-1);
  166. break;
  167. case 'ArrowRight':
  168. case 'ArrowDown':
  169. onOffset(1);
  170. break;
  171. }
  172. };
  173. return /*#__PURE__*/React.createElement("div", _extends({
  174. role: "radiogroup",
  175. "aria-label": "segmented control",
  176. tabIndex: disabled ? undefined : 0
  177. }, divProps, {
  178. className: classNames(prefixCls, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _defineProperty(_classNames2, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames2, "".concat(prefixCls, "-vertical"), vertical), _classNames2), className),
  179. ref: mergedRef
  180. }), /*#__PURE__*/React.createElement("div", {
  181. className: "".concat(prefixCls, "-group")
  182. }, /*#__PURE__*/React.createElement(MotionThumb, {
  183. vertical: vertical,
  184. prefixCls: prefixCls,
  185. value: rawValue,
  186. containerRef: containerRef,
  187. motionName: "".concat(prefixCls, "-").concat(motionName),
  188. direction: direction,
  189. getValueIndex: function getValueIndex(val) {
  190. return segmentedOptions.findIndex(function (n) {
  191. return n.value === val;
  192. });
  193. },
  194. onMotionStart: function onMotionStart() {
  195. setThumbShow(true);
  196. },
  197. onMotionEnd: function onMotionEnd() {
  198. setThumbShow(false);
  199. }
  200. }), segmentedOptions.map(function (segmentedOption) {
  201. var _classNames3;
  202. return /*#__PURE__*/React.createElement(InternalSegmentedOption, _extends({}, segmentedOption, {
  203. name: name,
  204. key: segmentedOption.value,
  205. prefixCls: prefixCls,
  206. className: classNames(segmentedOption.className, "".concat(prefixCls, "-item"), (_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefixCls, "-item-selected"), segmentedOption.value === rawValue && !thumbShow), _defineProperty(_classNames3, "".concat(prefixCls, "-item-focused"), isFocused && isKeyboard && segmentedOption.value === rawValue), _classNames3)),
  207. checked: segmentedOption.value === rawValue,
  208. onChange: handleChange,
  209. onFocus: handleFocus,
  210. onBlur: handleBlur,
  211. onKeyDown: handleKeyDown,
  212. onKeyUp: handleKeyUp,
  213. onMouseDown: handleMouseDown,
  214. disabled: !!disabled || !!segmentedOption.disabled
  215. }));
  216. })));
  217. });
  218. if (process.env.NODE_ENV !== 'production') {
  219. Segmented.displayName = 'Segmented';
  220. }
  221. var TypedSegmented = Segmented;
  222. export default TypedSegmented;