mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 01:20:24 +03:00
Moved common code between Checkbox and ToggleSwitch to child component
This commit is contained in:
parent
a675d60d59
commit
d2f818c1ea
3 changed files with 42 additions and 44 deletions
36
src/utils/BooleanControl.js
Normal file
36
src/utils/BooleanControl.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import classNames from 'classnames';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
export const basePropTypes = {
|
||||
checked: PropTypes.bool.isRequired,
|
||||
onChange: PropTypes.func.isRequired,
|
||||
children: PropTypes.oneOfType([ PropTypes.string, PropTypes.node ]),
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
const propTypes = {
|
||||
...basePropTypes,
|
||||
type: PropTypes.oneOf([ 'switch', 'checkbox' ]).isRequired,
|
||||
};
|
||||
|
||||
const BooleanControl = ({ checked, onChange, className, children, type }) => {
|
||||
const id = uuid();
|
||||
const onChecked = (e) => onChange(e.target.checked, e);
|
||||
const typeClasses = {
|
||||
'custom-switch': type === 'switch',
|
||||
'custom-checkbox': type === 'checkbox',
|
||||
};
|
||||
|
||||
return (
|
||||
<span className={classNames('custom-control', typeClasses, className)} style={{ display: 'inline' }}>
|
||||
<input type="checkbox" className="custom-control-input" id={id} checked={checked} onChange={onChecked} />
|
||||
<label className="custom-control-label" htmlFor={id}>{children}</label>
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
BooleanControl.propTypes = propTypes;
|
||||
|
||||
export default BooleanControl;
|
|
@ -1,27 +1,8 @@
|
|||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import classNames from 'classnames';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import BooleanControl, { basePropTypes } from './BooleanControl';
|
||||
|
||||
const propTypes = {
|
||||
checked: PropTypes.bool.isRequired,
|
||||
onChange: PropTypes.func.isRequired,
|
||||
children: PropTypes.oneOfType([ PropTypes.string, PropTypes.node ]),
|
||||
className: PropTypes.string,
|
||||
};
|
||||
const Checkbox = (props) => <BooleanControl type="checkbox" {...props} />;
|
||||
|
||||
const Checkbox = ({ checked, onChange, className, children }) => {
|
||||
const id = uuid();
|
||||
const onChecked = (e) => onChange(e.target.checked, e);
|
||||
|
||||
return (
|
||||
<span className={classNames('custom-control custom-checkbox', className)} style={{ display: 'inline' }}>
|
||||
<input type="checkbox" className="custom-control-input" id={id} checked={checked} onChange={onChecked} />
|
||||
<label className="custom-control-label" htmlFor={id}>{children}</label>
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
Checkbox.propTypes = propTypes;
|
||||
Checkbox.propTypes = basePropTypes;
|
||||
|
||||
export default Checkbox;
|
||||
|
|
|
@ -1,27 +1,8 @@
|
|||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import classNames from 'classnames';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import BooleanControl, { basePropTypes } from './BooleanControl';
|
||||
|
||||
const propTypes = {
|
||||
checked: PropTypes.bool.isRequired,
|
||||
onChange: PropTypes.func.isRequired,
|
||||
children: PropTypes.oneOfType([ PropTypes.string, PropTypes.node ]),
|
||||
className: PropTypes.string,
|
||||
};
|
||||
const ToggleSwitch = (props) => <BooleanControl type="switch" {...props} />;
|
||||
|
||||
const ToggleSwitch = ({ checked, onChange, className, children }) => {
|
||||
const id = uuid();
|
||||
const onChecked = (e) => onChange(e.target.checked, e);
|
||||
|
||||
return (
|
||||
<span className={classNames('custom-control custom-switch', className)} style={{ display: 'inline' }}>
|
||||
<input type="checkbox" className="custom-control-input" id={id} checked={checked} onChange={onChecked} />
|
||||
<label className="custom-control-label" htmlFor={id}>{children}</label>
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
ToggleSwitch.propTypes = propTypes;
|
||||
ToggleSwitch.propTypes = basePropTypes;
|
||||
|
||||
export default ToggleSwitch;
|
||||
|
|
Loading…
Reference in a new issue