input-statuses.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import dynamic from 'next/dynamic';
  2. // Lazy loaded components
  3. const CheckCircleFilled = dynamic(() => import('@ant-design/icons/CheckCircleFilled'), {
  4. ssr: false,
  5. });
  6. const ExclamationCircleFilled = dynamic(() => import('@ant-design/icons/ExclamationCircleFilled'), {
  7. ssr: false,
  8. });
  9. const LoadingOutlined = dynamic(() => import('@ant-design/icons/LoadingOutlined'), {
  10. ssr: false,
  11. });
  12. const WarningOutlined = dynamic(() => import('@ant-design/icons/WarningOutlined'), {
  13. ssr: false,
  14. });
  15. export const STATUS_ERROR = 'error';
  16. export const STATUS_PROCESSING = 'proessing';
  17. export const STATUS_SUCCESS = 'success';
  18. export const STATUS_WARNING = 'warning';
  19. const STATUS_INVALID = 'invalid';
  20. export type InputStatusTypes = 'error' | 'invalid' | 'proessing' | 'success' | 'warning';
  21. export interface StatusState {
  22. type: InputStatusTypes;
  23. icon: any; // Element type of sorts?
  24. message: string;
  25. }
  26. interface InputStates {
  27. [key: string]: StatusState;
  28. }
  29. const INPUT_STATES: InputStates = {
  30. [STATUS_SUCCESS]: {
  31. type: STATUS_SUCCESS,
  32. icon: <CheckCircleFilled style={{ color: 'green' }} />,
  33. message: 'Success!',
  34. },
  35. [STATUS_ERROR]: {
  36. type: STATUS_ERROR,
  37. icon: <ExclamationCircleFilled style={{ color: 'red' }} />,
  38. message: 'An error occurred.',
  39. },
  40. [STATUS_INVALID]: {
  41. type: STATUS_INVALID,
  42. icon: <ExclamationCircleFilled style={{ color: 'red' }} />,
  43. message: 'An error occurred.',
  44. },
  45. [STATUS_PROCESSING]: {
  46. type: STATUS_PROCESSING,
  47. icon: <LoadingOutlined />,
  48. message: '',
  49. },
  50. [STATUS_WARNING]: {
  51. type: STATUS_WARNING,
  52. icon: <WarningOutlined style={{ color: '#fc0' }} />,
  53. message: '',
  54. },
  55. };
  56. // Don't like any of the default messages in INPUT_STATES? Create a state with custom message by providing an icon style with your message.
  57. export function createInputStatus(type: InputStatusTypes, message?: string): StatusState {
  58. if (!type || !INPUT_STATES[type]) {
  59. return null;
  60. }
  61. if (!message) {
  62. return INPUT_STATES[type];
  63. }
  64. return {
  65. type,
  66. icon: INPUT_STATES[type].icon,
  67. message,
  68. };
  69. }