|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2019 Google, Inc. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | + |
| 23 | +import * as React from 'react'; |
| 24 | +import NativeSelect, { |
| 25 | + NativeSelectProps, // eslint-disable-line no-unused-vars |
| 26 | +} from './NativeSelect'; |
| 27 | +import EnhancedSelect, { |
| 28 | + EnhancedSelectProps, // eslint-disable-line no-unused-vars |
| 29 | +} from './EnhancedSelect'; |
| 30 | +import {MDCSelectFoundation} from '@material/select/foundation'; |
| 31 | + |
| 32 | +export type BaseSelectProps<T extends HTMLElement> |
| 33 | + = (T extends HTMLSelectElement ? NativeSelectProps : EnhancedSelectProps); |
| 34 | + |
| 35 | +export interface CommonSelectProps { |
| 36 | + enhanced: boolean; |
| 37 | + className?: string; |
| 38 | + disabled?: boolean; |
| 39 | + foundation?: MDCSelectFoundation; |
| 40 | + value?: string; |
| 41 | + selectClassName?: string; |
| 42 | +} |
| 43 | + |
| 44 | +export class BaseSelect<T extends HTMLElement = HTMLSelectElement> |
| 45 | + extends React.Component<BaseSelectProps<T>> { |
| 46 | + static defaultProps = { |
| 47 | + enhanced: false, |
| 48 | + selectClassName: '', |
| 49 | + }; |
| 50 | + |
| 51 | + handleFocus = (evt: React.FocusEvent<T>) => { |
| 52 | + const {foundation, onFocus} = this.props; |
| 53 | + if (foundation) { |
| 54 | + foundation.handleFocus(); |
| 55 | + } |
| 56 | + onFocus && onFocus(evt); |
| 57 | + }; |
| 58 | + |
| 59 | + handleBlur = (evt: React.FocusEvent<T>) => { |
| 60 | + const {foundation, onBlur} = this.props; |
| 61 | + if (foundation) { |
| 62 | + foundation.handleBlur(); |
| 63 | + } |
| 64 | + onBlur && onBlur(evt); |
| 65 | + }; |
| 66 | + |
| 67 | + handleTouchStart = (evt: React.TouchEvent<T>) => { |
| 68 | + const {foundation, onTouchStart} = this.props; |
| 69 | + if (foundation) { |
| 70 | + foundation.handleClick(this.getNormalizedXCoordinate(evt)); |
| 71 | + } |
| 72 | + onTouchStart && onTouchStart(evt); |
| 73 | + } |
| 74 | + |
| 75 | + handleMouseDown = (evt: React.MouseEvent<T>) => { |
| 76 | + const {foundation, onMouseDown} = this.props; |
| 77 | + if (foundation) { |
| 78 | + foundation.handleClick(this.getNormalizedXCoordinate(evt)); |
| 79 | + } |
| 80 | + onMouseDown && onMouseDown(evt); |
| 81 | + } |
| 82 | + |
| 83 | + handleClick = (evt: React.MouseEvent<T>) => { |
| 84 | + const {foundation, onClick} = this.props; |
| 85 | + if (foundation) { |
| 86 | + foundation.handleClick(this.getNormalizedXCoordinate(evt)); |
| 87 | + } |
| 88 | + onClick && onClick(evt); |
| 89 | + } |
| 90 | + |
| 91 | + handleKeyDown = (evt: React.KeyboardEvent<T>) => { |
| 92 | + const {foundation, onKeyDown} = this.props; |
| 93 | + if (foundation) { |
| 94 | + foundation.handleKeydown(evt.nativeEvent); |
| 95 | + } |
| 96 | + onKeyDown && onKeyDown(evt); |
| 97 | + } |
| 98 | + |
| 99 | + private isTouchEvent = (evt: MouseEvent | TouchEvent): evt is TouchEvent => { |
| 100 | + return Boolean((evt as TouchEvent).touches); |
| 101 | + } |
| 102 | + |
| 103 | + private getNormalizedXCoordinate |
| 104 | + = (evt: React.MouseEvent<T> | React.TouchEvent<T>) => { |
| 105 | + const targetClientRect = (evt.currentTarget as Element).getBoundingClientRect(); |
| 106 | + const xCoordinate |
| 107 | + = this.isTouchEvent(evt.nativeEvent) ? evt.nativeEvent.touches[0].clientX : evt.nativeEvent.clientX; |
| 108 | + return xCoordinate - targetClientRect.left; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + render() { |
| 113 | + const { |
| 114 | + /* eslint-disable no-unused-vars */ |
| 115 | + onFocus, |
| 116 | + onBlur, |
| 117 | + onClick, |
| 118 | + onMouseDown, |
| 119 | + onTouchStart, |
| 120 | + ref, |
| 121 | + /* eslint-enable no-unused-vars */ |
| 122 | + enhanced, |
| 123 | + children, |
| 124 | + onKeyDown, |
| 125 | + selectClassName, |
| 126 | + ...otherProps |
| 127 | + } = this.props; |
| 128 | + |
| 129 | + const props = { |
| 130 | + onFocus: this.handleFocus, |
| 131 | + onBlur: this.handleBlur, |
| 132 | + onMouseDown: this.handleMouseDown, |
| 133 | + onClick: this.handleClick, |
| 134 | + onTouchStart: this.handleTouchStart, |
| 135 | + className: selectClassName, |
| 136 | + ...otherProps, |
| 137 | + }; |
| 138 | + |
| 139 | + if (enhanced) { |
| 140 | + return ( |
| 141 | + <EnhancedSelect |
| 142 | + onKeyDown={this.handleKeyDown} |
| 143 | + {...props} |
| 144 | + > |
| 145 | + {children} |
| 146 | + </EnhancedSelect> |
| 147 | + ); |
| 148 | + } |
| 149 | + return ( |
| 150 | + <NativeSelect |
| 151 | + onKeyDown={onKeyDown} |
| 152 | + {...props} |
| 153 | + > |
| 154 | + {children} |
| 155 | + </NativeSelect> |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
0 commit comments