|
| 1 | +import './style.scss'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { useRef, useState, Children, isValidElement, useEffect } from 'react'; |
| 4 | +import { |
| 5 | + prefixClaName, |
| 6 | + classNames, |
| 7 | + getBEMElement, |
| 8 | + getBEMModifier, |
| 9 | +} from 'mo/common/className'; |
| 10 | +import { cloneReactChildren } from 'mo/react'; |
| 11 | +import { useContextView } from 'mo/components/contextview'; |
| 12 | + |
| 13 | +import { ISelectOption } from './option'; |
| 14 | +import { Icon } from '../icon'; |
| 15 | + |
| 16 | +export interface ISelect { |
| 17 | + value?: string; |
| 18 | + title?: string; |
| 19 | + style?: React.CSSProperties; |
| 20 | + className?: string; |
| 21 | + defaultValue?: string; |
| 22 | + placeholder?: string; |
| 23 | + prefix?: ReactNode; |
| 24 | + showArrow?: boolean; |
| 25 | + children?: ReactNode; |
| 26 | + onSelect?(e: React.MouseEvent, selectedOption?: ISelectOption): void; |
| 27 | +} |
| 28 | + |
| 29 | +export const selectClassName = prefixClaName('select'); |
| 30 | +const containerClassName = getBEMElement(selectClassName, 'container'); |
| 31 | +const selectOptionsClassName = getBEMElement(selectClassName, 'options'); |
| 32 | +const selectDescriptorClassName = getBEMElement(selectClassName, 'descriptor'); |
| 33 | +const inputClassName = getBEMElement(selectClassName, 'input'); |
| 34 | +const selectActiveClassName = getBEMModifier(selectClassName, 'active'); |
| 35 | +const selectArrowClassName = getBEMElement(selectClassName, 'arrow'); |
| 36 | + |
| 37 | +export function Select(props: ISelect) { |
| 38 | + const { |
| 39 | + className, |
| 40 | + children, |
| 41 | + defaultValue = '', |
| 42 | + placeholder, |
| 43 | + value, |
| 44 | + title, |
| 45 | + onSelect, |
| 46 | + ...custom |
| 47 | + } = props; |
| 48 | + |
| 49 | + const contextView = useContextView({ |
| 50 | + shadowOutline: false, |
| 51 | + }); |
| 52 | + |
| 53 | + const defaultSelectedOption: ISelectOption = {}; |
| 54 | + const options = Children.toArray(children); |
| 55 | + for (const option of options) { |
| 56 | + if (isValidElement(option)) { |
| 57 | + const optionProps = option.props as ISelectOption; |
| 58 | + if (optionProps.value && optionProps.value === defaultValue) { |
| 59 | + defaultSelectedOption.title = optionProps.children as string; |
| 60 | + defaultSelectedOption.value = optionProps.value; |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + const selectElm = useRef<HTMLDivElement>(null); |
| 67 | + const selectInput = useRef<HTMLInputElement>(null); |
| 68 | + const [isOpen, setIsOpen] = useState(false); |
| 69 | + const [inputValue, setInputValue] = useState(defaultSelectedOption); |
| 70 | + |
| 71 | + const handleOnClickOption = (e: React.MouseEvent) => { |
| 72 | + const option = e.target as HTMLDivElement; |
| 73 | + const value = option.getAttribute('data-value'); |
| 74 | + const title = option.getAttribute('title'); |
| 75 | + const desc = option.getAttribute('data-desc'); |
| 76 | + const optionItem = { |
| 77 | + value: value!, |
| 78 | + title: title!, |
| 79 | + description: desc!, |
| 80 | + }; |
| 81 | + |
| 82 | + setInputValue(optionItem); |
| 83 | + onSelect?.(e, optionItem); |
| 84 | + setIsOpen(false); |
| 85 | + contextView.hide(); |
| 86 | + }; |
| 87 | + |
| 88 | + const handOnHoverOption = (e: React.MouseEvent) => { |
| 89 | + const option = e.target as HTMLDivElement; |
| 90 | + const desc = option.getAttribute('data-desc'); |
| 91 | + const descriptor = contextView.view!.querySelector( |
| 92 | + '.' + selectDescriptorClassName |
| 93 | + ); |
| 94 | + if (descriptor) { |
| 95 | + const content = desc || 'None'; |
| 96 | + descriptor.innerHTML = content; |
| 97 | + descriptor.setAttribute('title', content); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + const events = { |
| 102 | + onClick: (e: React.MouseEvent) => { |
| 103 | + const select = selectElm.current; |
| 104 | + if (select) { |
| 105 | + const selectRect = select?.getBoundingClientRect(); |
| 106 | + selectRect.y = selectRect.y + selectRect.height; |
| 107 | + setIsOpen(true); |
| 108 | + |
| 109 | + contextView.show(selectRect, () => { |
| 110 | + return ( |
| 111 | + <div |
| 112 | + style={{ |
| 113 | + width: selectRect.width, |
| 114 | + }} |
| 115 | + className={classNames( |
| 116 | + containerClassName, |
| 117 | + selectActiveClassName |
| 118 | + )} |
| 119 | + onMouseOver={handOnHoverOption} |
| 120 | + > |
| 121 | + <div className={selectOptionsClassName}> |
| 122 | + {cloneReactChildren<ISelectOption>(children, { |
| 123 | + onClick: handleOnClickOption, |
| 124 | + })} |
| 125 | + </div> |
| 126 | + <div className={selectDescriptorClassName}> |
| 127 | + None |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + ); |
| 131 | + }); |
| 132 | + } |
| 133 | + }, |
| 134 | + }; |
| 135 | + |
| 136 | + const selectActive = isOpen ? selectActiveClassName : ''; |
| 137 | + const claNames = classNames(selectClassName, className, selectActive); |
| 138 | + |
| 139 | + useEffect(() => { |
| 140 | + contextView.onHide(() => { |
| 141 | + setIsOpen(false); |
| 142 | + }); |
| 143 | + |
| 144 | + return () => { |
| 145 | + contextView.dispose(); |
| 146 | + }; |
| 147 | + }, [isOpen, inputValue]); |
| 148 | + |
| 149 | + return ( |
| 150 | + <div ref={selectElm} className={claNames} {...(custom as any)}> |
| 151 | + <input |
| 152 | + {...events} |
| 153 | + ref={selectInput} |
| 154 | + autoComplete="off" |
| 155 | + placeholder={placeholder} |
| 156 | + className={inputClassName} |
| 157 | + value={inputValue.title} |
| 158 | + readOnly |
| 159 | + > |
| 160 | + {title} |
| 161 | + </input> |
| 162 | + <span className={selectArrowClassName}> |
| 163 | + <Icon type={'chevron-down'} /> |
| 164 | + </span> |
| 165 | + </div> |
| 166 | + ); |
| 167 | +} |
0 commit comments