fix hierarchical tag toggle

This commit is contained in:
Lachlan Kermode
2020-06-23 11:49:14 +02:00
parent 51b0f4a115
commit e5dff2ad86
2 changed files with 18 additions and 10 deletions

View File

@@ -1,14 +1,14 @@
import React, { useState } from 'react'
export default ({ showing, onClickHandler, timelineDims }) => {
const [checked, setChecked] = useState(true)
const handleCheck = () => setChecked(!checked)
const onNarrativise = () => onClickHandler(checked)
if (!showing) {
return null
}
const [checked, setChecked] = useState(true)
const handleCheck = () => setChecked(!checked)
const onNarrativise = () => onClickHandler(checked)
return <div className='stateoptions-panel' style={{ bottom: timelineDims.height }}>
<div>
<div className='button' onClick={onNarrativise}>Narrativise</div>

View File

@@ -2,11 +2,19 @@ import React from 'react'
import Checkbox from '../presentational/Checkbox'
import copy from '../../common/data/copy.json'
/** recursively get an array of node keys */
function allAssociatedKeys (node) {
if (!node.children) return [node.key]
const childKeys = Object.values(node.children).flatMap(n => allAssociatedKeys(n))
childKeys.push(node.key)
/** recursively get an array of node keys to toggle */
function childrenToToggle (node, activeFilters, parentOn) {
const isOn = activeFilters.includes(node.key)
if (!node.children) {
return [node.key]
}
const childKeys = Object.values(node.children)
.flatMap(n => childrenToToggle(n, activeFilters, isOn))
// NB: if turning a parent off, don't toggle off children on.
// likewise if turning a parent on, don't toggle on children off
if (!((!parentOn && isOn) || (parentOn && !isOn))) {
childKeys.push(node.key)
}
return childKeys
}
@@ -17,7 +25,7 @@ function FilterListPanel ({
language
}) {
function createNodeComponent (node, depth) {
const matchingKeys = allAssociatedKeys(node)
const matchingKeys = childrenToToggle(node, activeFilters, activeFilters.includes(node.key))
const children = Object.values(node.children)
return (
<li