Star.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React from 'react';
  2. import KeyCode from "rc-util/es/KeyCode";
  3. import classNames from 'classnames';
  4. function Star(props, ref) {
  5. var disabled = props.disabled,
  6. prefixCls = props.prefixCls,
  7. character = props.character,
  8. characterRender = props.characterRender,
  9. index = props.index,
  10. count = props.count,
  11. value = props.value,
  12. allowHalf = props.allowHalf,
  13. focused = props.focused,
  14. onHover = props.onHover,
  15. onClick = props.onClick;
  16. // =========================== Events ===========================
  17. var onInternalHover = function onInternalHover(e) {
  18. onHover(e, index);
  19. };
  20. var onInternalClick = function onInternalClick(e) {
  21. onClick(e, index);
  22. };
  23. var onInternalKeyDown = function onInternalKeyDown(e) {
  24. if (e.keyCode === KeyCode.ENTER) {
  25. onClick(e, index);
  26. }
  27. };
  28. // =========================== Render ===========================
  29. // >>>>> ClassName
  30. var starValue = index + 1;
  31. var classNameList = new Set([prefixCls]);
  32. // TODO: Current we just refactor from CC to FC. This logic seems can be optimized.
  33. if (value === 0 && index === 0 && focused) {
  34. classNameList.add("".concat(prefixCls, "-focused"));
  35. } else if (allowHalf && value + 0.5 >= starValue && value < starValue) {
  36. classNameList.add("".concat(prefixCls, "-half"));
  37. classNameList.add("".concat(prefixCls, "-active"));
  38. if (focused) {
  39. classNameList.add("".concat(prefixCls, "-focused"));
  40. }
  41. } else {
  42. if (starValue <= value) {
  43. classNameList.add("".concat(prefixCls, "-full"));
  44. } else {
  45. classNameList.add("".concat(prefixCls, "-zero"));
  46. }
  47. if (starValue === value && focused) {
  48. classNameList.add("".concat(prefixCls, "-focused"));
  49. }
  50. }
  51. // >>>>> Node
  52. var characterNode = typeof character === 'function' ? character(props) : character;
  53. var start = /*#__PURE__*/React.createElement("li", {
  54. className: classNames(Array.from(classNameList)),
  55. ref: ref
  56. }, /*#__PURE__*/React.createElement("div", {
  57. onClick: disabled ? null : onInternalClick,
  58. onKeyDown: disabled ? null : onInternalKeyDown,
  59. onMouseMove: disabled ? null : onInternalHover,
  60. role: "radio",
  61. "aria-checked": value > index ? 'true' : 'false',
  62. "aria-posinset": index + 1,
  63. "aria-setsize": count,
  64. tabIndex: disabled ? -1 : 0
  65. }, /*#__PURE__*/React.createElement("div", {
  66. className: "".concat(prefixCls, "-first")
  67. }, characterNode), /*#__PURE__*/React.createElement("div", {
  68. className: "".concat(prefixCls, "-second")
  69. }, characterNode)));
  70. if (characterRender) {
  71. start = characterRender(start, props);
  72. }
  73. return start;
  74. }
  75. export default /*#__PURE__*/React.forwardRef(Star);