UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

87 lines (85 loc) 3.47 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import { UNDEFINED } from '../../utils/constants'; import useSpreadState from '../../hooks/useSpreadState'; export function parseDashletContentHeight(contentHeight) { return contentHeight ? parseInt(`${contentHeight}`.replace('px', '')) : UNDEFINED; } export function getPersonFullName(person) { return `${person.firstName} ${person.lastName}`; } export function useSpreadStateWithSelected(initialState) { // @ts-ignore - Unsure how to make the compiler happy. Probably due to the generic ItemType of WithSelectedState. const [state, setState] = useSpreadState( Object.assign( { items: null, isAllSelected: false, hasSelected: false, selected: {}, selectedCount: 0 }, initialState ) ); const { items, selected } = state; const onSelectAll = (e) => { if (items.length > 0) { const nextState = {}; if (e.target.checked) { // Check all nextState.hasSelected = true; nextState.selectedCount = items.length; nextState.selected = items.reduce((state, item) => { state[item.id] = true; return state; }, {}); nextState.isAllSelected = true; } else { // Uncheck all nextState.selected = {}; nextState.hasSelected = false; nextState.isAllSelected = false; nextState.selectedCount = 0; } setState(nextState); } }; const onSelectItem = (e, item) => { let isChecked = e.target.checked; let nextState = {}; nextState.selected = Object.assign(Object.assign({}, selected), { [item.id]: isChecked }); let checkedOnly = Object.values(nextState.selected).filter(Boolean); nextState.hasSelected = checkedOnly.length > 0; nextState.isAllSelected = items.length && checkedOnly.length === items.length; nextState.selectedCount = isChecked ? state.selectedCount + 1 : state.selectedCount - 1; setState(nextState); }; const isSelected = (item) => { var _a; return (_a = selected[item.id]) !== null && _a !== void 0 ? _a : false; }; return [state, setState, onSelectItem, onSelectAll, isSelected]; }