UNPKG

@ezform/mui

Version:

Material UI form components for use with EZForm React validation and form library

17 lines (16 loc) 1.57 kB
import React, { memo } from "react"; import { useField, propsEqual } from "@ezform/core"; import { MenuItem, TextField } from "@material-ui/core"; import { FieldReadonly } from "./FieldReadonly"; export var FieldSelect = memo(function (props) { var _a; var id = props.id, name = props.name, form = props.form, _b = props.validator, validator = _b === void 0 ? function () { return null; } : _b, disabled = props.disabled, _c = props.readonly, readonly = _c === void 0 ? form.isReadonly : _c, label = props.label, options = props.options, _d = props.variant, variant = _d === void 0 ? "standard" : _d, defaultValue = props.defaultValue; useField(name, validator, form, defaultValue); var handleChange = function (e) { form.setField(name, e.target.value); }; if (readonly) { return (React.createElement(FieldReadonly, { variant: variant, name: name, id: id, label: label, value: ((_a = options.find(function (f) { return f.value === form.getField(name); })) === null || _a === void 0 ? void 0 : _a.label) || "", fullWidth: true })); } return (React.createElement(TextField, { select: true, label: label, variant: variant, id: id, value: form.getField(name) || "", onChange: handleChange, disabled: disabled, error: form.hasError(name), fullWidth: true, helperText: form.getHelperText(name) }, options.map(function (option) { return (React.createElement(MenuItem, { key: option.key, value: option.value, disabled: (option === null || option === void 0 ? void 0 : option.disabled) || false }, option.label)); }))); }, propsEqual);