Guest User

Untitled

a guest
Feb 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. /**
  2. Form component
  3. **/
  4.  
  5. import { withStyles } from 'material-ui/styles';
  6. import { styles } from './styles';
  7. import React from 'react';
  8. import PropTypes from 'prop-types';
  9. import Divider from 'material-ui/Divider';
  10. import FormActions from '../FormActions';
  11. import Grid from 'material-ui/Grid';
  12.  
  13. const { node, string, bool, array } = PropTypes;
  14.  
  15. class Form extends React.Component {
  16. static defaultProps = {
  17. actions: true,
  18. noValidate: false,
  19. };
  20.  
  21. constructor(props) {
  22. super();
  23. const { data = {}, validationRules = {} } = props;
  24. this.state = {
  25. data,
  26. };
  27. this.renderChild = this.renderChild.bind(this);
  28. this.handleSubmit = this.handleSubmit.bind(this);
  29. this.onFieldChange = this.onFieldChange.bind(this);
  30. }
  31.  
  32. onFieldChange(name, value, opts) {
  33. this.setState({
  34. data: { ...this.state.data, [name]: value },
  35. });
  36. }
  37.  
  38. renderChild(child, key) {
  39. const { props } = child;
  40. const { uiOptions } = props;
  41. const { xs, md, lg } = uiOptions
  42. ? uiOptions
  43. : {
  44. xs: 6,
  45. md: 4,
  46. lg: 4,
  47. };
  48.  
  49. return (
  50. <Grid item key={key} xs={xs}>
  51. {React.cloneElement(child, {
  52. ...props,
  53. onChange: this.onFieldChange,
  54. })}
  55. </Grid>
  56. );
  57. }
  58.  
  59. handleSubmit(e) {
  60. const { data } = this.state;
  61. const { onSubmit } = this.props;
  62. console.log(data);
  63.  
  64. // onSubmit(data);
  65. return false;
  66. }
  67.  
  68. render() {
  69. const {
  70. children,
  71. actions,
  72. classes,
  73. spacing = 16,
  74. alignItems = 'flex-start',
  75. ...rest
  76. } = this.props;
  77.  
  78. return (
  79. <form className={classes.root} {...rest}>
  80. <Grid
  81. container
  82. className={classes.container}
  83. spacing={spacing}
  84. alignItems={alignItems}
  85. >
  86. {children.length > 1
  87. ? children.map((child, key) => {
  88. return this.renderChild(child, key);
  89. })
  90. : this.renderChild(children)}
  91. </Grid>
  92. {actions === true ? (
  93. <Grid
  94. container
  95. className={classes.container}
  96. spacing={16}
  97. alignItems="flex-start"
  98. >
  99. <Grid item xs={12}>
  100. <FormActions handleSubmit={this.handleSubmit} />
  101. </Grid>
  102. </Grid>
  103. ) : null}
  104. </form>
  105. );
  106. }
  107. }
  108.  
  109. Form.propTypes = {
  110. noValidate: bool,
  111. autoComplete: bool,
  112. title: string,
  113. actions: bool,
  114. children: node.isRequired,
  115. };
  116.  
  117. export default withStyles(styles)(Form);
  118.  
  119. =============
  120.  
  121. /**
  122. Field component
  123. **/
  124.  
  125. import React from 'react';
  126. import PropTypes from 'prop-types';
  127. import { withStyles } from 'material-ui/styles';
  128. import { styles } from './styles';
  129. import TextField from '../TextField';
  130. import SelectField from '../SelectField';
  131. import Switch from '../Switch';
  132. import Checkbox from '../Checkbox';
  133.  
  134. const { string, array, bool, object } = PropTypes;
  135.  
  136. class Field extends React.Component {
  137. static defaultProps = {
  138. type: 'text',
  139. };
  140. constructor() {
  141. super();
  142. }
  143. render() {
  144. const { type, selectoptions, uioptions, ...rest } = this.props;
  145.  
  146. switch (type) {
  147. case 'switch':
  148. return <Switch uioptions={uioptions} {...rest} />;
  149. case 'checkbox':
  150. return <Checkbox uioptions={uioptions} {...rest} />;
  151. case 'select':
  152. return (
  153. <SelectField
  154. uioptions={uioptions}
  155. selectoptions={selectoptions}
  156. {...rest}
  157. />
  158. );
  159. default:
  160. return <TextField type={type} uioptions={uioptions} {...rest} />;
  161. }
  162.  
  163. return null;
  164. }
  165. }
  166.  
  167. Field.propTypes = {
  168. label: string.isRequired,
  169. name: string.isRequired,
  170. required: bool,
  171. uioptions: object,
  172. selectoptions: array,
  173. type: string,
  174. };
  175.  
  176. export default withStyles(styles)(Field);
  177.  
  178. ======
  179.  
  180. /**
  181. Button - material-ui
  182. **/
  183.  
  184. import { withStyles } from 'material-ui/styles';
  185. import { styles } from './styles';
  186. import React from 'react';
  187. import PropTypes from 'prop-types';
  188. import MuiButton from 'material-ui/Button';
  189.  
  190. const { string, func, object, node, oneOfType } = PropTypes;
  191.  
  192. const Button = props => {
  193. const { style, children, classes, onClick } = props;
  194.  
  195. return (
  196. <MuiButton color="primary" style={style} onClick={onClick} {...props}>
  197. {children}
  198. </MuiButton>
  199. );
  200. };
  201.  
  202. Button.propTypes = {
  203. style: object,
  204. children: oneOfType([string, node]),
  205. onClick: func,
  206. };
  207.  
  208. export default withStyles(styles)(Button);
  209.  
  210. ====
  211. /**
  212. Checkbox - material-ui
  213. **/
  214.  
  215. import React from 'react';
  216. import PropTypes from 'prop-types';
  217. import { withStyles } from 'material-ui/styles';
  218. import MuiCheckbox from 'material-ui/Checkbox';
  219. import { FormGroup, FormControlLabel } from 'material-ui/Form';
  220. import { styles } from './styles';
  221.  
  222. const { string } = PropTypes;
  223.  
  224. const Checkbox = props => {
  225. const { classes, value, label, uioptions, ...rest } = props;
  226. return (
  227. <FormControlLabel
  228. control={
  229. <MuiCheckbox className={classes.checkbox} value={value} {...rest} />
  230. }
  231. label={label}
  232. />
  233. );
  234. };
  235.  
  236. Checkbox.propTypes = {
  237. value: string.isRequired,
  238. };
  239.  
  240. export default withStyles(styles)(Checkbox);
  241.  
  242. ===
  243. /**
  244. Form actions component
  245. **/
  246.  
  247. import { withStyles } from 'material-ui/styles';
  248. import { styles } from './styles';
  249. import React from 'react';
  250. import PropTypes from 'prop-types';
  251. import Button from '../Button';
  252.  
  253. const { array, func } = PropTypes;
  254.  
  255. class FormActions extends React.Component {
  256. static defaultProps = {
  257. actions: ['submit'],
  258. };
  259. constructor() {
  260. super();
  261. }
  262. render() {
  263. const { classes, actions, handleSubmit, ...rest } = this.props;
  264.  
  265. return (
  266. <section className={classes.actions}>
  267. {actions.map((action, key) => {
  268. return (
  269. <Button key={key} onClick={handleSubmit}>
  270. {action}
  271. </Button>
  272. );
  273. })}
  274. </section>
  275. );
  276. }
  277. }
  278.  
  279. FormActions.propTypes = {
  280. actions: array.isRequired,
  281. handleSubmit: func,
  282. handleClose: func,
  283. };
  284.  
  285. export default withStyles(styles)(FormActions);
  286.  
  287. ==
  288.  
  289. /**
  290. FormGroup component
  291. **/
  292.  
  293. import React from 'react';
  294. import PropTypes from 'prop-types';
  295. import { FormGroup as MuiGroup } from 'material-ui/Form';
  296.  
  297. const { array } = PropTypes;
  298.  
  299. const FormGroup = props => {
  300. const { children } = props;
  301. return <MuiGroup>{children}</MuiGroup>;
  302. };
  303.  
  304. FormGroup.propTypes = {
  305. children: array.isRequired,
  306. };
  307.  
  308. export default FormGroup;
  309. ==
  310.  
  311. /**
  312. SelectField
  313. **/
  314.  
  315. import React from 'react';
  316. import PropTypes from 'prop-types';
  317. import { withStyles } from 'material-ui/styles';
  318. import Select from 'material-ui/Select';
  319. import Input, { InputLabel } from 'material-ui/Input';
  320. import { MenuItem } from 'material-ui/Menu';
  321. import { FormControl, FormHelperText } from 'material-ui/Form';
  322. import { styles } from './styles';
  323.  
  324. const { string, array, func, bool } = PropTypes;
  325.  
  326. class SelectField extends React.Component {
  327. constructor() {
  328. super();
  329. this.state = {
  330. options: [],
  331. value: 0,
  332. };
  333. this.handleChange = this.handleChange.bind(this);
  334. }
  335. handleChange(e) {
  336. const { name, onChange } = this.props;
  337. const { value } = e.target;
  338.  
  339. this.setState({
  340. value,
  341. });
  342.  
  343. onChange(name, value);
  344. }
  345. render() {
  346. const {
  347. classes,
  348. label,
  349. name,
  350. id,
  351. selectoptions,
  352. onChange,
  353. ...rest
  354. } = this.props;
  355. const { value } = this.state;
  356.  
  357. return (
  358. <FormControl className={classes.root}>
  359. <InputLabel htmlFor={id}>{label}</InputLabel>
  360. <Select
  361. value={value}
  362. renderValue={() => this.state.value}
  363. onChange={e => this.handleChange(e)}
  364. inputProps={{
  365. name: name,
  366. id: id,
  367. }}
  368. {...rest}
  369. >
  370. {selectoptions &&
  371. selectoptions.map((option, idx) => {
  372. return (
  373. <MenuItem key={idx} value={option.value}>
  374. {option.text}
  375. </MenuItem>
  376. );
  377. })}
  378. </Select>
  379. </FormControl>
  380. );
  381. }
  382. }
  383.  
  384. SelectField.propTypes = {
  385. label: string.isRequired,
  386. name: string.isRequired,
  387. selectoptions: array.isRequired,
  388. controlFunc: func,
  389. };
  390.  
  391. export default withStyles(styles)(SelectField);
  392.  
  393. ===
  394. /**
  395. TextField
  396. **/
  397.  
  398. import React from 'react';
  399. import PropTypes from 'prop-types';
  400. import { withStyles } from 'material-ui/styles';
  401. import { styles } from './styles';
  402. import MuiTextField from 'material-ui/TextField';
  403.  
  404. const { string, bool, func } = PropTypes;
  405.  
  406. const TextField = props => {
  407. const { classes, name, id, onChange, uioptions, ...rest } = props;
  408.  
  409. return (
  410. <MuiTextField
  411. className={classes.root}
  412. onChange={e => onChange(name, e.target.value)}
  413. {...rest}
  414. />
  415. );
  416. };
  417.  
  418. TextField.propTypes = {
  419. id: string,
  420. helperText: string,
  421. multiline: bool,
  422. fullWidth: bool,
  423. onChange: func,
  424. label: string.isRequired,
  425. name: string.isRequired,
  426. };
  427.  
  428. export default withStyles(styles)(TextField);
Add Comment
Please, Sign In to add comment