mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-28 17:55:15 +02:00
Make accordion expand dependent on text width
This commit is contained in:
parent
4c5eb20e09
commit
ffc0c0a229
@ -12,6 +12,7 @@ import {
|
|||||||
} from 'constants/operators';
|
} from 'constants/operators';
|
||||||
|
|
||||||
import { useStyles } from '../ConstraintAccordion.styles';
|
import { useStyles } from '../ConstraintAccordion.styles';
|
||||||
|
import {useState} from "react";
|
||||||
interface IConstraintAccordionViewProps {
|
interface IConstraintAccordionViewProps {
|
||||||
constraint: IConstraint;
|
constraint: IConstraint;
|
||||||
onDelete?: () => void;
|
onDelete?: () => void;
|
||||||
@ -26,27 +27,34 @@ export const ConstraintAccordionView = ({
|
|||||||
onDelete,
|
onDelete,
|
||||||
}: IConstraintAccordionViewProps) => {
|
}: IConstraintAccordionViewProps) => {
|
||||||
const { classes: styles } = useStyles();
|
const { classes: styles } = useStyles();
|
||||||
|
const [expandable, setExpandable] = useState(true)
|
||||||
|
const [expanded, setExpanded] = useState(false);
|
||||||
|
|
||||||
const singleValue = oneOf(
|
const singleValue = oneOf(
|
||||||
[...semVerOperators, ...numOperators, ...dateOperators],
|
[...semVerOperators, ...numOperators, ...dateOperators],
|
||||||
constraint.operator
|
constraint.operator
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const handleChange =
|
||||||
|
() => (event: React.SyntheticEvent, isExpanded: boolean) => {
|
||||||
|
setExpanded((!isExpanded && expandable));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Accordion
|
<Accordion
|
||||||
className={styles.accordion}
|
className={styles.accordion}
|
||||||
classes={{ root: styles.accordionRoot }}
|
classes={{ root: styles.accordionRoot }}
|
||||||
|
expanded={expanded}
|
||||||
|
onChange={handleChange}
|
||||||
>
|
>
|
||||||
<AccordionSummary
|
<AccordionSummary className={styles.summary} expandIcon={null}>
|
||||||
className={styles.summary}
|
|
||||||
expandIcon={<ExpandMore titleAccess="Toggle" />}
|
|
||||||
>
|
|
||||||
<ConstraintAccordionViewHeader
|
<ConstraintAccordionViewHeader
|
||||||
compact={compact}
|
compact={compact}
|
||||||
constraint={constraint}
|
constraint={constraint}
|
||||||
onEdit={onEdit}
|
onEdit={onEdit}
|
||||||
onDelete={onDelete}
|
onDelete={onDelete}
|
||||||
singleValue={singleValue}
|
singleValue={singleValue}
|
||||||
|
allowExpand={(shouldExpand) => setExpandable(shouldExpand)}
|
||||||
/>
|
/>
|
||||||
</AccordionSummary>
|
</AccordionSummary>
|
||||||
|
|
||||||
|
@ -5,11 +5,12 @@ import { IConstraint } from 'interfaces/strategy';
|
|||||||
|
|
||||||
import { useStyles } from 'component/common/ConstraintAccordion/ConstraintAccordion.styles';
|
import { useStyles } from 'component/common/ConstraintAccordion/ConstraintAccordion.styles';
|
||||||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
||||||
import React from 'react';
|
import React, { useEffect, useRef, useState } from 'react';
|
||||||
import { formatConstraintValue } from 'utils/formatConstraintValue';
|
import { formatConstraintValue } from 'utils/formatConstraintValue';
|
||||||
import { useLocationSettings } from 'hooks/useLocationSettings';
|
import { useLocationSettings } from 'hooks/useLocationSettings';
|
||||||
import { ConstraintOperator } from 'component/common/ConstraintAccordion/ConstraintOperator/ConstraintOperator';
|
import { ConstraintOperator } from 'component/common/ConstraintAccordion/ConstraintOperator/ConstraintOperator';
|
||||||
import classnames from 'classnames';
|
import classnames from 'classnames';
|
||||||
|
import ReactDOM from 'react-dom';
|
||||||
|
|
||||||
const StyledHeaderText = styled('span')(({ theme }) => ({
|
const StyledHeaderText = styled('span')(({ theme }) => ({
|
||||||
display: '-webkit-box',
|
display: '-webkit-box',
|
||||||
@ -54,6 +55,7 @@ interface IConstraintAccordionViewHeaderProps {
|
|||||||
onDelete?: () => void;
|
onDelete?: () => void;
|
||||||
onEdit?: () => void;
|
onEdit?: () => void;
|
||||||
singleValue: boolean;
|
singleValue: boolean;
|
||||||
|
allowExpand: (shouldExpand: boolean) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ConstraintAccordionViewHeader = ({
|
export const ConstraintAccordionViewHeader = ({
|
||||||
@ -62,9 +64,14 @@ export const ConstraintAccordionViewHeader = ({
|
|||||||
onEdit,
|
onEdit,
|
||||||
onDelete,
|
onDelete,
|
||||||
singleValue,
|
singleValue,
|
||||||
|
allowExpand
|
||||||
}: IConstraintAccordionViewHeaderProps) => {
|
}: IConstraintAccordionViewHeaderProps) => {
|
||||||
const { classes: styles } = useStyles();
|
const { classes: styles } = useStyles();
|
||||||
const { locationSettings } = useLocationSettings();
|
const { locationSettings } = useLocationSettings();
|
||||||
|
const [height, setHeight] = useState(0);
|
||||||
|
const [width, setWidth] = useState(0);
|
||||||
|
const [textWidth, setTextWidth] = useState(0);
|
||||||
|
const elementRef = useRef<HTMLElement>(null);
|
||||||
|
|
||||||
const onEditClick =
|
const onEditClick =
|
||||||
onEdit &&
|
onEdit &&
|
||||||
@ -80,6 +87,35 @@ export const ConstraintAccordionViewHeader = ({
|
|||||||
onDelete();
|
onDelete();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (elementRef && elementRef.current != null) {
|
||||||
|
console.log(elementRef.current.clientHeight);
|
||||||
|
console.log(elementRef.current.clientWidth);
|
||||||
|
setTextWidth(
|
||||||
|
Math.round(getTextWidth(elementRef.current.innerText) / 2) // 2 lines
|
||||||
|
);
|
||||||
|
setHeight(elementRef.current.clientHeight);
|
||||||
|
setWidth(elementRef.current.clientWidth);
|
||||||
|
allowExpand(textWidth > width)
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function getTextWidth(text: string | null) {
|
||||||
|
if (text != null) {
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
const context = canvas.getContext('2d');
|
||||||
|
|
||||||
|
if (context != null) {
|
||||||
|
context.font = getComputedStyle(document.body).font;
|
||||||
|
|
||||||
|
return context.measureText(text).width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const shouldBeExpandable = textWidth > width;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.headerContainer}>
|
<div className={styles.headerContainer}>
|
||||||
<ConstraintIcon />
|
<ConstraintIcon />
|
||||||
@ -104,20 +140,25 @@ export const ConstraintAccordionViewHeader = ({
|
|||||||
}
|
}
|
||||||
elseShow={
|
elseShow={
|
||||||
<div className={styles.headerValuesContainer}>
|
<div className={styles.headerValuesContainer}>
|
||||||
<StyledValuesSpan>
|
<StyledValuesSpan ref={elementRef}>
|
||||||
{constraint?.values
|
{constraint?.values
|
||||||
?.map(value => value)
|
?.map(value => value)
|
||||||
.join(', ')}
|
.join(', ')}
|
||||||
</StyledValuesSpan>
|
</StyledValuesSpan>
|
||||||
<p
|
<ConditionallyRender
|
||||||
className={classnames(
|
condition={shouldBeExpandable}
|
||||||
styles.headerValuesExpand,
|
show={
|
||||||
'valuesExpandLabel'
|
<p
|
||||||
)}
|
className={classnames(
|
||||||
>
|
styles.headerValuesExpand,
|
||||||
Expand to view all ({constraint?.values?.length}
|
'valuesExpandLabel'
|
||||||
)
|
)}
|
||||||
</p>
|
>
|
||||||
|
Expand to view all (
|
||||||
|
{constraint?.values?.length})
|
||||||
|
</p>
|
||||||
|
}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
Loading…
Reference in New Issue
Block a user