option.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 { getHighLightTextHTML } from '../_utils/index';
  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. interface renderOptionContentArgument {
  22. config: {
  23. searchWords: any;
  24. sourceString: React.ReactNode
  25. };
  26. children: React.ReactNode;
  27. inputValue: string;
  28. prefixCls: string
  29. }
  30. class Option extends PureComponent<OptionProps> {
  31. static isSelectOption = true;
  32. static propTypes = {
  33. children: PropTypes.node,
  34. disabled: PropTypes.bool,
  35. value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  36. selected: PropTypes.bool,
  37. label: PropTypes.node,
  38. empty: PropTypes.bool,
  39. emptyContent: PropTypes.node,
  40. onSelect: PropTypes.func,
  41. focused: PropTypes.bool,
  42. showTick: PropTypes.bool,
  43. className: PropTypes.string,
  44. style: PropTypes.object,
  45. onMouseEnter: PropTypes.func,
  46. prefixCls: PropTypes.string,
  47. renderOptionItem: PropTypes.func,
  48. inputValue: PropTypes.string,
  49. };
  50. static defaultProps = {
  51. prefixCls: cssClasses.PREFIX_OPTION
  52. };
  53. onClick({ value, label, children, ...rest }: Partial<OptionProps>, event: React.MouseEvent) {
  54. const { props } = this;
  55. const isDisabled = props.disabled;
  56. if (!isDisabled) {
  57. props.onSelect({ ...rest, value, label: label || children }, event);
  58. }
  59. }
  60. renderOptionContent({ config, children, inputValue, prefixCls }: renderOptionContentArgument) {
  61. if (isString(children) && inputValue) {
  62. return getHighLightTextHTML(config as any);
  63. }
  64. return children;
  65. }
  66. render() {
  67. const {
  68. children,
  69. disabled,
  70. value,
  71. selected,
  72. label,
  73. empty,
  74. emptyContent,
  75. onSelect,
  76. focused,
  77. showTick,
  78. className,
  79. style,
  80. onMouseEnter,
  81. prefixCls,
  82. renderOptionItem,
  83. inputValue,
  84. ...rest
  85. } = this.props;
  86. const optionClassName = classNames(prefixCls, {
  87. [`${prefixCls}-disabled`]: disabled,
  88. [`${prefixCls}-selected`]: selected,
  89. [`${prefixCls}-focused`]: focused,
  90. [`${prefixCls}-empty`]: empty,
  91. [className]: className,
  92. });
  93. const selectedIconClassName = classNames([`${prefixCls}-icon`]);
  94. if (empty) {
  95. if (emptyContent === null) {
  96. return null;
  97. }
  98. return (
  99. <LocaleConsumer<Locale['Select']> componentName="Select">
  100. {(locale: Locale['Select']) => (
  101. <div className={optionClassName} x-semi-prop="emptyContent">
  102. {emptyContent || locale.emptyText}
  103. </div>
  104. )}
  105. </LocaleConsumer>
  106. );
  107. }
  108. // 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
  109. if (typeof renderOptionItem === 'function') {
  110. return renderOptionItem({
  111. disabled,
  112. focused,
  113. selected,
  114. style,
  115. label,
  116. value,
  117. inputValue,
  118. onMouseEnter: (e: React.MouseEvent) => onMouseEnter(e),
  119. onClick: (e: React.MouseEvent) => this.onClick({ value, label, children, ...rest }, e),
  120. ...rest
  121. });
  122. }
  123. const config = {
  124. searchWords: inputValue,
  125. sourceString: children,
  126. option: {
  127. highlightClassName: `${prefixCls}-keyword`
  128. }
  129. };
  130. return (
  131. // eslint-disable-next-line jsx-a11y/interactive-supports-focus,jsx-a11y/click-events-have-key-events
  132. <div
  133. className={optionClassName}
  134. onClick={e => {
  135. this.onClick({ value, label, children, ...rest }, e);
  136. }}
  137. onMouseEnter={e => onMouseEnter && onMouseEnter(e)}
  138. role="option"
  139. aria-selected={selected ? "true" : "false"}
  140. aria-disabled={disabled ? "true" : "false"}
  141. style={style}
  142. >
  143. {showTick ? (
  144. <div className={selectedIconClassName}>
  145. <IconTick />
  146. </div>
  147. ) : null}
  148. {isString(children) ? <div className={`${prefixCls}-text`}>{this.renderOptionContent({ children, config, inputValue, prefixCls })}</div> : children}
  149. </div>
  150. );
  151. }
  152. }
  153. export default Option;