SaveButton.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import loading from "assets/loading.gif";
  4. type Props = {
  5. text?: string;
  6. onClick: () => void;
  7. disabled?: boolean;
  8. status?: string | null;
  9. color?: string;
  10. rounded?: boolean;
  11. helper?: string | null;
  12. saveText?: string | null;
  13. // Makes flush with corner if not within a modal
  14. makeFlush?: boolean;
  15. clearPosition?: boolean;
  16. statusPosition?: "right" | "left";
  17. // Provide the classname to modify styles from other components
  18. className?: string;
  19. successText?: string;
  20. };
  21. const SaveButton: React.FC<Props> = (props) => {
  22. const renderStatus = () => {
  23. if (props.status) {
  24. if (props.status === "successful") {
  25. return (
  26. <StatusWrapper position={props.statusPosition} successful={true}>
  27. <i className="material-icons">done</i>
  28. <StatusTextWrapper>
  29. {props?.successText || "Successfully updated"}
  30. </StatusTextWrapper>
  31. </StatusWrapper>
  32. );
  33. } else if (props.status === "loading") {
  34. return (
  35. <StatusWrapper position={props.statusPosition} successful={false}>
  36. <LoadingGif src={loading} />
  37. <StatusTextWrapper>
  38. {props.saveText || "Updating . . ."}
  39. </StatusTextWrapper>
  40. </StatusWrapper>
  41. );
  42. } else if (props.status === "error") {
  43. return (
  44. <StatusWrapper position={props.statusPosition} successful={false}>
  45. <i className="material-icons">error_outline</i>
  46. <StatusTextWrapper>Could not update</StatusTextWrapper>
  47. </StatusWrapper>
  48. );
  49. } else {
  50. return (
  51. <StatusWrapper position={props.statusPosition} successful={false}>
  52. <i className="material-icons">error_outline</i>
  53. <StatusTextWrapper>{props.status}</StatusTextWrapper>
  54. </StatusWrapper>
  55. );
  56. }
  57. } else if (props.helper) {
  58. return (
  59. <StatusWrapper position={props.statusPosition} successful={true}>
  60. {props.helper}
  61. </StatusWrapper>
  62. );
  63. }
  64. };
  65. return (
  66. <ButtonWrapper
  67. makeFlush={props.makeFlush}
  68. clearPosition={props.clearPosition}
  69. className={props.className}
  70. >
  71. {props.statusPosition !== "right" && <div>{renderStatus()}</div>}
  72. <Button
  73. rounded={props.rounded}
  74. disabled={props.disabled}
  75. onClick={props.onClick}
  76. color={props.color || "#5561C0"}
  77. >
  78. {props.children || props.text}
  79. </Button>
  80. {props.statusPosition === "right" && <div>{renderStatus()}</div>}
  81. </ButtonWrapper>
  82. );
  83. };
  84. export default SaveButton;
  85. const LoadingGif = styled.img`
  86. width: 15px;
  87. height: 15px;
  88. margin-right: 9px;
  89. margin-bottom: 0px;
  90. `;
  91. const StatusTextWrapper = styled.p`
  92. display: -webkit-box;
  93. line-clamp: 2;
  94. -webkit-line-clamp: 2;
  95. -webkit-box-orient: vertical;
  96. line-height: 19px;
  97. margin: 0;
  98. `;
  99. // TODO: prevent status re-render on form refresh to allow animation
  100. // animation: statusFloatIn 0.5s;
  101. const StatusWrapper = styled.div<{
  102. successful: boolean;
  103. position: "right" | "left";
  104. }>`
  105. display: flex;
  106. align-items: center;
  107. font-family: "Work Sans", sans-serif;
  108. font-size: 13px;
  109. color: #ffffff55;
  110. ${(props) => {
  111. if (props.position !== "right") {
  112. return "margin-right: 25px;";
  113. }
  114. return "margin-left: 25px;";
  115. }}
  116. max-width: 500px;
  117. overflow: hidden;
  118. text-overflow: ellipsis;
  119. > i {
  120. font-size: 18px;
  121. margin-right: 10px;
  122. float: left;
  123. color: ${(props) => (props.successful ? "#4797ff" : "#fcba03")};
  124. }
  125. animation-fill-mode: forwards;
  126. @keyframes statusFloatIn {
  127. from {
  128. opacity: 0;
  129. transform: translateY(10px);
  130. }
  131. to {
  132. opacity: 1;
  133. transform: translateY(0px);
  134. }
  135. }
  136. `;
  137. const ButtonWrapper = styled.div`
  138. ${(props: { makeFlush: boolean; clearPosition?: boolean }) => {
  139. const baseStyles = `
  140. display: flex;
  141. align-items: center;
  142. z-index: 99;
  143. `;
  144. if (props.clearPosition) {
  145. return baseStyles;
  146. }
  147. if (!props.makeFlush) {
  148. return `
  149. ${baseStyles}
  150. position: absolute;
  151. justify-content: flex-end;
  152. bottom: 25px;
  153. right: 27px;
  154. left: 27px;
  155. `;
  156. }
  157. return `
  158. ${baseStyles}
  159. position: absolute;
  160. justify-content: flex-end;
  161. bottom: 5px;
  162. right: 0;
  163. `;
  164. }}
  165. `;
  166. const Button = styled.button<{
  167. disabled: boolean;
  168. color: string;
  169. rounded: boolean;
  170. }>`
  171. height: 35px;
  172. font-size: 13px;
  173. font-weight: 500;
  174. font-family: "Work Sans", sans-serif;
  175. color: white;
  176. display: flex;
  177. align-items: center;
  178. padding: 6px 20px 7px 20px;
  179. text-align: left;
  180. border: 0;
  181. border-radius: ${(props) => (props.rounded ? "100px" : "5px")};
  182. background: ${(props) => (!props.disabled ? props.color : "#aaaabb")};
  183. cursor: ${(props) => (!props.disabled ? "pointer" : "default")};
  184. user-select: none;
  185. :focus {
  186. outline: 0;
  187. }
  188. :hover {
  189. filter: ${(props) => (!props.disabled ? "brightness(120%)" : "")};
  190. }
  191. > i {
  192. color: white;
  193. width: 18px;
  194. height: 18px;
  195. font-weight: 600;
  196. font-size: 14px;
  197. border-radius: 20px;
  198. display: flex;
  199. align-items: center;
  200. margin-right: 10px;
  201. margin-left: -5px;
  202. justify-content: center;
  203. }
  204. `;