Source: components/criteria_sidebar/CriteriaSidebarCollapse.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. import ClayBadge from '@clayui/badge';
  15. import ClayIcon from '@clayui/icon';
  16. import getCN from 'classnames';
  17. import dateFns from 'date-fns';
  18. import PropTypes from 'prop-types';
  19. import React from 'react';
  20. import {PROPERTY_TYPES} from '../../utils/constants.es';
  21. import {propertyGroupShape} from '../../utils/types.es';
  22. import {jsDatetoYYYYMMDD} from '../../utils/utils.es';
  23. import CriteriaSidebarItem from './CriteriaSidebarItem.es';
  24. const INPUT_DATE_FORMAT = 'YYYY-MM-DD';
  25. /**
  26. * Returns a default value for a property provided.
  27. * @param {Object} property
  28. * @returns {string}
  29. */
  30. function getDefaultValue(property) {
  31. const {options, type} = property;
  32. let defaultValue = '';
  33. if (type === PROPERTY_TYPES.STRING && options && options.length) {
  34. defaultValue = options[0].value;
  35. }
  36. else if (type === PROPERTY_TYPES.DATE) {
  37. defaultValue = jsDatetoYYYYMMDD(new Date());
  38. }
  39. else if (type === PROPERTY_TYPES.DATE_TIME) {
  40. const simpleDate = jsDatetoYYYYMMDD(new Date());
  41. defaultValue = dateFns
  42. .parse(simpleDate, INPUT_DATE_FORMAT)
  43. .toISOString();
  44. }
  45. else if (type === PROPERTY_TYPES.BOOLEAN) {
  46. defaultValue = 'true';
  47. }
  48. else if (type === PROPERTY_TYPES.INTEGER && options && options.length) {
  49. defaultValue = options[0].value;
  50. }
  51. else if (type === PROPERTY_TYPES.INTEGER) {
  52. defaultValue = 0;
  53. }
  54. else if (type === PROPERTY_TYPES.DOUBLE && options && options.length) {
  55. defaultValue = options[0].value;
  56. }
  57. else if (type === PROPERTY_TYPES.DOUBLE) {
  58. defaultValue = '0.00';
  59. }
  60. return defaultValue;
  61. }
  62. /**
  63. * Filters properties by label
  64. */
  65. function filterProperties(properties, searchValue) {
  66. return properties.filter((property) => {
  67. const propertyLabel = property.label.toLowerCase();
  68. return propertyLabel.indexOf(searchValue.toLowerCase()) !== -1;
  69. });
  70. }
  71. const CriteriaSidebarCollapse = ({
  72. onCollapseClick,
  73. propertyGroups,
  74. propertyKey,
  75. searchValue,
  76. }) => {
  77. const _handleClick = (key, editing) => () => onCollapseClick(key, editing);
  78. return (
  79. <ul className="list-unstyled sidebar-collapse-groups">
  80. {propertyGroups.map((propertyGroup) => {
  81. const key = propertyGroup.propertyKey;
  82. const active = key === propertyKey;
  83. const properties = propertyGroup
  84. ? propertyGroup.properties
  85. : [];
  86. const filteredProperties = searchValue
  87. ? filterProperties(properties, searchValue)
  88. : properties;
  89. const activeClasses = getCN({
  90. active,
  91. });
  92. const propertyListClasses = getCN(
  93. 'properties-list',
  94. activeClasses
  95. );
  96. return (
  97. <li
  98. className={`sidebar-collapse-${propertyGroup.propertyKey}`}
  99. key={key}
  100. >
  101. <div
  102. className="sidebar-collapse-header-root"
  103. onClick={_handleClick(key, active)}
  104. >
  105. <a className="d-flex justify-content-between sidebar-collapse-header">
  106. {propertyGroup.name}
  107. {searchValue && (
  108. <ClayBadge
  109. className="ml-auto mr-2"
  110. displayType="secondary"
  111. label={filteredProperties.length}
  112. />
  113. )}
  114. <span className="collapse-icon">
  115. <ClayIcon
  116. className={activeClasses}
  117. symbol="angle-right"
  118. />
  119. </span>
  120. </a>
  121. </div>
  122. <ul className={propertyListClasses}>
  123. {active && filteredProperties.length === 0 && (
  124. <li className="empty-message">
  125. {Liferay.Language.get(
  126. 'no-results-were-found'
  127. )}
  128. </li>
  129. )}
  130. {active &&
  131. filteredProperties.length > 0 &&
  132. filteredProperties.map(
  133. ({label, name, options, type}) => {
  134. const defaultValue = getDefaultValue({
  135. label,
  136. name,
  137. options,
  138. type,
  139. });
  140. return (
  141. <CriteriaSidebarItem
  142. className={`color--${key}`}
  143. defaultValue={defaultValue}
  144. key={name}
  145. label={label}
  146. name={name}
  147. propertyKey={key}
  148. type={type}
  149. />
  150. );
  151. }
  152. )}
  153. </ul>
  154. </li>
  155. );
  156. })}
  157. </ul>
  158. );
  159. };
  160. CriteriaSidebarCollapse.propTypes = {
  161. onCollapseClick: PropTypes.func,
  162. propertyGroups: PropTypes.arrayOf(propertyGroupShape),
  163. propertyKey: PropTypes.string,
  164. searchValue: PropTypes.string,
  165. };
  166. export default CriteriaSidebarCollapse;