Skip to content

Commit

Permalink
fix: simplyfy html structure for text highlight (#1187)
Browse files Browse the repository at this point in the history
* fix: simplyfy html structure for text highlight

move text highlight to a separate level of spans inside the labelText
remove the need check the text for direction (dir="auto" works)
makes it easier to style and less affected by other styling

* fix: hide correct end of text for text overflow
  • Loading branch information
T-Wizard committed Mar 24, 2023
1 parent b13e542 commit 64a3b20
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import Histogram from './components/Histogram';
import Frequency from './components/Frequency';
import ItemGrid from './components/ItemGrid';
import getCellFromPages from './helpers/get-cell-from-pages';
import rtlUtil from '../../../../utils/rtl-util';

function RowColumn({ index, rowIndex, columnIndex, style, data }) {
const {
Expand Down Expand Up @@ -137,7 +136,6 @@ function RowColumn({ index, rowIndex, columnIndex, style, data }) {
const isGridCol = dataLayout === 'grid' && layoutOrder === 'column';

const label = cell?.qText ?? '';
const textDirection = rtlUtil.detectTextDirection(label);
// Search highlights. Split up labelText span into several and add the highlighted class to matching sub-strings.

let labels;
Expand Down Expand Up @@ -232,7 +230,6 @@ function RowColumn({ index, rowIndex, columnIndex, style, data }) {
isGridCol={isGridCol}
isSingleSelect={isSingleSelect}
valueTextAlign={valueTextAlign}
textDirection={textDirection}
/>
) : (
<Field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,12 @@ describe('<ListBoxRowColumn />', () => {
);
const testInstance = testRenderer.root;
const types = testInstance.findAllByType(Typography);
expect(types[0].props.children.props.children).toBe('nebula.js');
expect(types[0].props.className.includes('highlighted')).toBe(true);
expect(types[1].props.children.props.children).toBe(' ftw');
expect(types).toHaveLength(1);
const spans = types[0].props.children.props.children;
expect(spans).toHaveLength(2);
expect(spans[0].props.children).toBe('nebula.js');
expect(spans[0].props.className.includes('highlighted')).toBe(true);
expect(spans[1].props.children).toBe(' ftw');
await testRenderer.unmount();
});

Expand Down Expand Up @@ -662,9 +665,12 @@ describe('<ListBoxRowColumn />', () => {
);
const testInstance = testRenderer.root;
const types = testInstance.findAllByType(Typography);
expect(types[0].props.children.props.children).toBe('nebula.js ');
expect(types[1].props.children.props.children).toBe('ftw');
expect(types[1].props.className.includes('highlighted')).toBe(true);
expect(types).toHaveLength(1);
const spans = types[0].props.children.props.children;
expect(spans).toHaveLength(2);
expect(spans[0].props.children).toBe('nebula.js ');
expect(spans[1].props.children).toBe('ftw');
expect(spans[1].props.className.includes('highlighted')).toBe(true);
// TODO: MUIv5 - no idea why this breaks
// const hits = testInstance.findAllByProps({ className: 'RowColumn-highlighted' });
// expect(hits).to.have.length(2);
Expand Down Expand Up @@ -711,10 +717,13 @@ describe('<ListBoxRowColumn />', () => {
);
const testInstance = testRenderer.root;
const types = testInstance.findAllByType(Typography);
expect(types[0].props.children.props.children).toBe('nebula.js ftw ');
expect(types[1].props.children.props.children).toBe('yeah');
expect(types[1].props.className.includes('RowColumn-highlighted')).toBe(true);
expect(types[2].props.children.props.children).toBe(' buddy');
expect(types).toHaveLength(1);
const spans = types[0].props.children.props.children;
expect(spans).toHaveLength(3);
expect(spans[0].props.children).toBe('nebula.js ftw ');
expect(spans[1].props.children).toBe('yeah');
expect(spans[1].props.className.includes('RowColumn-highlighted')).toBe(true);
expect(spans[2].props.children).toBe(' buddy');
await testRenderer.unmount();
});

Expand Down Expand Up @@ -799,10 +808,13 @@ describe('<ListBoxRowColumn />', () => {
// const cells = testInstance.findAllByProps({ className: 'RowColumn-highlighted' });
// expect(cells).to.have.length(2);
const types = testInstance.findAllByType(Typography);
expect(types[1].props.children.props.children).toBe('nebula.js ftw ');
expect(types[2].props.children.props.children).toBe('yeah');
expect(types[2].props.className.includes('RowColumn-highlighted')).toBe(true);
expect(types[3].props.children.props.children).toBe(' buddy');
expect(types).toHaveLength(2);
const spans = types[1].props.children.props.children;
expect(spans).toHaveLength(3);
expect(spans[0].props.children).toBe('nebula.js ftw ');
expect(spans[1].props.children).toBe('yeah');
expect(spans[1].props.className.includes('RowColumn-highlighted')).toBe(true);
expect(spans[2].props.children).toBe(' buddy');
await testRenderer.unmount();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function CheckboxField({
isGridCol,
showGray,
isSingleSelect,
highlighted,
checkboxes,
valueTextAlign,
}) {
Expand Down Expand Up @@ -46,7 +45,6 @@ function CheckboxField({
<LabelTag
label={label}
color={color}
highlighted={highlighted}
dense={dense}
showGray={showGray}
checkboxes={checkboxes}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function Field({
isGridCol,
showGray,
isSingleSelect,
highlighted,
checkboxes,
valueTextAlign,
}) {
Expand All @@ -29,15 +28,13 @@ function Field({
isGridCol={isGridCol}
showGray={showGray}
isSingleSelect={isSingleSelect}
highlighted={highlighted}
checkboxes={checkboxes}
valueTextAlign={valueTextAlign}
/>
) : (
<ValueField
label={label}
color={color}
highlighted={false}
dense={dense}
showGray
checkboxes={checkboxes}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import React from 'react';
import CheckboxField from './CheckboxField';
import ValueField from './ValueField';
import classes from '../helpers/classes';

function LabelsWithRanges({ labels, dense, showGray, checkboxes, textDirection }) {
return (
<div dir={textDirection}>
{labels.map(([label, highlighted], index) => {
const key = `${index}`;
return (
<ValueField
label={label}
key={key}
highlighted={highlighted}
dense={dense}
showGray={showGray}
checkboxes={checkboxes}
/>
);
})}
</div>
);
function LabelsWithRanges({ labels, dense, showGray, checkboxes }) {
const text = labels.map(([label, highlighted], index) => (
<span id={index} className={highlighted ? classes.highlighted : ''}>
{label}
</span>
));
return <ValueField label={text} dense={dense} showGray={showGray} checkboxes={checkboxes} />;
}

function FieldWithRanges({
Expand All @@ -35,17 +25,8 @@ function FieldWithRanges({
isGridCol,
isSingleSelect,
valueTextAlign,
textDirection,
}) {
const LWR = (
<LabelsWithRanges
labels={labels}
dense={dense}
showGray={showGray}
checkboxes={checkboxes}
textDirection={textDirection}
/>
);
const LWR = <LabelsWithRanges labels={labels} dense={dense} showGray={showGray} checkboxes={checkboxes} />;
return checkboxes ? (
<CheckboxField
onChange={onChange}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React from 'react';
import ValueField from './ValueField';

function LabelTag({ label, color, highlighted, dense, showGray, checkboxes, cell, valueTextAlign }) {
function LabelTag({ label, color, dense, showGray, checkboxes, cell, valueTextAlign }) {
if (typeof label === 'string') {
return (
<ValueField
label={label}
color={color}
highlighted={highlighted}
dense={dense}
showGray={showGray}
checkboxes={checkboxes}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,10 @@ const RowColRoot = styled('div', {
fontSize: 12,
},

// Highlight is added to labelText spans, which are created as siblings to original labelText,
// Highlight is added to labelText spans, which are created as children to original labelText,
// when a search string is matched.
[`& .${classes.highlighted}`]: {
overflow: 'visible',
width: '100%',
'& > span': {
width: '100%',
backgroundColor: '#FFC72A',
},
backgroundColor: '#FFC72A',
},

// Checkbox and label container.
Expand All @@ -118,14 +113,6 @@ const RowColRoot = styled('div', {
},
},

[`& .${classes.checkboxLabel} .${classes.labelText}`]: {
width: 'auto',
},

[`& .${classes.checkboxLabel} .${classes.labelText}.${classes.highlighted}`]: {
width: 'auto',
},

// The icons container holding tick and lock, shown inside fields.
[`& .${classes.icon}`]: {
display: 'flex',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@ import { joinClassNames } from '../helpers/operations';
import classes from '../helpers/classes';
import { excludedOrAlternative } from '../helpers/cell-states';

function ValueField({ label, color, highlighted = false, dense, showGray = true, checkboxes, cell, valueTextAlign }) {
function ValueField({ label, color, dense, showGray = true, checkboxes, cell, valueTextAlign }) {
return (
<Typography
component="span"
variant="body2"
className={joinClassNames([
classes.labelText,
highlighted && classes.highlighted,
dense && classes.labelDense,
showGray && excludedOrAlternative({ cell, checkboxes }) && classes.excludedTextWithCheckbox,
])}
color={color}
align={valueTextAlign}
dir="auto"
>
<span dir="auto" style={{ whiteSpace: 'pre' }}>
{label}
</span>
<span style={{ whiteSpace: 'pre' }}>{label}</span>
</Typography>
);
}
Expand Down
57 changes: 0 additions & 57 deletions apis/nucleus/src/utils/rtl-util.js

This file was deleted.

0 comments on commit 64a3b20

Please sign in to comment.