UNPKG

formik-material-ui

Version:

[![Build Status](https://travis-ci.org/stackworx/formik-material-ui.svg?branch=master)](https://travis-ci.org/stackworx/formik-material-ui) [![license](https://badgen.now.sh/badge/license/MIT)](./LICENSE) [![Coverage Status](https://coveralls.io/repos/git

143 lines (128 loc) 7.61 kB
import MuiTextField from '@material-ui/core/TextField'; import MuiSwitch from '@material-ui/core/Switch'; import MuiCheckbox from '@material-ui/core/Checkbox'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import MuiSelect from '@material-ui/core/Select'; import { getIn } from 'formik'; import FormControl from '@material-ui/core/FormControl'; import InputLabel from '@material-ui/core/InputLabel'; import Input from '@material-ui/core/Input'; import FormHelperText from '@material-ui/core/FormHelperText'; import MuiRadioGroup from '@material-ui/core/RadioGroup'; import { createElement, useCallback } from 'react'; import MuiInputBase from '@material-ui/core/InputBase'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; return t; } var fieldToTextField = function (_a) { var field = _a.field, form = _a.form, disabled = _a.disabled, props = __rest(_a, ["field", "form", "disabled"]); var name = field.name; var touched = form.touched, errors = form.errors, isSubmitting = form.isSubmitting; var fieldError = getIn(errors, name); var showError = getIn(touched, name) && !!fieldError; return __assign({}, props, field, { error: showError, helperText: showError ? fieldError : props.helperText, disabled: disabled != undefined ? disabled : isSubmitting }); }; var TextField = function (_a) { var children = _a.children, props = __rest(_a, ["children"]); return (createElement(MuiTextField, __assign({}, fieldToTextField(props)), children)); }; TextField.displayName = 'FormikMaterialUITextField'; var fieldToSwitch = function (_a) { var field = _a.field, isSubmitting = _a.form.isSubmitting, disabled = _a.disabled, props = __rest(_a, ["field", "form", "disabled"]); return __assign({ disabled: disabled != undefined ? disabled : isSubmitting }, props, field, { value: field.name, checked: field.value }); }; var Switch = function (props) { return createElement(MuiSwitch, __assign({}, fieldToSwitch(props))); }; Switch.displayName = 'FormikMaterialUISwitch'; var fieldToCheckbox = function (_a) { var field = _a.field, isSubmitting = _a.form.isSubmitting, disabled = _a.disabled, props = __rest(_a, ["field", "form", "disabled"]); return __assign({ disabled: disabled != undefined ? disabled : isSubmitting }, props, field, { checked: field.value, value: field.value ? 'checked' : '' }); }; var Checkbox = function (props) { return createElement(MuiCheckbox, __assign({}, fieldToCheckbox(props))); }; Checkbox.displayName = 'FormikMaterialUICheckbox'; var CheckboxWithLabel = function (_a) { var Label = _a.Label, props = __rest(_a, ["Label"]); return (createElement(FormControlLabel, __assign({ control: createElement(MuiCheckbox, __assign({}, fieldToCheckbox(props))) }, Label))); }; CheckboxWithLabel.displayName = 'FormikMaterialUICheckboxWithLabel'; var fieldToSelect = function (_a) { var field = _a.field, _b = _a.form, isSubmitting = _b.isSubmitting, setFieldValue = _b.setFieldValue, disabled = _a.disabled, props = __rest(_a, ["field", "form", "disabled"]); var onChange = useCallback(function (event) { if (props.multiple && props.native) { var options = event.target.options; var value = []; for (var i = 0, l = options.length; i < l; i += 1) { if (options[i].selected) { value.push(options[i].value); } } setFieldValue(field.name, value); } else { field.onChange(event); } }, [field.name, props.multiple, props.native]); return __assign({ disabled: disabled != undefined ? disabled : isSubmitting }, props, field, { onChange: onChange }); }; var Select = function (props) { return createElement(MuiSelect, __assign({}, fieldToSelect(props))); }; Select.displayName = 'FormikMaterialUISelect'; var SimpleFileUpload = function (_a) { var label = _a.label, field = _a.field, _b = _a.form, touched = _b.touched, errors = _b.errors, isSubmitting = _b.isSubmitting, setFieldValue = _b.setFieldValue, _c = _a.disabled, disabled = _c === void 0 ? false : _c, inputProps = _a.InputProps, inputLabelProps = _a.InputLabelProps; var error = getIn(touched, field.name) && getIn(errors, field.name); return (createElement("div", null, createElement(FormControl, null, label && (createElement(InputLabel, __assign({ shrink: true, error: !!error }, inputLabelProps), label)), createElement(Input, __assign({ error: !!error, inputProps: { type: 'file', disabled: disabled || isSubmitting, name: field.name, onChange: function (event) { var file = event.currentTarget.files[0]; setFieldValue(field.name, file); }, } }, inputProps)), error && createElement(FormHelperText, { error: true }, error)))); }; var fieldToRadioGroup = function (_a) { var field = _a.field, form = _a.form, props = __rest(_a, ["field", "form"]); return (__assign({}, props, field)); }; var RadioGroup = function (props) { return createElement(MuiRadioGroup, __assign({}, fieldToRadioGroup(props))); }; RadioGroup.displayName = 'FormikMaterialUIRadioGroup'; var fieldToInputBase = function (_a) { var field = _a.field, isSubmitting = _a.form.isSubmitting, disabled = _a.disabled, props = __rest(_a, ["field", "form", "disabled"]); return __assign({ disabled: disabled != undefined ? disabled : isSubmitting }, props, field); }; var InputBase = function (props) { return createElement(MuiInputBase, __assign({}, fieldToInputBase(props))); }; InputBase.displayName = 'FormikMaterialUIInputBase'; export { fieldToTextField, TextField, fieldToSwitch, Switch, fieldToCheckbox, Checkbox, CheckboxWithLabel, fieldToSelect, Select, SimpleFileUpload, fieldToRadioGroup, RadioGroup, fieldToInputBase, InputBase }; //# sourceMappingURL=formik-material-ui.es6.js.map