|
| 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 React from 'react'; |
| 24 | +import classnames from 'classnames'; |
| 25 | +import {MDCSelectIconAdapter} from '@material/select/icon/adapter'; |
| 26 | +import {MDCSelectIconFoundation} from '@material/select/icon/foundation'; |
| 27 | + |
| 28 | +export interface SelectIconProps extends React.HTMLProps<HTMLElement> { |
| 29 | + setIconFoundation?: (foundation?: MDCSelectIconFoundation) => void; |
| 30 | + tag?: keyof React.ReactHTML; |
| 31 | +} |
| 32 | + |
| 33 | +interface ElementAttributes { |
| 34 | + 'tabindex'?: number; |
| 35 | + role?: string; |
| 36 | +}; |
| 37 | + |
| 38 | +interface SelectIconState extends ElementAttributes {}; |
| 39 | + |
| 40 | +export class SelectIcon extends React.Component<SelectIconProps, SelectIconState> { |
| 41 | + foundation?: MDCSelectIconFoundation; |
| 42 | + |
| 43 | + state: SelectIconState = { |
| 44 | + 'tabindex': undefined, |
| 45 | + 'role': undefined, |
| 46 | + }; |
| 47 | + |
| 48 | + static defaultProps = { |
| 49 | + tag: 'i', |
| 50 | + }; |
| 51 | + |
| 52 | + componentDidMount() { |
| 53 | + const {setIconFoundation} = this.props; |
| 54 | + this.foundation = new MDCSelectIconFoundation(this.adapter); |
| 55 | + this.foundation.init(); |
| 56 | + setIconFoundation && setIconFoundation(this.foundation); |
| 57 | + } |
| 58 | + |
| 59 | + componentWillUnmount() { |
| 60 | + const {setIconFoundation} = this.props; |
| 61 | + if (this.foundation) { |
| 62 | + this.foundation.destroy(); |
| 63 | + setIconFoundation && setIconFoundation(undefined); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + get adapter(): MDCSelectIconAdapter { |
| 68 | + return { |
| 69 | + getAttr: (attr: keyof ElementAttributes) => { |
| 70 | + if (this.state[attr] !== undefined) { |
| 71 | + return (this.state[attr] as ElementAttributes[keyof ElementAttributes])!.toString(); |
| 72 | + } |
| 73 | + const reactAttr = attr === 'tabindex' ? 'tabIndex' : attr; |
| 74 | + if (this.props[reactAttr] !== undefined) { |
| 75 | + return (this.props[reactAttr])!.toString(); |
| 76 | + } |
| 77 | + return null; |
| 78 | + }, |
| 79 | + setAttr: (attr: keyof ElementAttributes, value: ElementAttributes[keyof ElementAttributes]) => { |
| 80 | + this.setState((prevState) => ({ |
| 81 | + ...prevState, |
| 82 | + [attr]: value, |
| 83 | + })); |
| 84 | + }, |
| 85 | + removeAttr: (attr: keyof ElementAttributes) => { |
| 86 | + this.setState((prevState) => ({...prevState, [attr]: null})); |
| 87 | + }, |
| 88 | + setContent: () => { |
| 89 | + // not implmenting because developer should would never call `setContent()` |
| 90 | + }, |
| 91 | + // the adapter methods below are effectively useless since React |
| 92 | + // handles events and width differently |
| 93 | + registerInteractionHandler: () => undefined, |
| 94 | + deregisterInteractionHandler: () => undefined, |
| 95 | + notifyIconAction: () => undefined, |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + render() { |
| 100 | + const { |
| 101 | + tag: Tag, |
| 102 | + setIconFoundation, // eslint-disable-line no-unused-vars |
| 103 | + children, |
| 104 | + className, |
| 105 | + ...otherProps |
| 106 | + } = this.props; |
| 107 | + const {tabindex: tabIndex, role} = this.state; |
| 108 | + return ( |
| 109 | + // @ts-ignore https://github.com/Microsoft/TypeScript/issues/28892 |
| 110 | + <Tag |
| 111 | + className={classnames('mdc-select__icon', className)} |
| 112 | + role={role} |
| 113 | + tabIndex={tabIndex} |
| 114 | + {...otherProps} |
| 115 | + > |
| 116 | + {children} |
| 117 | + </Tag> |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments