|
| 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 classnames from 'classnames'; |
| 25 | +import {MDCSelectHelperTextAdapter} from '@material/select/helper-text/adapter'; |
| 26 | +import {MDCSelectHelperTextFoundation} from '@material/select/helper-text/foundation'; |
| 27 | + |
| 28 | +export interface SelectHelperTextProps extends React.HTMLProps<HTMLParagraphElement> { |
| 29 | + persistent?: boolean; |
| 30 | + setHelperTextFoundation?: (foundation?: MDCSelectHelperTextFoundation) => void; |
| 31 | +} |
| 32 | + |
| 33 | +interface ElementAttributes { |
| 34 | + 'aria-hidden'?: boolean | 'false' | 'true'; |
| 35 | + role?: string; |
| 36 | +}; |
| 37 | + |
| 38 | +interface SelectHelperTextState extends ElementAttributes { |
| 39 | + classList: Set<string>; |
| 40 | +}; |
| 41 | + |
| 42 | +export class SelectHelperText extends React.Component<SelectHelperTextProps, SelectHelperTextState> { |
| 43 | + foundation?: MDCSelectHelperTextFoundation; |
| 44 | + |
| 45 | + state: SelectHelperTextState = { |
| 46 | + 'aria-hidden': undefined, |
| 47 | + 'role': undefined, |
| 48 | + 'classList': new Set(), |
| 49 | + }; |
| 50 | + |
| 51 | + componentDidMount() { |
| 52 | + const {setHelperTextFoundation} = this.props; |
| 53 | + this.foundation = new MDCSelectHelperTextFoundation(this.adapter); |
| 54 | + this.foundation.init(); |
| 55 | + setHelperTextFoundation && setHelperTextFoundation(this.foundation); |
| 56 | + } |
| 57 | + |
| 58 | + componentWillUnmount() { |
| 59 | + const {setHelperTextFoundation} = this.props; |
| 60 | + if (this.foundation) { |
| 61 | + this.foundation.destroy(); |
| 62 | + setHelperTextFoundation && setHelperTextFoundation(undefined); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + get classes() { |
| 67 | + const {className, persistent} = this.props; |
| 68 | + const {classList} = this.state; |
| 69 | + return classnames('mdc-select-helper-text', Array.from(classList), className, { |
| 70 | + 'mdc-select-helper-text--persistent': persistent, |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + get adapter(): MDCSelectHelperTextAdapter { |
| 75 | + return { |
| 76 | + addClass: (className: string) => { |
| 77 | + const classList = new Set(this.state.classList); |
| 78 | + classList.add(className); |
| 79 | + this.setState({classList}); |
| 80 | + }, |
| 81 | + removeClass: (className: string) => { |
| 82 | + const classList = new Set(this.state.classList); |
| 83 | + classList.delete(className); |
| 84 | + this.setState({classList}); |
| 85 | + }, |
| 86 | + hasClass: (className: string) => { |
| 87 | + return this.classes.split(' ').includes(className); |
| 88 | + }, |
| 89 | + setAttr: (attr: keyof ElementAttributes, value: ElementAttributes[keyof ElementAttributes]) => { |
| 90 | + this.setState((prevState) => ({ |
| 91 | + ...prevState, |
| 92 | + [attr]: value, |
| 93 | + })); |
| 94 | + }, |
| 95 | + removeAttr: (attr: keyof ElementAttributes) => { |
| 96 | + this.setState((prevState) => ({...prevState, [attr]: null})); |
| 97 | + }, |
| 98 | + setContent: () => { |
| 99 | + // not implmenting because developer should would never call `setContent()` |
| 100 | + }, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + render() { |
| 105 | + const { |
| 106 | + children, |
| 107 | + /* eslint-disable no-unused-vars */ |
| 108 | + persistent, |
| 109 | + className, |
| 110 | + setHelperTextFoundation, |
| 111 | + /* eslint-enable no-unused-vars */ |
| 112 | + ...otherProps |
| 113 | + } = this.props; |
| 114 | + const { |
| 115 | + 'aria-hidden': ariaHidden, |
| 116 | + role, |
| 117 | + } = this.state; |
| 118 | + |
| 119 | + return ( |
| 120 | + <p |
| 121 | + className={this.classes} |
| 122 | + aria-hidden={ariaHidden} |
| 123 | + role={role} |
| 124 | + {...otherProps} |
| 125 | + > |
| 126 | + {children} |
| 127 | + </p> |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments