UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

60 lines 3.55 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(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); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import * as React from 'react'; import { useInput } from "../form/useInput.js"; import { useChoices } from "../form/choices/useChoices.js"; import { useChoicesContext } from "../form/choices/useChoicesContext.js"; import { isRequired } from "../form/validation/validate.js"; import { FieldTitle } from "../util/FieldTitle.js"; export var AutocompleteInput = function (props) { var _a = useChoicesContext(props), allChoices = _a.allChoices, source = _a.source, setFilters = _a.setFilters, filterValues = _a.filterValues; var getChoiceText = useChoices(props).getChoiceText; var _b = useInput(__assign({ source: source }, props)), field = _b.field, fieldState = _b.fieldState; return (React.createElement("div", null, React.createElement("div", null, React.createElement(FieldTitle, { label: props.label, source: props.source, resource: props.resource, isRequired: isRequired(props.validate) })), React.createElement("input", { type: "text", value: filterValues['q'], onChange: function (e) { return setFilters(__assign(__assign({}, filterValues), { q: e.target.value })); } }), React.createElement("button", { type: "button", onClick: function (event) { event.preventDefault(); field.onChange([]); } }, "Clear value"), React.createElement("ul", null, allChoices === null || allChoices === void 0 ? void 0 : allChoices.map(function (choice) { return (React.createElement("li", { key: choice.id }, React.createElement("label", null, React.createElement("input", { name: field.name, type: props.multiple ? 'checkbox' : 'radio', value: choice.id, onChange: function (event) { var newValue = event.target.checked ? props.multiple ? __spreadArray(__spreadArray([], field.value, true), [choice.id], false) : choice.id : props.multiple ? field.value.filter(function (v) { return v !== choice.id; }) : null; field.onChange(newValue); }, checked: props.multiple ? field.value.includes(choice.id) : // eslint-disable-next-line eqeqeq field.value == choice.id, "aria-describedby": fieldState.error ? "error-".concat(props.source) : undefined }), getChoiceText(choice)))); })), fieldState.error ? (React.createElement("p", { id: "error-".concat(props.source), style: { color: 'red' } }, fieldState.error.message)) : null)); }; //# sourceMappingURL=AutocompleteInput.js.map