import * as React from "react"; // import ReactJson from "react-json-view"; // import styled from "styled-components"; import { get, head } from "lodash"; // import { Grid } from "@material-ui/core"; import TextField from "@material-ui/core/TextField"; import Icon from "@sc/plugins/webcomponents/v2/Icon"; import { DropdownFieldProps } from "../types"; import { EditorMode } from "@sc/modules/v2/Editor/types"; import { AttributeContainer } from "@sc/modules/v2/CRUD/AttributeBuilder"; import { AttributeNames } from "@sc/modules/v2/CRUD/AttributeBuilder/types"; // import IconSelector from "@sc/modules/v2/Properties/components/properties/IconSelector"; import SortableList from "@sc/modules/v2/CRUD/SortableList"; import { LiveFormFieldProps } from "../../LiveFormField/types"; /** * A component that shows a LIVE dropdown input field or tools for configuring that dropdown field's look, feel, and behavior */ const DropdownField: React.FC = (props) => { const { mode = EditorMode.LIVE, placeholder, data = [], items = [], styles = { labelStyle: {}, descriptionStyle: {}, inputStyle: {}, containerStyle: {}, validationStyle: {}, iconStyle: {}, }, name, label, description, defaultValue, disabled, icon, columns = 12, onBlur = () => null, onChange = () => null, attributeContainerDataSettings = {}, } = props; /** * Deals with any changes that comes from the various attributes that are loaded * * • Converts to LiveFormFieldProps shape * • Triggers (and passes along the mutated LiveFormFieldProps payload to) the onChange event * @params */ const handleChange = (data: LiveFormFieldProps): void => { console.log({ data }); onChange(data); }; const handleBlur = (data: LiveFormFieldProps): void => { onBlur(data); }; const { iconStyle = {}, descriptionStyle = {}, containerStyle = {}, inputStyle = {}, validationStyle = {}, labelStyle = {}, } = styles; const ItemComponent = (itm) => { const [value, setValue] = React.useState(""); React.useEffect(() => { setValue(itm.value); }, []); return (
e.stopPropagation()} onBlur={(e) => { const k = items.findIndex((dta) => dta.name === itm.name); const item = head(items.filter((dta) => dta.name === itm.name)); // console.log({ item }); // setValue(e.target.value); const updatedData = { ...data, items: [ ...items.slice(0, k), { ...item, value: e.target.value }, ...items.slice(k + 1), ], }; handleChange(updatedData); handleBlur(updatedData); }} onChange={(e) => { setValue(e.target.value); }} />
); }; return (
{mode === EditorMode.LIVE && (
{label &&
{label}
} {description &&
{description}
} {icon && ( )}
)} {mode === EditorMode.EDITOR && (
- Dropdown Items -
({ ...itm, id: itm.name }))} ItemComponent={ItemComponent} canEdit={false} onAdd={() => { const updatedData = { ...data, items: items.length ? [ ...items, { ...items[items.length - 1], name: Math.random() .toString(36) .slice(2), id: Math.random() .toString(36) .slice(2), }, ] : [ { id: Math.random() .toString(36) .slice(2), name: Math.random() .toString(36) .slice(2), value: "", }, ], }; handleChange(updatedData); handleBlur(updatedData); }} onChange={(updatedData) => { handleChange({ ...data, items: updatedData }); handleBlur({ ...data, items: updatedData }); }} onDelete={(payload, updatedData) => { handleChange({ ...data, items: updatedData }); handleBlur({ ...data, items: updatedData }); }} canDragAndDrop />
)}
); }; export default DropdownField;