option.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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';
  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 isDisabled = props.disabled;
  57. if (!isDisabled) {
  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. id,
  86. ...rest
  87. } = this.props;
  88. const optionClassName = classNames(prefixCls, {
  89. [`${prefixCls}-disabled`]: disabled,
  90. [`${prefixCls}-selected`]: selected,
  91. [`${prefixCls}-focused`]: focused,
  92. [`${prefixCls}-empty`]: empty,
  93. [className]: className,
  94. });
  95. const selectedIconClassName = classNames([`${prefixCls}-icon`]);
  96. if (empty) {
  97. if (emptyContent === null) {
  98. return null;
  99. }
  100. return (
  101. <LocaleConsumer<Locale['Select']> componentName="Select">
  102. {(locale: Locale['Select']) => (
  103. <div className={optionClassName} x-semi-prop="emptyContent">
  104. {emptyContent || locale.emptyText}
  105. </div>
  106. )}
  107. </LocaleConsumer>
  108. );
  109. }
  110. // 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
  111. if (typeof renderOptionItem === 'function') {
  112. return renderOptionItem({
  113. disabled,
  114. focused,
  115. selected,
  116. style,
  117. label,
  118. value,
  119. inputValue,
  120. onMouseEnter: (e: React.MouseEvent) => onMouseEnter(e),
  121. onClick: (e: React.MouseEvent) => this.onClick({ value, label, children, ...rest }, e),
  122. ...rest
  123. });
  124. }
  125. const config = {
  126. searchWords: inputValue,
  127. sourceString: children,
  128. option: {
  129. highlightClassName: `${prefixCls}-keyword`
  130. }
  131. };
  132. return (
  133. // eslint-disable-next-line jsx-a11y/interactive-supports-focus,jsx-a11y/click-events-have-key-events
  134. <div
  135. className={optionClassName}
  136. onClick={e => {
  137. this.onClick({ value, label, children, ...rest }, e);
  138. }}
  139. onMouseEnter={e => onMouseEnter && onMouseEnter(e)}
  140. role="option"
  141. id={id}
  142. aria-selected={selected ? "true" : "false"}
  143. aria-disabled={disabled ? "true" : "false"}
  144. style={style}
  145. >
  146. {showTick ? (
  147. <div className={selectedIconClassName}>
  148. <IconTick />
  149. </div>
  150. ) : null}
  151. {isString(children) ? <div className={`${prefixCls}-text`}>{this.renderOptionContent({ children, config, inputValue, prefixCls })}</div> : children}
  152. </div>
  153. );
  154. }
  155. }
  156. export default Option;