diff --git a/package.json b/package.json index dfb215659..e8b0ca2b8 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "start": "webpack-dev-server --config test/screenshot/webpack.config.js --content-base test/screenshot", "stop": "./test/screenshot/stop.sh", "build": "npm run clean && mkdirp build && node --max_old_space_size=8192 node_modules/.bin/webpack --config packages/webpack.config.js --progress --colors", - "capture": "MDC_COMMIT_HASH=$(git rev-parse --short HEAD) MDC_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) mocha --require ts-node/register --require babel-core/register --ui tdd --timeout 20000 test/screenshot/capture-suite.tsx", + "capture": "MDC_COMMIT_HASH=$(git rev-parse --short HEAD) MDC_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) mocha --require ts-node/register --require babel-core/register --ui tdd --timeout 30000 test/screenshot/capture-suite.tsx", "clean": "rm -rf build/** build packages/**/dist/", "commitmsg": "validate-commit-msg", "fix": "eslint --fix --ext .jsx,.js,.tsx,.ts packages test", @@ -18,7 +18,7 @@ "test:watch": "karma start karma.local.js --auto-watch", "test:unit": "npm run clean && NODE_ENV=test karma start karma.local.js --single-run", "test:unit-ci": "karma start karma.ci.js --single-run", - "test:image-diff": "MDC_COMMIT_HASH=$(git rev-parse --short HEAD) MDC_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) mocha --require ts-node/register --require babel-core/register --ui tdd --timeout 20000 test/screenshot/diff-suite.tsx", + "test:image-diff": "MDC_COMMIT_HASH=$(git rev-parse --short HEAD) MDC_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) mocha --require ts-node/register --require babel-core/register --ui tdd --timeout 30000 test/screenshot/diff-suite.tsx", "test:screenshots": "docker run -it --rm --cap-add=SYS_ADMIN -e MDC_GCLOUD_SERVICE_ACCOUNT_KEY=\"${MDC_GCLOUD_SERVICE_ACCOUNT_KEY}\" mdcreact/screenshots /bin/sh -c 'git checkout .; git checkout master; git pull; npm i; /home/pptruser/material-components-web-react/test/screenshot/start.sh; sleep 35s; npm run test:image-diff'", "upload:screenshots": "node ./test/screenshot/upload-screenshots.js" }, diff --git a/packages/button/index.js b/packages/button/index.js deleted file mode 100644 index ce008cfdb..000000000 --- a/packages/button/index.js +++ /dev/null @@ -1,99 +0,0 @@ -// The MIT License -// -// Copyright (c) 2018 Google, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -import React from 'react'; -import classnames from 'classnames'; -import PropTypes from 'prop-types'; -import {withRipple} from '@material/react-ripple'; - -export const Button = (props) => { - const { - className, - raised, - unelevated, - outlined, - dense, - icon, - children, - initRipple, - unbounded, // eslint-disable-line no-unused-vars - ...otherProps - } = props; - - const classes = classnames('mdc-button', className, { - 'mdc-button--raised': raised, - 'mdc-button--unelevated': unelevated, - 'mdc-button--outlined': outlined, - 'mdc-button--dense': dense, - }); - - const SemanticButton = props.href ? 'a' : 'button'; - - return ( - - {icon ? renderIcon(icon) : null} - {children} - - ); -}; - -const addClassesToElement = (classes, element) => { - const propsWithClasses = { - className: classnames(classes, element.props.className), - }; - return React.cloneElement(element, propsWithClasses); -}; - -const renderIcon = (icon) => addClassesToElement('mdc-button__icon', icon); - -Button.propTypes = { - raised: PropTypes.bool, - unelevated: PropTypes.bool, - outlined: PropTypes.bool, - dense: PropTypes.bool, - disabled: PropTypes.bool, - unbounded: PropTypes.bool, - initRipple: PropTypes.func, - className: PropTypes.string, - icon: PropTypes.element, - href: PropTypes.string, - children: PropTypes.string, -}; - -Button.defaultProps = { - raised: false, - unelevated: false, - outlined: false, - dense: false, - disabled: false, - unbounded: false, - initRipple: () => {}, - className: '', - icon: null, - children: '', -}; - -export default withRipple(Button); diff --git a/packages/button/index.tsx b/packages/button/index.tsx new file mode 100644 index 000000000..1846ff8a8 --- /dev/null +++ b/packages/button/index.tsx @@ -0,0 +1,103 @@ +// The MIT License +// +// Copyright (c) 2018 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +import * as React from 'react'; +import * as classnames from 'classnames'; +import * as Ripple from '@material/react-ripple'; + +const BUTTON_CLASS_NAME = 'mdc-button__icon'; + +type ButtonTypes = HTMLAnchorElement | HTMLButtonElement; + +export interface ButtonProps extends Ripple.InjectedProps, React.HTMLAttributes { + raised?: boolean; + unelevated?: boolean; + outlined?: boolean; + dense?: boolean; + disabled?: boolean; + className?: string; + icon?: React.ReactElement>; + href?: string; +} + +export const Button = ( + { + className = '', + raised = false, + unelevated = false, + outlined = false, + dense = false, + disabled = false, + icon, + href, + children, + initRipple, + // eslint disabled since we do not want to include + // this in ...otherProps. + // if unbounded is passed to the + ); +}; + +const renderIcon = (icon?: React.ReactElement>) => ( + icon ? + React.cloneElement(icon, { + className: classnames(BUTTON_CLASS_NAME, icon.props.className), + }) : + null +); + +Button.defaultProps = { + initRipple: () => {}, +}; + +export default Ripple.withRipple, ButtonTypes>(Button); diff --git a/test/screenshot/button/index.js b/test/screenshot/button/index.tsx similarity index 67% rename from test/screenshot/button/index.js rename to test/screenshot/button/index.tsx index a12fdb32f..64fc9377b 100644 --- a/test/screenshot/button/index.js +++ b/test/screenshot/button/index.tsx @@ -1,24 +1,29 @@ -import React from 'react'; +import * as React from 'react'; import MaterialIcon from '../../../packages/material-icon/index'; import '../../../packages/button/index.scss'; import './index.scss'; - import Button from '../../../packages/button/index'; -const svgIcon = ( - - -); - + 4.1-2 5.64V6.37c1.24 1.55 2 3.5 2 5.63z' + /> + +); const ButtonScreenshotTest = () => { return (
@@ -36,7 +41,9 @@ const ButtonScreenshotTest = () => {
- +
@@ -44,24 +51,23 @@ const ButtonScreenshotTest = () => {
- +
-
-
); }; - export default ButtonScreenshotTest; diff --git a/test/screenshot/chips/index.tsx b/test/screenshot/chips/index.tsx index ac698a8a4..b74721e34 100644 --- a/test/screenshot/chips/index.tsx +++ b/test/screenshot/chips/index.tsx @@ -1,8 +1,6 @@ import * as React from 'react'; import './index.scss'; import '../../../packages/chips/index.scss'; -// TODO: fix in #513 -// @ts-ignore import MaterialIcon from '../../../packages/material-icon'; import {ChipProps, Chip, ChipSet} from '../../../packages/chips/index'; // eslint-disable-line no-unused-vars // no .d.ts file diff --git a/test/screenshot/text-field/refTest.tsx b/test/screenshot/text-field/refTest.tsx index 8762c6d78..d263476be 100644 --- a/test/screenshot/text-field/refTest.tsx +++ b/test/screenshot/text-field/refTest.tsx @@ -1,7 +1,6 @@ import * as React from 'react'; import TextField, {Input} from '../../../packages/text-field'; -// @ts-ignore -import Button from '../../../packages/button/index.js'; +import Button from '../../../packages/button/index'; class OutlinedTextField extends React.Component<{}, {value: string}> { inputEl: Input | null = null; diff --git a/test/unit/button/index.test.js b/test/unit/button/index.test.tsx similarity index 83% rename from test/unit/button/index.test.js rename to test/unit/button/index.test.tsx index 7ccfc77ad..b9299ae9c 100644 --- a/test/unit/button/index.test.js +++ b/test/unit/button/index.test.tsx @@ -1,6 +1,6 @@ -import React from 'react'; +import * as React from 'react'; import {assert} from 'chai'; -import td from 'testdouble'; +import * as td from 'testdouble'; import {mount, shallow} from 'enzyme'; import {Button} from '../../../packages/button/index'; @@ -49,12 +49,14 @@ test('renders a button tag', () => { }); test('renders a button with an anchor tag', () => { - const wrapper = shallow(