Skip to content

Commit

Permalink
fix(checkbox,textInput): consistent name, id attributes (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Feb 16, 2021
1 parent 389f786 commit db5491a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`TextInput Component should handle readOnly, disabled: active 1`] = `
aria-invalid="false"
class="pf-c-form-control curiosity-text-input "
id="generatedid-"
name="generatedid-"
type="text"
value=""
/>
Expand All @@ -16,6 +17,7 @@ exports[`TextInput Component should handle readOnly, disabled: disabled 1`] = `
class="pf-c-form-control curiosity-text-input "
disabled=""
id="generatedid-"
name="generatedid-"
type="text"
value=""
/>
Expand All @@ -26,6 +28,7 @@ exports[`TextInput Component should handle readOnly, disabled: readOnly 1`] = `
aria-invalid="false"
class="pf-c-form-control curiosity-text-input "
id="generatedid-"
name="generatedid-"
readonly=""
type="text"
value=""
Expand All @@ -37,6 +40,7 @@ exports[`TextInput Component should render a basic component: basic component 1`
aria-invalid="false"
class="pf-c-form-control curiosity-text-input "
id="generatedid-"
name="generatedid-"
type="text"
value=""
/>
Expand Down
23 changes: 14 additions & 9 deletions src/components/form/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { helpers } from '../../common';
* @param {object} props
* @param {string} props.ariaLabel
* @param {Node} props.children
* @param {string} props.id
* @param {*} props.isChecked
* @param {boolean} props.isDisabled
* @param {boolean} props.isReadOnly
Expand All @@ -23,6 +24,7 @@ import { helpers } from '../../common';
const Checkbox = ({
ariaLabel,
children,
id,
isChecked,
isDisabled,
isReadOnly,
Expand All @@ -34,7 +36,8 @@ const Checkbox = ({
}) => {
const [check, setCheck] = React.useState();
const updatedChecked = check ?? isChecked ?? false;
const nameId = name || helpers.generateId();
const updatedName = name || helpers.generateId();
const updatedId = id || updatedName;

/**
* onChange event, provide restructured event.
Expand All @@ -46,8 +49,8 @@ const Checkbox = ({
const onCheckboxChange = (checked, event) => {
const mockEvent = {
...createMockEvent(event),
id: nameId,
name: nameId,
id: updatedId,
name: updatedName,
value,
checked
};
Expand All @@ -60,11 +63,11 @@ const Checkbox = ({
<PfCheckbox
aria-label={ariaLabel || children || label}
checked={updatedChecked}
id={nameId}
id={updatedId}
isChecked={updatedChecked}
isDisabled={isDisabled || false}
label={children || label}
name={nameId}
name={updatedName}
onChange={onCheckboxChange}
value={value}
readOnly={isReadOnly || false}
Expand All @@ -76,12 +79,13 @@ const Checkbox = ({
/**
* Prop types.
*
* @type {{onChange: Function, children: Node, name: string, isChecked: *, isDisabled: boolean,
* isReadOnly: boolean, label: string, value: *, ariaLabel: string}}
* @type {{isReadOnly: boolean, onChange: Function, children: Node, name: string, id: string,
* isDisabled: boolean, label: string, isChecked: boolean, value: *, ariaLabel: string}}
*/
Checkbox.propTypes = {
ariaLabel: PropTypes.string,
children: PropTypes.node,
id: PropTypes.string,
isChecked: PropTypes.any,
isDisabled: PropTypes.bool,
isReadOnly: PropTypes.bool,
Expand All @@ -94,12 +98,13 @@ Checkbox.propTypes = {
/**
* Default props.
*
* @type {{onChange: Function, children: Node, name: string, isChecked: *, isDisabled: boolean,
* isReadOnly: boolean, label: string, value: *, ariaLabel: string}}
* @type {{isReadOnly: boolean, onChange: Function, children: Node, name: string, id: string,
* isDisabled: boolean, label: string, isChecked: boolean, value: *, ariaLabel: string}}
*/
Checkbox.defaultProps = {
ariaLabel: null,
children: null,
id: null,
isChecked: false,
isDisabled: false,
isReadOnly: false,
Expand Down
21 changes: 13 additions & 8 deletions src/components/form/textInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ class TextInput extends React.Component {
* @returns {Node}
*/
render() {
const { name, updatedValue } = this.state;
const { updatedValue } = this.state;
const {
className,
id,
isDisabled,
name,
onChange,
onClear,
onKeyUp,
Expand All @@ -93,12 +95,13 @@ class TextInput extends React.Component {
value,
...props
} = this.props;
const nameId = name || helpers.generateId();
const updatedName = name || helpers.generateId();
const updatedId = id || updatedName;

return (
<PfTextInput
id={nameId}
name={nameId}
id={updatedId}
name={updatedName}
className={`curiosity-text-input ${className}`}
isDisabled={isDisabled || false}
onChange={this.onChange}
Expand All @@ -117,11 +120,12 @@ class TextInput extends React.Component {
* Prop types.
*
* @type {{onKeyUp: Function, isReadOnly: boolean, onChange: Function, onClear: Function,
* name: string, className: string, isDisabled: boolean, onMouseUp: Function, type: string,
* value: string}}
* name: string, className: string, id: string, isDisabled: boolean, onMouseUp: Function,
* type: string, value: string}}
*/
TextInput.propTypes = {
className: PropTypes.string,
id: PropTypes.string,
isDisabled: PropTypes.bool,
isReadOnly: PropTypes.bool,
name: PropTypes.string,
Expand All @@ -137,11 +141,12 @@ TextInput.propTypes = {
* Default props.
*
* @type {{onKeyUp: Function, isReadOnly: boolean, onChange: Function, onClear: Function,
* name: string, className: string, isDisabled: boolean, onMouseUp: Function, type: string,
* value: string}}
* name: string, className: string, id: string, isDisabled: boolean, onMouseUp: Function,
* type: string, value: string}}
*/
TextInput.defaultProps = {
className: '',
id: null,
isDisabled: false,
isReadOnly: false,
name: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ exports[`ToolbarFieldDisplayName Component should render a non-connected compone
<TextInput
aria-label="t(curiosity-toolbar.placeholder, {\\"context\\":\\"displayName\\"})"
className=""
id={null}
isDisabled={false}
isReadOnly={false}
name={null}
Expand Down

0 comments on commit db5491a

Please sign in to comment.