From 08f5c1072f30be37cdc0ece35c20e49612ae3e8a Mon Sep 17 00:00:00 2001 From: Iris Scholten Date: Tue, 6 Mar 2018 12:22:58 -0800 Subject: [PATCH] change ConfirmButtons to typescript and refactor; create initial ConfirmButtons tests --- ui/src/shared/components/ConfirmButtons.js | 94 ------------------- ui/src/shared/components/ConfirmButtons.tsx | 89 ++++++++++++++++++ .../shared/components/ConfirmButtons.test.tsx | 21 +++++ 3 files changed, 110 insertions(+), 94 deletions(-) delete mode 100644 ui/src/shared/components/ConfirmButtons.js create mode 100644 ui/src/shared/components/ConfirmButtons.tsx create mode 100644 ui/test/shared/components/ConfirmButtons.test.tsx diff --git a/ui/src/shared/components/ConfirmButtons.js b/ui/src/shared/components/ConfirmButtons.js deleted file mode 100644 index 5a086a907..000000000 --- a/ui/src/shared/components/ConfirmButtons.js +++ /dev/null @@ -1,94 +0,0 @@ -import React, {PropTypes, Component} from 'react' -import classnames from 'classnames' - -import OnClickOutside from 'shared/components/OnClickOutside' - -class ConfirmButtons extends Component { - constructor(props) { - super(props) - } - - handleConfirm = item => () => { - this.props.onConfirm(item) - } - - handleCancel = item => () => { - this.props.onCancel(item) - } - - handleClickOutside = () => { - this.props.onClickOutside(this.props.item) - } - - render() { - const { - item, - buttonSize, - isDisabled, - confirmLeft, - confirmHoverText, - } = this.props - const hoverText = confirmHoverText || 'Save' - - return confirmLeft - ?
- - -
- :
- - -
- } -} - -const {func, oneOfType, shape, string, bool} = PropTypes - -ConfirmButtons.propTypes = { - onConfirm: func.isRequired, - item: oneOfType([shape(), string]), - onCancel: func.isRequired, - buttonSize: string, - isDisabled: bool, - onClickOutside: func, - confirmLeft: bool, - confirmHoverText: string, -} - -ConfirmButtons.defaultProps = { - buttonSize: 'btn-sm', - onClickOutside: () => {}, -} -export default OnClickOutside(ConfirmButtons) diff --git a/ui/src/shared/components/ConfirmButtons.tsx b/ui/src/shared/components/ConfirmButtons.tsx new file mode 100644 index 000000000..945d221da --- /dev/null +++ b/ui/src/shared/components/ConfirmButtons.tsx @@ -0,0 +1,89 @@ +import React, {PureComponent} from 'react' +import classnames from 'classnames' + +import OnClickOutside from 'src/shared/components/OnClickOutside' +import ConfirmButton from 'src/shared/components/ConfirmButton' + +type Item = Object | string + +interface ConfirmButtonsProps { + onConfirm: (item: Item) => void + item: Item + onCancel: (item: Item) => void + buttonSize?: string + isDisabled?: boolean + onClickOutside?: (item: Item) => void + confirmLeft?: boolean + confirmHoverText?: string +} + +class ConfirmButtons extends PureComponent { + constructor(props) { + super(props) + } + + public static defaultProps: Partial = { + buttonSize: 'btn-sm', + onClickOutside: () => {}, + } + + handleConfirm = item => () => { + this.props.onConfirm(item) + } + + handleCancel = item => () => { + this.props.onCancel(item) + } + + handleClickOutside = () => { + this.props.onClickOutside(this.props.item) + } + + render() { + const { + item, + buttonSize, + isDisabled, + confirmLeft, + confirmHoverText, + } = this.props + const hoverText = confirmHoverText || 'Save' + + const confirmClass = classnames('btn btn-success btn-square', { + [buttonSize]: buttonSize, + }) + + const cancelClass = classnames('btn btn-info btn-square', { + [buttonSize]: buttonSize, + }) + return confirmLeft + ?
+ + +
+ :
+ + +
+ } +} + +export default OnClickOutside(ConfirmButtons) diff --git a/ui/test/shared/components/ConfirmButtons.test.tsx b/ui/test/shared/components/ConfirmButtons.test.tsx new file mode 100644 index 000000000..2d33b5018 --- /dev/null +++ b/ui/test/shared/components/ConfirmButtons.test.tsx @@ -0,0 +1,21 @@ +import React from 'react' +import ConfirmButtons from 'src/shared/components/ConfirmButtons' +import ConfirmButton from 'src/shared/components/ConfirmButton' + +import {shallow} from 'enzyme' + +const props = {} + +describe('Componenets.Shared.ConfirmButtons', () => { + describe('rendering', () => { + it('has a confirm and cancel button', () => { + const wrapper = shallow() + const buttons = wrapper.dive().find(ConfirmButton) + expect(buttons.length).toBe(2) + }) + }) + + describe('user interaction', () => { + it('') + }) +})