Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(checkBox): updated PF4 checkBox component according to PF-next #717

Merged
merged 1 commit into from
Oct 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { HTMLProps, FormEvent } from 'react';
import PropTypes from 'prop-types';
import { HTMLProps, FormEvent, ReactNode } from 'react';
import { Omit } from '../../typeUtils';

export interface CheckboxProps
extends Omit<HTMLProps<HTMLInputElement>, 'type' | 'onChange' | 'disabled'> {
isDisabled?: boolean;
isValid?: boolean;
onChange?(checked: boolean, event: FormEvent<HTMLInputElement>): void;
id: string;
'aria-label': string;
label?: ReactNode;
}

declare const Checkbox: React.SFC<CheckboxProps>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Checkbox } from '@patternfly/react-core';
import Controlled from './examples/ControlledCheckbox';
import Uncontrolled from './examples/UncontrolledCheckbox';
import Disabled from './examples/DisabledCheckbox';
import Custom from './examples/CustomLabelCheckbox';
import { Checkbox } from '@patternfly/react-core';

export default {
title: 'Checkbox',
components: {
Checkbox
},
examples: [Controlled, Uncontrolled, Disabled]
examples: [Controlled, Uncontrolled, Disabled, Custom]
};
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import React from 'react';
import styles from '@patternfly/patternfly-next/components/Check/check.css';
import { css } from '@patternfly/react-styles';
import PropTypes from 'prop-types';
import { css, getModifier } from '@patternfly/react-styles';

const propTypes = {
/** additional classes added to the Checkbox */
/** Additional classes added to the Checkbox. */
className: PropTypes.string,
/** Flag to show if the Checkbox selection is valid or invalid. */
isValid: PropTypes.bool,
/** Flag to show if the Checkbox is disabled. */
isDisabled: PropTypes.bool,
/** A callback for when the Checkbox selection changes. */
onChange: PropTypes.func
onChange: PropTypes.func,
/** Label text of the checkbox. */
label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/** Id of the checkbox. */
id: PropTypes.string.isRequired,
/** Aria-label of the checkbox. */
'aria-label': PropTypes.any.isRequired
};

const defaultProps = {
className: '',
isValid: true,
isDisabled: false,
onChange: () => undefined
onChange: () => undefined,
label: undefined
};

class Checkbox extends React.Component {
Expand All @@ -27,16 +34,23 @@ class Checkbox extends React.Component {
};

render() {
const { className, onChange, isValid, isDisabled, ...props } = this.props;
const { className, onChange, isValid, isDisabled, label, ...props } = this.props;
return (
<input
{...props}
className={css(styles.check, className)}
type="checkbox"
onChange={this.handleChange}
aria-invalid={!isValid}
disabled={isDisabled}
/>
<div className={css(styles.check, className)}>
<input
{...props}
className={css(styles.checkInput)}
rvsia marked this conversation as resolved.
Show resolved Hide resolved
type="checkbox"
onChange={this.handleChange}
aria-invalid={!isValid}
disabled={isDisabled}
/>
{label && (
<label className={css(styles.checkLabel, getModifier(styles, isDisabled && 'disabled'))} htmlFor={props.id}>
{label}
</label>
)}
</div>
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,43 @@ const props = {
};

test('controlled', () => {
const view = shallow(<Checkbox checked />);
const view = shallow(<Checkbox checked id="check" aria-label="check" />);
expect(view).toMatchSnapshot();
});

test('uncontrolled', () => {
const view = shallow(<Checkbox />);
const view = shallow(<Checkbox id="check" aria-label="check" />);
expect(view).toMatchSnapshot();
});

test('isDisabled', () => {
const view = shallow(<Checkbox isDisabled />);
const view = shallow(<Checkbox id="check" isDisabled aria-label="check" />);
expect(view).toMatchSnapshot();
});

test('label is string', () => {
const view = shallow(<Checkbox label="Label" id="check" checked aria-label="check" />);
expect(view).toMatchSnapshot();
});

test('label is function', () => {
rvsia marked this conversation as resolved.
Show resolved Hide resolved
const functionLabel = () => <h1>Header</h1>;
const view = shallow(<Checkbox label={functionLabel()} id="check" checked aria-label="check" />);
expect(view).toMatchSnapshot();
});

test('label is node', () => {
const view = shallow(<Checkbox label={<h1>Header</h1>} id="check" checked aria-label="check" />);
expect(view).toMatchSnapshot();
});

test('passing class', () => {
const view = shallow(<Checkbox label="label" className="class-123" id="check" checked aria-label="check" />);
expect(view).toMatchSnapshot();
});

test('passing HTML attribute', () => {
const view = shallow(<Checkbox label="label" aria-labelledby="labelId" id="check" checked aria-label="check" />);
expect(view).toMatchSnapshot();
});

Expand All @@ -27,7 +53,7 @@ test('checkbox passes value and event to onChange handler', () => {
const event = {
currentTarget: { checked: newValue }
};
const view = shallow(<Checkbox {...props} />);
const view = shallow(<Checkbox id="check" {...props} aria-label="check" />);
view.find('input').simulate('change', event);
expect(props.onChange).toBeCalledWith(newValue, event);
});
Loading