| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- import React, { Component } from "react";
- import styled from "styled-components";
- import loading from "assets/loading.gif";
- type Props = {
- text?: string;
- onClick: () => void;
- disabled?: boolean;
- status?: string | null;
- color?: string;
- rounded?: boolean;
- helper?: string | null;
- saveText?: string | null;
- // Makes flush with corner if not within a modal
- makeFlush?: boolean;
- clearPosition?: boolean;
- statusPosition?: "right" | "left";
- // Provide the classname to modify styles from other components
- className?: string;
- successText?: string;
- absoluteSave?: boolean;
- };
- const SaveButton: React.FC<Props> = (props) => {
- const renderStatus = () => {
- if (props.status) {
- if (props.status === "successful") {
- return (
- <StatusWrapper position={props.statusPosition} successful={true}>
- <i className="material-icons">done</i>
- <StatusTextWrapper>
- {props?.successText || "Successfully updated"}
- </StatusTextWrapper>
- </StatusWrapper>
- );
- } else if (props.status === "loading") {
- return (
- <StatusWrapper position={props.statusPosition} successful={false}>
- <LoadingGif src={loading} />
- <StatusTextWrapper>
- {props.saveText || "Updating . . ."}
- </StatusTextWrapper>
- </StatusWrapper>
- );
- } else if (props.status === "error") {
- return (
- <StatusWrapper position={props.statusPosition} successful={false}>
- <i className="material-icons">error_outline</i>
- <StatusTextWrapper>Could not update</StatusTextWrapper>
- </StatusWrapper>
- );
- } else {
- return (
- <StatusWrapper position={props.statusPosition} successful={false}>
- <i className="material-icons">error_outline</i>
- <StatusTextWrapper>{props.status}</StatusTextWrapper>
- </StatusWrapper>
- );
- }
- } else if (props.helper) {
- return (
- <StatusWrapper position={props.statusPosition} successful={true}>
- {props.helper}
- </StatusWrapper>
- );
- }
- };
- return (
- <ButtonWrapper
- absoluteSave={props.absoluteSave}
- makeFlush={props.makeFlush}
- clearPosition={props.clearPosition}
- className={props.className}
- >
- {props.statusPosition !== "right" && <div>{renderStatus()}</div>}
- <Button
- rounded={props.rounded}
- disabled={props.disabled}
- onClick={props.onClick}
- color={props.color}
- >
- {props.children || props.text}
- </Button>
- {props.statusPosition === "right" && <div>{renderStatus()}</div>}
- </ButtonWrapper>
- );
- };
- export default SaveButton;
- const LoadingGif = styled.img`
- width: 15px;
- height: 15px;
- margin-right: 9px;
- margin-bottom: 0px;
- `;
- const StatusTextWrapper = styled.p`
- display: -webkit-box;
- line-clamp: 2;
- -webkit-line-clamp: 2;
- -webkit-box-orient: vertical;
- line-height: 19px;
- margin: 0;
- `;
- // TODO: prevent status re-render on form refresh to allow animation
- // animation: statusFloatIn 0.5s;
- const StatusWrapper = styled.div<{
- successful: boolean;
- position: "right" | "left";
- }>`
- display: flex;
- align-items: center;
- font-family: "Work Sans", sans-serif;
- font-size: 13px;
- color: #ffffff55;
- ${(props) => {
- if (props.position !== "right") {
- return "margin-right: 25px;";
- }
- return "margin-left: 25px;";
- }}
- max-width: 500px;
- overflow: hidden;
- text-overflow: ellipsis;
- > i {
- font-size: 18px;
- margin-right: 10px;
- float: left;
- color: ${(props) => (props.successful ? "#4797ff" : "#fcba03")};
- }
- animation-fill-mode: forwards;
- @keyframes statusFloatIn {
- from {
- opacity: 0;
- transform: translateY(10px);
- }
- to {
- opacity: 1;
- transform: translateY(0px);
- }
- }
- `;
- const ButtonWrapper = styled.div`
- ${(props: { makeFlush: boolean; clearPosition?: boolean; absoluteSave: boolean }) => {
- const baseStyles = `
- display: flex;
- position: ${props.absoluteSave ? "absolute" : ""};
- bottom: ${props.absoluteSave ? "5px" : ""};
- align-items: center;
- z-index: 99;
- `;
- if (props.clearPosition) {
- return baseStyles;
- }
- if (!props.makeFlush) {
- return `
- ${baseStyles}
- position: absolute;
- justify-content: flex-end;
- bottom: 25px;
- right: 27px;
- left: 27px;
- `;
- }
- return `
- ${baseStyles}
- position: absolute;
- justify-content: flex-end;
- bottom: 5px;
- right: 0;
- `;
- }}
- `;
- const Button = styled.button<{
- disabled: boolean;
- color: string;
- rounded: boolean;
- }>`
- height: 35px;
- font-size: 13px;
- font-weight: 500;
- font-family: "Work Sans", sans-serif;
- color: white;
- display: flex;
- align-items: center;
- padding: 6px 20px 7px 20px;
- text-align: left;
- border: 0;
- border-radius: ${(props) => (props.rounded ? "100px" : "5px")};
- background: ${(props) => (!props.disabled ? (props.color || props.theme.button) : "#aaaabb")};
- cursor: ${(props) => (props.disabled ? "not-allowed" : "pointer")};
- user-select: none;
- :focus {
- outline: 0;
- }
- :hover {
- filter: ${(props) => (!props.disabled ? "brightness(120%)" : "")};
- }
- > i {
- color: white;
- width: 18px;
- height: 18px;
- font-weight: 600;
- font-size: 14px;
- border-radius: 20px;
- display: flex;
- align-items: center;
- margin-right: 10px;
- margin-left: -5px;
- justify-content: center;
- }
- `;
|