|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2018 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 * as classnames from 'classnames'; |
| 25 | +import * as Ripple from '@material/react-ripple'; |
| 26 | + |
| 27 | +export interface FabProps extends Ripple.InjectedProps<HTMLButtonElement> { |
| 28 | + mini?: boolean; |
| 29 | + icon?: React.ReactElement<HTMLElement>; |
| 30 | + textLabel?: string; |
| 31 | + className?: string; |
| 32 | + initRipple: React.Ref<HTMLButtonElement>; |
| 33 | + unbounded: boolean; |
| 34 | +} |
| 35 | + |
| 36 | +const Icon: React.FunctionComponent<{icon?: React.ReactElement<HTMLElement>}> = ({icon}) => { |
| 37 | + if (!icon) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + const updatedProps = { |
| 41 | + className: classnames('mdc-fab__icon', icon.props.className), |
| 42 | + }; |
| 43 | + return React.cloneElement(icon, updatedProps); |
| 44 | +}; |
| 45 | + |
| 46 | +const TextLabel: React.FunctionComponent<{textLabel: string}> = ({ |
| 47 | + textLabel, // eslint-disable-line react/prop-types |
| 48 | +}) => { |
| 49 | + if (textLabel.length === 0) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + return <span className='mdc-fab__label'>{textLabel}</span>; |
| 53 | +}; |
| 54 | + |
| 55 | +export const Fab: React.FunctionComponent<FabProps & React.HTMLProps<HTMLButtonElement>> = ({ |
| 56 | + /* eslint-disable react/prop-types */ |
| 57 | + mini = false, |
| 58 | + icon, |
| 59 | + textLabel = '', |
| 60 | + className = '', |
| 61 | + initRipple = () => {}, |
| 62 | + unbounded, |
| 63 | + /* eslint-enable react/prop-types */ |
| 64 | + ...otherProps |
| 65 | +}) => { |
| 66 | + const extended = textLabel.length > 0; |
| 67 | + const classes = classnames('mdc-fab', className, { |
| 68 | + 'mdc-fab--mini': mini, |
| 69 | + 'mdc-fab--extended': extended, |
| 70 | + }); |
| 71 | + |
| 72 | + return ( |
| 73 | + <button className={classes} ref={initRipple} {...otherProps}> |
| 74 | + <Icon icon={icon} /> |
| 75 | + <TextLabel textLabel={textLabel} /> |
| 76 | + </button> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +export default Ripple.withRipple<FabProps, HTMLButtonElement>(Fab); |
0 commit comments