@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
90 lines (84 loc) • 3.28 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useContext, useRef, useState } from 'react';
import { Alert, styled, Icon } from '@mui/material';
import { SCUserContext } from '@selfcommunity/react-core';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import { PREFIX } from './constants';
import { LoadingButton } from '@mui/lab';
import { defineMessages } from 'react-intl';
const messages = defineMessages({
errorLoadImage: {
id: 'ui.changeGroupCover.button.change.alertErrorImage',
defaultMessage: 'ui.changeGroupCover.button.change.alertErrorImage'
}
});
const classes = {
root: `${PREFIX}-upload-course-cover-root`
};
const Root = styled(LoadingButton, {
name: PREFIX,
slot: 'UploadCourseCoverRoot'
})(() => ({}));
/**
* > API documentation for the Community-JS Change Group Cover component. Learn about the available props and the CSS API.
*
*
* This component renders a button that allows admins to edit the group cover and a popover that specifies format and sizes allowed.
* Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/UploadCourseCover)
#### Import
```jsx
import {UploadCourseCover} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCUploadCourseCover` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCUploadCourseCover-root|Styles applied to the root element.|
* @param inProps
*/
export default function UploadCourseCover(inProps) {
//PROPS
const props = useThemeProps({
props: inProps,
name: PREFIX
});
const { isUploading = false, onChange, className = false } = props, rest = __rest(props, ["isUploading", "onChange", "className"]);
//CONTEXT
const scUserContext = useContext(SCUserContext);
//STATE
let fileInput = useRef(null);
const [alert, setAlert] = useState(null);
// Anonymous
if (!scUserContext.user) {
return null;
}
/**
* Handles file upload
* @param event
*/
const handleUpload = (event) => {
const selectedFile = event.target.files[0];
if (!selectedFile)
return;
fileInput = selectedFile;
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
onChange(fileInput);
};
img.src = e.target.result;
};
reader.readAsDataURL(selectedFile);
};
/**
* If there is an error
*/
if (alert) {
return (_jsx(Alert, Object.assign({ color: "error", onClose: () => setAlert(null) }, { children: alert })));
}
return (_jsxs(_Fragment, { children: [_jsx("input", { type: "file", onChange: handleUpload, ref: fileInput, hidden: true, accept: ".gif,.png,.jpg,.jpeg" }), _jsx(Root, Object.assign({ className: classNames(classes.root, className), size: "medium", variant: "contained", disabled: isUploading, onClick: () => fileInput.current.click(), loading: isUploading }, rest, { children: _jsx(Icon, { children: "image" }) }))] }));
}