|
| 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; |
0 commit comments