option.tsx 5.8 KB

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