mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-03 23:07:26 +03:00
Merge pull request #291 from acelaya-forks/feature/toggle-switch
Feature/toggle switch
This commit is contained in:
commit
8be17cce8a
6 changed files with 55 additions and 30 deletions
|
@ -1,7 +1,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Card, CardBody, CardHeader } from 'reactstrap';
|
import { Card, CardBody, CardHeader } from 'reactstrap';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import Checkbox from '../utils/Checkbox';
|
import ToggleSwitch from '../utils/ToggleSwitch';
|
||||||
import { SettingsType } from './reducers/settings';
|
import { SettingsType } from './reducers/settings';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
|
@ -13,9 +13,9 @@ const RealTimeUpdates = ({ settings: { realTimeUpdates }, setRealTimeUpdates })
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>Real-time updates</CardHeader>
|
<CardHeader>Real-time updates</CardHeader>
|
||||||
<CardBody>
|
<CardBody>
|
||||||
<Checkbox checked={realTimeUpdates.enabled} onChange={setRealTimeUpdates}>
|
<ToggleSwitch checked={realTimeUpdates.enabled} onChange={setRealTimeUpdates}>
|
||||||
Enable or disable real-time updates, when using Shlink v2.2.0 or newer.
|
Enable or disable real-time updates, when using Shlink v2.2.0 or newer.
|
||||||
</Checkbox>
|
</ToggleSwitch>
|
||||||
</CardBody>
|
</CardBody>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
|
|
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 React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import BooleanControl, { basePropTypes } from './BooleanControl';
|
||||||
import classNames from 'classnames';
|
|
||||||
import { v4 as uuid } from 'uuid';
|
|
||||||
|
|
||||||
const propTypes = {
|
const Checkbox = (props) => <BooleanControl type="checkbox" {...props} />;
|
||||||
checked: PropTypes.bool.isRequired,
|
|
||||||
onChange: PropTypes.func.isRequired,
|
|
||||||
children: PropTypes.oneOfType([ PropTypes.string, PropTypes.node ]),
|
|
||||||
className: PropTypes.string,
|
|
||||||
};
|
|
||||||
|
|
||||||
const Checkbox = ({ checked, onChange, className, children }) => {
|
Checkbox.propTypes = basePropTypes;
|
||||||
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;
|
|
||||||
|
|
||||||
export default Checkbox;
|
export default Checkbox;
|
||||||
|
|
8
src/utils/ToggleSwitch.js
Normal file
8
src/utils/ToggleSwitch.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import React from 'react';
|
||||||
|
import BooleanControl, { basePropTypes } from './BooleanControl';
|
||||||
|
|
||||||
|
const ToggleSwitch = (props) => <BooleanControl type="switch" {...props} />;
|
||||||
|
|
||||||
|
ToggleSwitch.propTypes = basePropTypes;
|
||||||
|
|
||||||
|
export default ToggleSwitch;
|
|
@ -17,7 +17,7 @@ import { fillTheGaps } from '../../utils/helpers/visits';
|
||||||
import './LineChartCard.scss';
|
import './LineChartCard.scss';
|
||||||
import { useToggle } from '../../utils/helpers/hooks';
|
import { useToggle } from '../../utils/helpers/hooks';
|
||||||
import { rangeOf } from '../../utils/utils';
|
import { rangeOf } from '../../utils/utils';
|
||||||
import Checkbox from '../../utils/Checkbox';
|
import ToggleSwitch from '../../utils/ToggleSwitch';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
title: PropTypes.string,
|
title: PropTypes.string,
|
||||||
|
@ -167,9 +167,9 @@ const LineChartCard = ({ title, visits, highlightedVisits, highlightedLabel = 'S
|
||||||
</UncontrolledDropdown>
|
</UncontrolledDropdown>
|
||||||
</div>
|
</div>
|
||||||
<div className="float-right mr-2">
|
<div className="float-right mr-2">
|
||||||
<Checkbox checked={skipNoVisits} onChange={toggleSkipNoVisits}>
|
<ToggleSwitch checked={skipNoVisits} onChange={toggleSkipNoVisits}>
|
||||||
<small>Skip dates with no visits</small>
|
<small>Skip dates with no visits</small>
|
||||||
</Checkbox>
|
</ToggleSwitch>
|
||||||
</div>
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardBody className="line-chart-card__body">
|
<CardBody className="line-chart-card__body">
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { CardHeader, DropdownItem } from 'reactstrap';
|
||||||
import { Line } from 'react-chartjs-2';
|
import { Line } from 'react-chartjs-2';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import LineChartCard from '../../../src/visits/helpers/LineChartCard';
|
import LineChartCard from '../../../src/visits/helpers/LineChartCard';
|
||||||
import Checkbox from '../../../src/utils/Checkbox';
|
import ToggleSwitch from '../../../src/utils/ToggleSwitch';
|
||||||
|
|
||||||
describe('<LineChartCard />', () => {
|
describe('<LineChartCard />', () => {
|
||||||
let wrapper;
|
let wrapper;
|
||||||
|
@ -90,7 +90,7 @@ describe('<LineChartCard />', () => {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(wrapper.find(Line).prop('data').labels).toHaveLength(2);
|
expect(wrapper.find(Line).prop('data').labels).toHaveLength(2);
|
||||||
wrapper.find(Checkbox).simulate('change');
|
wrapper.find(ToggleSwitch).simulate('change');
|
||||||
expect(wrapper.find(Line).prop('data').labels).toHaveLength(4);
|
expect(wrapper.find(Line).prop('data').labels).toHaveLength(4);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue