option.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React, { PureComponent } from 'react';
  2. import classNames from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { isString } from 'lodash';
  5. import { cssClasses } from '@douyinfe/semi-foundation/autoComplete/constants';
  6. import LocaleConsumer from '../locale/localeConsumer';
  7. import { IconTick } from '@douyinfe/semi-icons';
  8. import Highlight from '../highlight';
  9. import { Locale } from '../locale/interface';
  10. import { BasicOptionProps } from '@douyinfe/semi-foundation/autoComplete/optionFoundation';
  11. export interface OptionProps extends BasicOptionProps {
  12. [x: string]: any;
  13. value?: string | number;
  14. label?: string | number | React.ReactNode;
  15. children?: React.ReactNode;
  16. disabled?: boolean;
  17. showTick?: boolean;
  18. className?: string;
  19. style?: React.CSSProperties
  20. }
  21. class Option extends PureComponent<OptionProps> {
  22. static isSelectOption = true;
  23. static propTypes = {
  24. children: PropTypes.node,
  25. disabled: PropTypes.bool,
  26. value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  27. selected: PropTypes.bool,
  28. label: PropTypes.node,
  29. empty: PropTypes.bool,
  30. emptyContent: PropTypes.node,
  31. onSelect: PropTypes.func,
  32. focused: PropTypes.bool,
  33. showTick: PropTypes.bool,
  34. className: PropTypes.string,
  35. style: PropTypes.object,
  36. onMouseEnter: PropTypes.func,
  37. prefixCls: PropTypes.string,
  38. renderOptionItem: PropTypes.func,
  39. inputValue: PropTypes.string,
  40. };
  41. static defaultProps = {
  42. prefixCls: cssClasses.PREFIX_OPTION
  43. };
  44. onClick({ value, label, children, ...rest }: Partial<OptionProps>, event: React.MouseEvent) {
  45. const { props } = this;
  46. const isDisabled = props.disabled;
  47. if (!isDisabled) {
  48. props.onSelect({ ...rest, value, label: label || children }, event);
  49. }
  50. }
  51. renderOptionContent({ children, inputValue, prefixCls }) {
  52. if (isString(children) && inputValue) {
  53. return (<Highlight
  54. searchWords={[inputValue]}
  55. sourceString={children}
  56. highlightClassName={`${prefixCls}-keyword`}
  57. />);
  58. }
  59. return children;
  60. }
  61. render() {
  62. const {
  63. children,
  64. disabled,
  65. value,
  66. selected,
  67. label,
  68. empty,
  69. emptyContent,
  70. onSelect,
  71. focused,
  72. showTick,
  73. className,
  74. style,
  75. onMouseEnter,
  76. prefixCls,
  77. renderOptionItem,
  78. inputValue,
  79. ...rest
  80. } = this.props;
  81. const optionClassName = classNames(prefixCls, {
  82. [`${prefixCls}-disabled`]: disabled,
  83. [`${prefixCls}-selected`]: selected,
  84. [`${prefixCls}-focused`]: focused,
  85. [`${prefixCls}-empty`]: empty,
  86. [className]: className,
  87. });
  88. const selectedIconClassName = classNames([`${prefixCls}-icon`]);
  89. if (empty) {
  90. if (emptyContent === null) {
  91. return null;
  92. }
  93. return (
  94. <LocaleConsumer<Locale['Select']> componentName="Select">
  95. {(locale: Locale['Select']) => (
  96. <div className={optionClassName} x-semi-prop="emptyContent">
  97. {emptyContent || locale.emptyText}
  98. </div>
  99. )}
  100. </LocaleConsumer>
  101. );
  102. }
  103. // Since there are empty, locale and other logic, the custom renderOptionItem is directly converged to the internal option instead of being placed in Select/index
  104. if (typeof renderOptionItem === 'function') {
  105. return renderOptionItem({
  106. disabled,
  107. focused,
  108. selected,
  109. style,
  110. label,
  111. value,
  112. inputValue,
  113. onMouseEnter: (e: React.MouseEvent) => onMouseEnter(e),
  114. onClick: (e: React.MouseEvent) => this.onClick({ value, label, children, ...rest }, e),
  115. ...rest
  116. });
  117. }
  118. return (
  119. // eslint-disable-next-line jsx-a11y/interactive-supports-focus,jsx-a11y/click-events-have-key-events
  120. <div
  121. className={optionClassName}
  122. onClick={e => {
  123. this.onClick({ value, label, children, ...rest }, e);
  124. }}
  125. onMouseEnter={e => onMouseEnter && onMouseEnter(e)}
  126. role="option"
  127. aria-selected={selected ? "true" : "false"}
  128. aria-disabled={disabled ? "true" : "false"}
  129. style={style}
  130. >
  131. {showTick ? (
  132. <div className={selectedIconClassName}>
  133. <IconTick />
  134. </div>
  135. ) : null}
  136. {isString(children) ? <div className={`${prefixCls}-text`}>{this.renderOptionContent({ children, inputValue, prefixCls })}</div> : children}
  137. </div>
  138. );
  139. }
  140. }
  141. export default Option;