import { Card, CardContent, IconButton, Typography } from "@material-ui/core"; import ArrowDropDownIcon from "@material-ui/icons/ArrowDropDown"; import { AttributeReference } from "@saleor/attributes/utils/data"; import CardTitle from "@saleor/components/CardTitle"; import Hr from "@saleor/components/Hr"; import { AttributeValueFragment } from "@saleor/fragments/types/AttributeValueFragment"; import { PageErrorWithAttributesFragment } from "@saleor/fragments/types/PageErrorWithAttributesFragment"; import { ProductErrorWithAttributesFragment } from "@saleor/fragments/types/ProductErrorWithAttributesFragment"; import { FormsetAtomicData } from "@saleor/hooks/useFormset"; import { makeStyles } from "@saleor/macaw-ui"; import { FetchMoreProps } from "@saleor/types"; import { AttributeEntityTypeEnum, AttributeInputTypeEnum, MeasurementUnitsEnum } from "@saleor/types/globalTypes"; import classNames from "classnames"; import React from "react"; import { defineMessages, FormattedMessage, useIntl } from "react-intl"; import AttributeRow, { AttributeRowHandlers } from "./AttributeRow"; import { VariantAttributeScope } from "./types"; export interface AttributeInputData { inputType: AttributeInputTypeEnum; entityType?: AttributeEntityTypeEnum; unit?: MeasurementUnitsEnum | null; variantAttributeScope?: VariantAttributeScope; isRequired: boolean; values: AttributeValueFragment[]; selectedValues?: AttributeValueFragment[]; references?: AttributeReference[]; } export type AttributeInput = FormsetAtomicData; export type AttributeFileInput = FormsetAtomicData; export interface AttributesProps extends AttributeRowHandlers { attributes: AttributeInput[]; attributeValues: AttributeValueFragment[]; fetchAttributeValues: (query: string, attributeId: string) => void; fetchMoreAttributeValues: FetchMoreProps; onAttributeSelectBlur: () => void; disabled: boolean; loading: boolean; errors: Array< ProductErrorWithAttributesFragment | PageErrorWithAttributesFragment >; title?: React.ReactNode; entityId?: string; } const useStyles = makeStyles( theme => ({ attributeSection: { "&:last-of-type": { paddingBottom: 0 }, padding: theme.spacing(2, 0) }, attributeSectionLabel: { alignItems: "center", display: "flex" }, card: { overflow: "visible" }, cardContent: { "&:last-child": { paddingBottom: theme.spacing(1) }, paddingTop: theme.spacing(1) }, expansionBar: { display: "flex" }, expansionBarButton: { marginBottom: theme.spacing(1) }, expansionBarButtonIcon: { transition: theme.transitions.duration.short + "ms" }, expansionBarLabel: { color: theme.palette.text.disabled, fontSize: 14 }, expansionBarLabelContainer: { alignItems: "center", display: "flex", flex: 1 }, rotate: { transform: "rotate(180deg)" }, uploadFileButton: { float: "right" }, uploadFileContent: { color: theme.palette.primary.main, float: "right", fontSize: theme.typography.body1.fontSize } }), { name: "Attributes" } ); const messages = defineMessages({ attributesNumber: { defaultMessage: "{number} Attributes", description: "number of attributes" }, header: { defaultMessage: "Attributes", description: "attributes, section header" } }); const Attributes: React.FC = ({ attributes, attributeValues, errors, title, onAttributeSelectBlur, entityId = "_defaultId", ...props }) => { const intl = useIntl(); const classes = useStyles({}); const [expanded, setExpansionStatus] = React.useState(true); const toggleExpansion = () => setExpansionStatus(!expanded); return (
{expanded && attributes.length > 0 && ( <>
{attributes.map((attribute, attributeIndex) => { const error = errors.find(err => err.attributes?.includes(attribute.id) ); return ( {attributeIndex > 0 &&
}
); })} )}
); }; Attributes.displayName = "Attributes"; export default Attributes;