option.tsx 5.1 KB

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