|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import { formatMessage } from '@openimis/fe-core'; |
| 3 | +import { injectIntl } from 'react-intl'; |
| 4 | +import { withTheme, withStyles } from '@material-ui/core/styles'; |
| 5 | +import { |
| 6 | + Select, |
| 7 | + InputLabel, |
| 8 | + FormControl, |
| 9 | + MenuItem, |
| 10 | +} from '@material-ui/core'; |
| 11 | +import { |
| 12 | + MODULE_NAME, |
| 13 | +} from '../constants'; |
| 14 | + |
| 15 | +const styles = (theme) => ({ |
| 16 | + label: { |
| 17 | + color: theme.palette.primary.main, |
| 18 | + }, |
| 19 | + formControl: { |
| 20 | + position: 'relative', |
| 21 | + }, |
| 22 | +}); |
| 23 | + |
| 24 | +function ProjectAllowsMultiEnrollmentPicker({ |
| 25 | + intl, |
| 26 | + classes, |
| 27 | + value, |
| 28 | + label, |
| 29 | + onChange, |
| 30 | + required, |
| 31 | + readOnly = false, |
| 32 | + withNull = false, |
| 33 | + nullLabel = null, |
| 34 | +}) { |
| 35 | + const options = [ |
| 36 | + { value: true, label: formatMessage(intl, MODULE_NAME, 'common.true') }, |
| 37 | + { value: false, label: formatMessage(intl, MODULE_NAME, 'common.false') }, |
| 38 | + ]; |
| 39 | + |
| 40 | + const handleChange = (e) => { |
| 41 | + if (value !== e.target.value) { |
| 42 | + onChange(e.target.value); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + if (withNull) { |
| 48 | + options.unshift({ |
| 49 | + value: null, |
| 50 | + label: nullLabel || formatMessage(intl, MODULE_NAME, 'common.any'), |
| 51 | + }); |
| 52 | + } |
| 53 | + }, []); |
| 54 | + |
| 55 | + return ( |
| 56 | + <FormControl required={required} fullWidth className={classes.formControl}> |
| 57 | + <InputLabel shrink className={classes.label}> |
| 58 | + {formatMessage(intl, MODULE_NAME, label)} |
| 59 | + </InputLabel> |
| 60 | + <Select |
| 61 | + readOnly={readOnly} |
| 62 | + options={options} |
| 63 | + value={value} |
| 64 | + onChange={handleChange} |
| 65 | + > |
| 66 | + {options.map((option) => ( |
| 67 | + <MenuItem value={option.value}> |
| 68 | + {option.label} |
| 69 | + </MenuItem> |
| 70 | + ))} |
| 71 | + </Select> |
| 72 | + </FormControl> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +export default injectIntl(withTheme(withStyles(styles)(ProjectAllowsMultiEnrollmentPicker))); |
0 commit comments