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

fix(CustomInput): hidden attribute #1741 #1777

Merged
merged 5 commits into from Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 25 additions & 24 deletions src/CustomFileInput.js
Expand Up @@ -4,48 +4,48 @@ import classNames from 'classnames';
import { mapToCssModules } from './utils';

const propTypes = {
className: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
label: PropTypes.node,
valid: PropTypes.bool,
invalid: PropTypes.bool,
bsSize: PropTypes.string,
htmlFor: PropTypes.string,
cssModule: PropTypes.object,
onChange: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.array, PropTypes.func]),
innerRef: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
PropTypes.func,
])
className: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
label: PropTypes.node,
valid: PropTypes.bool,
invalid: PropTypes.bool,
bsSize: PropTypes.string,
htmlFor: PropTypes.string,
cssModule: PropTypes.object,
onChange: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.array, PropTypes.func]),
innerRef: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
PropTypes.func,
])
};

class CustomFileInput extends React.Component {
constructor(props) {
super(props);

this.state = {
files:null,
files: null,
};

this.onChange = this.onChange.bind(this);
}

onChange(e) {
let input = e.target;
let {onChange} = this.props;
let { onChange } = this.props;
let files = this.getSelectedFiles(input);

if (typeof(onChange) === 'function') {
if (typeof (onChange) === 'function') {
onChange(...arguments);
}

this.setState({files})
this.setState({ files })
}

getSelectedFiles(input) {
let {multiple} = this.props;
let { multiple } = this.props;

if (multiple && input.files) {
let files = [].slice.call(input.files);
Expand Down Expand Up @@ -76,6 +76,7 @@ class CustomFileInput extends React.Component {
type,
onChange,
dataBrowse,
hidden,
...attributes
} = this.props;

Expand All @@ -96,12 +97,12 @@ class CustomFileInput extends React.Component {
);

const labelHtmlFor = htmlFor || attributes.id;
const {files} = this.state;
const { files } = this.state;

return (
<div className={customClass}>
<input type="file" {...attributes} ref={innerRef} className={classNames(validationClassNames, mapToCssModules('custom-file-input', cssModule))} onChange={this.onChange}/>
<label className={mapToCssModules('custom-file-label', cssModule)} htmlFor={labelHtmlFor} data-browse={ dataBrowse }>{files || label || 'Choose file'}</label>
<div className={customClass} hidden={hidden || false}>
<input type="file" {...attributes} ref={innerRef} className={classNames(validationClassNames, mapToCssModules('custom-file-input', cssModule))} onChange={this.onChange} />
<label className={mapToCssModules('custom-file-label', cssModule)} htmlFor={labelHtmlFor} data-browse={dataBrowse}>{files || label || 'Choose file'}</label>
{children}
</div>
);
Expand Down
168 changes: 92 additions & 76 deletions src/CustomInput.js
@@ -1,93 +1,109 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules } from './utils';
import CustomFileInput from './CustomFileInput';
import React from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import { mapToCssModules } from "./utils";
import CustomFileInput from "./CustomFileInput";

const propTypes = {
className: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
type: PropTypes.string.isRequired,
label: PropTypes.node,
inline: PropTypes.bool,
valid: PropTypes.bool,
invalid: PropTypes.bool,
bsSize: PropTypes.string,
htmlFor: PropTypes.string,
cssModule: PropTypes.object,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.array, PropTypes.func]),
innerRef: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
PropTypes.func,
])
className: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
type: PropTypes.string.isRequired,
label: PropTypes.node,
inline: PropTypes.bool,
valid: PropTypes.bool,
invalid: PropTypes.bool,
bsSize: PropTypes.string,
htmlFor: PropTypes.string,
cssModule: PropTypes.object,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.array, PropTypes.func]),
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func])
};

function CustomInput(props) {
const {
className,
label,
inline,
valid,
invalid,
cssModule,
children,
bsSize,
innerRef,
htmlFor,
...attributes
} = props;
const {
className,
label,
inline,
valid,
invalid,
cssModule,
children,
bsSize,
innerRef,
htmlFor,
...attributes
} = props;

const type = attributes.type;
const type = attributes.type;

const customClass = mapToCssModules(classNames(
className,
`custom-${type}`,
bsSize ? `custom-${type}-${bsSize}` : false,
), cssModule);
const customClass = mapToCssModules(
classNames(className, `custom-${type}`, bsSize ? `custom-${type}-${bsSize}` : false),
cssModule
);

const validationClassNames = mapToCssModules(classNames(
invalid && 'is-invalid',
valid && 'is-valid',
), cssModule);
const validationClassNames = mapToCssModules(
classNames(invalid && "is-invalid", valid && "is-valid"),
cssModule
);

const labelHtmlFor = htmlFor || attributes.id;
const labelHtmlFor = htmlFor || attributes.id;

if (type === 'select') {
const { type, ...rest } = attributes;
return <select {...rest} ref={innerRef} className={classNames(validationClassNames, customClass)}>{children}</select>;
}
if (type === "select") {
const { type, ...rest } = attributes;
return (
<select
{...rest}
ref={innerRef}
className={classNames(validationClassNames, customClass)}
>
{children}
</select>
);
}

if (type === 'file') {
return (
<CustomFileInput {...props}/>
);
}
if (type === "file") {
return <CustomFileInput {...props} />;
}

if (type !== 'checkbox' && type !== 'radio' && type !== 'switch') {
return <input {...attributes} ref={innerRef} className={classNames(validationClassNames, customClass)} />;
}
if (type !== "checkbox" && type !== "radio" && type !== "switch") {
return (
<input
{...attributes}
ref={innerRef}
className={classNames(validationClassNames, customClass)}
/>
);
}

const wrapperClasses = classNames(
customClass,
mapToCssModules(classNames(
'custom-control',
{ 'custom-control-inline': inline }
), cssModule)
);
const wrapperClasses = classNames(
customClass,
mapToCssModules(
classNames("custom-control", { "custom-control-inline": inline }),
cssModule
)
);

return (
<div className={wrapperClasses}>
<input
{...attributes}
type={type === 'switch' ? 'checkbox' : type}
ref={innerRef}
className={classNames(validationClassNames, mapToCssModules('custom-control-input', cssModule))}
/>
<label className={mapToCssModules('custom-control-label', cssModule)} htmlFor={labelHtmlFor}>{label}</label>
{children}
</div>
);
const { hidden, ...rest } = attributes;
return (
<div className={wrapperClasses} hidden={hidden || false}>
<input
{...rest}
type={type === "switch" ? "checkbox" : type}
ref={innerRef}
className={classNames(
validationClassNames,
mapToCssModules("custom-control-input", cssModule)
)}
/>
<label
className={mapToCssModules("custom-control-label", cssModule)}
htmlFor={labelHtmlFor}
>
{label}
</label>
{children}
</div>
);
}

CustomInput.propTypes = propTypes;
Expand Down