Skip to content

Commit 346d594

Browse files
committed
feat(mui): Add Slider component
1 parent e67d447 commit 346d594

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

packages/mui-component-mapper/src/files/component-mapper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Radio from './radio';
1313
import Wizard from './wizard';
1414
import FieldArray from './field-array';
1515
import DualListSelect from './dual-list-select';
16+
import Slider from './slider';
1617

1718
export const components = {
1819
TextField,
@@ -26,7 +27,8 @@ export const components = {
2627
PlainText,
2728
SubForm,
2829
Wizard,
29-
DualListSelect
30+
DualListSelect,
31+
Slider
3032
};
3133

3234
const componentMapper = {
@@ -43,7 +45,8 @@ const componentMapper = {
4345
[componentTypes.PLAIN_TEXT]: PlainText,
4446
[componentTypes.WIZARD]: Wizard,
4547
[componentTypes.FIELD_ARRAY]: FieldArray,
46-
[componentTypes.DUAL_LIST_SELECT]: DualListSelect
48+
[componentTypes.DUAL_LIST_SELECT]: DualListSelect,
49+
[componentTypes.SLIDER]: Slider
4750
};
4851

4952
export default componentMapper;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { FormControl, FormGroup, FormHelperText, Slider as MUISlider, FormLabel, Grid } from '@material-ui/core';
4+
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
5+
6+
import FormFieldGrid from '../common/form-field-grid';
7+
import { validationError } from '../common/helpers';
8+
9+
const Slider = (props) => {
10+
const {
11+
input,
12+
isReadOnly,
13+
isDisabled,
14+
isRequired,
15+
label,
16+
helperText,
17+
description,
18+
validateOnMount,
19+
meta,
20+
FormFieldGridProps,
21+
FormControlProps,
22+
FormGroupProps,
23+
FormLabelProps,
24+
FormHelperTextProps,
25+
before,
26+
after,
27+
InputGridProps,
28+
BeforeGridProps,
29+
SliderGridProps,
30+
AfterGridProps,
31+
...rest
32+
} = useFieldApi(props);
33+
34+
const invalid = validationError(meta, validateOnMount);
35+
const text = invalid || helperText || description;
36+
37+
return (
38+
<FormFieldGrid {...FormFieldGridProps}>
39+
<FormControl fullWidth required={isRequired} error={!!invalid} component="fieldset" {...FormControlProps}>
40+
<FormGroup {...FormGroupProps}>
41+
<FormLabel component="legend" {...FormLabelProps}>
42+
{label}
43+
</FormLabel>
44+
<Grid container spacing={2} alignItems="center" {...InputGridProps}>
45+
{before && (
46+
<Grid item {...BeforeGridProps}>
47+
{before}
48+
</Grid>
49+
)}
50+
<Grid item xs {...SliderGridProps}>
51+
<MUISlider {...input} {...rest} disabled={isDisabled || isReadOnly} onChange={(_e, value) => input.onChange(value)} />
52+
</Grid>
53+
{after && (
54+
<Grid item {...AfterGridProps}>
55+
{after}
56+
</Grid>
57+
)}
58+
</Grid>
59+
{(invalid || text) && <FormHelperText {...FormHelperTextProps}>{invalid || text}</FormHelperText>}
60+
</FormGroup>
61+
</FormControl>
62+
</FormFieldGrid>
63+
);
64+
};
65+
66+
Slider.propTypes = {
67+
isReadOnly: PropTypes.bool,
68+
isDisabled: PropTypes.bool,
69+
isRequired: PropTypes.bool,
70+
label: PropTypes.node,
71+
helperText: PropTypes.node,
72+
description: PropTypes.node,
73+
validateOnMount: PropTypes.bool,
74+
FormFieldGridProps: PropTypes.object,
75+
FormControlProps: PropTypes.object,
76+
FormGroupProps: PropTypes.object,
77+
FormLabelProps: PropTypes.object,
78+
FormHelperTextProps: PropTypes.object,
79+
before: PropTypes.node,
80+
after: PropTypes.node,
81+
InputGridProps: PropTypes.object,
82+
BeforeGridProps: PropTypes.object,
83+
SliderGridProps: PropTypes.object,
84+
AfterGridProps: PropTypes.object
85+
};
86+
87+
export default Slider;

packages/mui-component-mapper/src/tests/form-fields.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ describe('formFields', () => {
4949
componentTypes.DATE_PICKER,
5050
componentTypes.TIME_PICKER,
5151
componentTypes.SWITCH,
52-
componentTypes.SELECT
52+
componentTypes.SELECT,
53+
componentTypes.SLIDER
5354
].forEach((component) => {
5455
describe(`Component type: ${component}`, () => {
5556
beforeEach(() => {
@@ -184,6 +185,8 @@ describe('formFields', () => {
184185
.first()
185186
.props().disabled
186187
).toEqual(true);
188+
} else if (component === componentTypes.SLIDER) {
189+
expect(wrapper.find('.Mui-disabled')).toHaveLength(2);
187190
} else {
188191
expect(
189192
wrapper
@@ -208,6 +211,8 @@ describe('formFields', () => {
208211
.first()
209212
.props().readOnly
210213
).toEqual(true);
214+
} else if (component === componentTypes.SLIDER) {
215+
expect(wrapper.find('.Mui-disabled')).toHaveLength(2);
211216
} else {
212217
expect(
213218
wrapper

0 commit comments

Comments
 (0)