import { InputAdornment, TextField } from "@material-ui/core"; import makeStyles from "@material-ui/core/styles/makeStyles"; import { getMeasurementUnitMessage } from "@saleor/attributes/components/AttributeDetails/utils"; import { AttributeInput } from "@saleor/components/Attributes/Attributes"; import BasicAttributeRow from "@saleor/components/Attributes/BasicAttributeRow"; import ExtendedAttributeRow from "@saleor/components/Attributes/ExtendedAttributeRow"; import { getErrorMessage, getFileChoice, getMultiChoices, getMultiDisplayValue, getReferenceDisplayValue, getRichTextData, getSingleChoices, getSingleDisplayValue } from "@saleor/components/Attributes/utils"; import Checkbox from "@saleor/components/Checkbox"; import { DateTimeField } from "@saleor/components/DateTimeField"; import FileUploadField from "@saleor/components/FileUploadField"; import MultiAutocompleteSelectField from "@saleor/components/MultiAutocompleteSelectField"; import RichTextEditor from "@saleor/components/RichTextEditor"; import SingleAutocompleteSelectField from "@saleor/components/SingleAutocompleteSelectField"; import SortableChipsField from "@saleor/components/SortableChipsField"; import { AttributeValueFragment } from "@saleor/fragments/types/AttributeValueFragment"; import { PageErrorWithAttributesFragment } from "@saleor/fragments/types/PageErrorWithAttributesFragment"; import { ProductErrorWithAttributesFragment } from "@saleor/fragments/types/ProductErrorWithAttributesFragment"; import { FormsetChange } from "@saleor/hooks/useFormset"; import { commonMessages } from "@saleor/intl"; import { FetchMoreProps, ReorderEvent } from "@saleor/types"; import { AttributeInputTypeEnum } from "@saleor/types/globalTypes"; import React from "react"; import { defineMessages, useIntl } from "react-intl"; const messages = defineMessages({ multipleValueLabel: { defaultMessage: "Values", description: "attribute values" }, valueLabel: { defaultMessage: "Value", description: "attribute value" } }); const useStyles = makeStyles( () => ({ fileField: { float: "right" }, pullRight: { display: "flex", justifyContent: "flex-end" } }), { name: "AttributeRow" } ); export interface AttributeRowHandlers { onChange: FormsetChange; onFileChange: FormsetChange; onMultiChange: FormsetChange; onReferencesAddClick: (attribute: AttributeInput) => void; onReferencesRemove: FormsetChange; onReferencesReorder: FormsetChange; fetchAttributeValues: (query: string, attributeId: string) => void; fetchMoreAttributeValues: FetchMoreProps; } interface AttributeRowProps extends AttributeRowHandlers { attribute: AttributeInput; attributeValues: AttributeValueFragment[]; disabled: boolean; error: ProductErrorWithAttributesFragment | PageErrorWithAttributesFragment; loading: boolean; entityId: string; onAttributeSelectBlur?: () => void; } const AttributeRow: React.FC = ({ attribute, attributeValues, disabled, error, loading, onFileChange, onMultiChange, onReferencesAddClick, onReferencesRemove, onReferencesReorder, onChange, fetchAttributeValues, fetchMoreAttributeValues, entityId, onAttributeSelectBlur }) => { const intl = useIntl(); const classes = useStyles({}); switch (attribute.data.inputType) { case AttributeInputTypeEnum.REFERENCE: return ( onReferencesAddClick(attribute)} disabled={disabled} > onReferencesRemove( attribute.id, attribute.value?.filter(id => id !== value) ) } onValueReorder={event => onReferencesReorder(attribute.id, event)} loading={loading} error={!!error} helperText={getErrorMessage(error, intl)} /> ); case AttributeInputTypeEnum.FILE: return ( onFileChange(attribute.id, file)} onFileDelete={() => onFileChange(attribute.id, undefined)} error={!!error} helperText={getErrorMessage(error, intl)} inputProps={{ name: `attribute:${attribute.label}` }} /> ); case AttributeInputTypeEnum.DROPDOWN: return ( onChange(attribute.id, event.target.value)} allowCustomValues={true} fetchOnFocus={true} fetchChoices={value => fetchAttributeValues(value, attribute.id)} onBlur={onAttributeSelectBlur} {...fetchMoreAttributeValues} /> ); case AttributeInputTypeEnum.RICH_TEXT: return ( onChange(attribute.id, JSON.stringify(data))} data={getRichTextData(attribute)} /> ); case AttributeInputTypeEnum.NUMERIC: return ( onChange(attribute.id, event.target.value)} type="number" value={attribute.value[0]} InputProps={ attribute.data.unit && { endAdornment: ( {getMeasurementUnitMessage( attribute.data.unit, intl.formatMessage )} ) } } /> ); case AttributeInputTypeEnum.BOOLEAN: return (
onChange(attribute.id, JSON.stringify(event.target.checked)) } checked={JSON.parse(attribute.value[0] ?? "false")} className={classes.pullRight} helperText={getErrorMessage(error, intl)} error={!!error} />
); case AttributeInputTypeEnum.DATE: return ( onChange(attribute.id, event.target.value)} type="date" value={attribute.value[0]} InputLabelProps={{ shrink: true }} /> ); case AttributeInputTypeEnum.DATE_TIME: return ( onChange(attribute.id, value)} /> ); default: return ( onMultiChange(attribute.id, event.target.value)} allowCustomValues={true} fetchOnFocus={true} fetchChoices={value => fetchAttributeValues(value, attribute.id)} onBlur={onAttributeSelectBlur} {...fetchMoreAttributeValues} /> ); } }; AttributeRow.displayName = "AttributeRow"; export default AttributeRow;