-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumField.js
41 lines (33 loc) · 1.08 KB
/
EnumField.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// src/mui/field/EnumField.js
import React, { PropTypes } from 'react';
import pure from 'recompose/pure';
import { translate as Translate } from 'admin-on-rest';
import Chip from 'material-ui/Chip';
import inflection from 'inflection';
/**
* @example
* <EnumField source="gender" />
* in i18n messages you need to add
* en: {resources: { people: { enums: { gender: { m: 'Male', f: 'Female' } } } } }
*/
const EnumField = ({ record, source, resource, elStyle = { margin: 4 }, translate }) => {
const text = translate(
`resources.${resource}.enums.${source}.${record[source]}`,
{ _: inflection.humanize(record[source]) },
);
return <Chip style={elStyle} >{text}</Chip>;
};
EnumField.propTypes = {
addLabel: PropTypes.bool,
elStyle: PropTypes.object,
label: PropTypes.string,
resource: PropTypes.string.isRequired,
record: PropTypes.object.isRequired,
source: PropTypes.string.isRequired,
translate: PropTypes.func.isRequired,
};
const PureEnumField = pure(EnumField);
PureEnumField.defaultProps = {
addLabel: true,
};
export default Translate(PureEnumField);