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) {
  49. defaultValue = 0;
  50. }
  51. else if (type === PROPERTY_TYPES.DOUBLE) {
  52. defaultValue = '0.00';
  53. }
  54. return defaultValue;
  55. }
  56. /**
  57. * Filters properties by label
  58. */
  59. function filterProperties(properties, searchValue) {
  60. return properties.filter(property => {
  61. const propertyLabel = property.label.toLowerCase();
  62. return propertyLabel.indexOf(searchValue.toLowerCase()) !== -1;
  63. });
  64. }
  65. const CriteriaSidebarCollapse = ({
  66. onCollapseClick,
  67. propertyGroups,
  68. propertyKey,
  69. searchValue,
  70. }) => {
  71. const _handleClick = (key, editing) => () => onCollapseClick(key, editing);
  72. return (
  73. <ul className="list-unstyled sidebar-collapse-groups">
  74. {propertyGroups.map(propertyGroup => {
  75. const key = propertyGroup.propertyKey;
  76. const active = key === propertyKey;
  77. const properties = propertyGroup
  78. ? propertyGroup.properties
  79. : [];
  80. const filteredProperties = searchValue
  81. ? filterProperties(properties, searchValue)
  82. : properties;
  83. const activeClasses = getCN({
  84. active,
  85. });
  86. const propertyListClasses = getCN(
  87. 'properties-list',
  88. activeClasses
  89. );
  90. return (
  91. <li
  92. className={`sidebar-collapse-${propertyGroup.propertyKey}`}
  93. key={key}
  94. >
  95. <div
  96. className="sidebar-collapse-header-root"
  97. onClick={_handleClick(key, active)}
  98. >
  99. <a className="d-flex justify-content-between sidebar-collapse-header">
  100. {propertyGroup.name}
  101. {searchValue && (
  102. <ClayBadge
  103. className="ml-auto mr-2"
  104. displayType="secondary"
  105. label={filteredProperties.length}
  106. />
  107. )}
  108. <span className="collapse-icon">
  109. <ClayIcon
  110. className={activeClasses}
  111. symbol="angle-right"
  112. />
  113. </span>
  114. </a>
  115. </div>
  116. <ul className={propertyListClasses}>
  117. {active && filteredProperties.length === 0 && (
  118. <li className="empty-message">
  119. {Liferay.Language.get(
  120. 'no-results-were-found'
  121. )}
  122. </li>
  123. )}
  124. {active &&
  125. filteredProperties.length > 0 &&
  126. filteredProperties.map(
  127. ({label, name, options, type}) => {
  128. const defaultValue = getDefaultValue({
  129. label,
  130. name,
  131. options,
  132. type,
  133. });
  134. return (
  135. <CriteriaSidebarItem
  136. className={`color--${key}`}
  137. defaultValue={defaultValue}
  138. key={name}
  139. label={label}
  140. name={name}
  141. propertyKey={key}
  142. type={type}
  143. />
  144. );
  145. }
  146. )}
  147. </ul>
  148. </li>
  149. );
  150. })}
  151. </ul>
  152. );
  153. };
  154. CriteriaSidebarCollapse.propTypes = {
  155. onCollapseClick: PropTypes.func,
  156. propertyGroups: PropTypes.arrayOf(propertyGroupShape),
  157. propertyKey: PropTypes.string,
  158. searchValue: PropTypes.string,
  159. };
  160. export default CriteriaSidebarCollapse;