Skip to content

Commit

Permalink
fix(TDOPS-5510/inlineEdit): add data attributes from props (#5166)
Browse files Browse the repository at this point in the history
* fix(TDOPS-5510/inlineEdit): add data attributes from props

* fix(TDOPS-5510/inlineEdit): add data attributes from props

* fix(TDOPS-5510/inlineEdit): add data attributes from props

* fix(TDOPS-5510/inlineEdit): add data attributes from props
  • Loading branch information
ybaskaran authored Feb 7, 2024
1 parent e034360 commit 52563ff
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 41 deletions.
8 changes: 8 additions & 0 deletions .changeset/fast-seahorses-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@talend/react-faceted-search': patch
'@talend/design-system': patch
'@talend/react-components': patch
'@talend/utils': patch
---

fix(TDOPS-5510/inlineEdit): add data attributes from props
3 changes: 3 additions & 0 deletions packages/components/src/EditableText/InlineForm.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Component } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';

import { getDataAttrFromProps } from '@talend/utils';

import { Action } from '../Actions';
import FocusManager from '../FocusManager';
import getDefaultT from '../translate';
Expand Down Expand Up @@ -142,6 +144,7 @@ class InlineForm extends Component {
disabled={notFilled}
hideLabel
data-feature={feature && `${feature}.submit`}
{...getDataAttrFromProps(this.props)}
/>
</div>
</form>
Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/EditableText/InlineForm.component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,14 @@ describe('InlineForm', () => {
const input = screen.getByRole('textbox');
expect(input).toHaveAttribute('placeholder', placeholder);
});
it('should add data attributes to submit', () => {
const props = {
...defaultProps,
required: false,
'data-tracking': 'test-tracker',
};
render(<InlineForm {...props} />);
const submit = screen.getAllByRole('button')[1];
expect(submit).toHaveAttribute('data-tracking', props['data-tracking']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.mock('@talend/utils', () => {
return {
// we need stable but different uuid (is fixed to 42 by current mock)
randomUUID: () => `mocked-uuid-${i++}`,
getDataAttrFromProps: () => ({ 'data-tracking': 'test-tracker' }),
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { useTranslation } from 'react-i18next';
import classnames from 'classnames';
import { DataAttributes } from 'src/types';

import { getDataAttrFromProps } from '@talend/utils';

import { useId } from '../../../useId';
import { ButtonIcon } from '../../ButtonIcon';
import { I18N_DOMAIN_DESIGN_SYSTEM } from '../../constants';
Expand Down Expand Up @@ -253,6 +255,7 @@ const InlineEditingPrimitive = forwardRef(
icon="check-filled"
data-test={`${dataTest ? `${dataTest}.` : ''}inlineediting.button.submit`}
data-testid={`${dataTestId ? `${dataTestId}.` : ''}inlineediting.button.submit`}
{...getDataAttrFromProps(rest)}
size="XS"
>
{t('INLINE_EDITING_SUBMIT', 'Submit')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ exports[`InlineEditing should render a11y html 1`] = `
class="theme-clickable theme-buttonIcon theme-size_XS"
data-test="inlineediting.button.submit"
data-testid="inlineediting.button.submit"
data-tracking="test-tracker"
tabindex="0"
type="button"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
SkeletonHeading,
StackVertical,
} from '@talend/design-system';
import { getDataAttrFromProps } from '@talend/utils';

import { I18N_DOMAIN_FACETED_SEARCH } from '../../../constants';
import { getApplyDataFeature, getDataAttributesFrom } from '../../../helpers/usage.helpers';
import { getApplyDataFeature } from '../../../helpers/usage.helpers';

import styles from './BadgeCheckboxes.module.scss';

Expand Down Expand Up @@ -164,7 +165,7 @@ const BadgeCheckboxesForm = ({
data-feature={applyDataFeature}
type="submit"
disabled={rest.isLoading}
{...getDataAttributesFrom(rest)}
{...getDataAttrFromProps(rest)}
>
{t('APPLY', { defaultValue: 'Apply' })}
</ButtonPrimary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import PropTypes from 'prop-types';

import { ButtonPrimary, Form } from '@talend/design-system';
import { DatePicker } from '@talend/react-components';
import { getDataAttrFromProps } from '@talend/utils';

import { getApplyDataFeature, getDataAttributesFrom } from '../../../helpers/usage.helpers';
import { getApplyDataFeature } from '../../../helpers/usage.helpers';

import styles from './BadgeDate.module.scss';

Expand Down Expand Up @@ -39,7 +40,7 @@ const BadgeDateForm = ({ id, onChange, onSubmit, value, feature, t, ...rest }) =
<ButtonPrimary
data-feature={applyDataFeature}
type="submit"
{...getDataAttributesFrom(rest)}
{...getDataAttrFromProps(rest)}
>
{t('APPLY', { defaultValue: 'Apply' })}
</ButtonPrimary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
SkeletonHeading,
StackVertical,
} from '@talend/design-system';

import { getDataAttributesFrom } from '../../../helpers/usage.helpers';
import { getDataAttrFromProps } from '@talend/utils';

import styles from './BadgeMenu.module.scss';

Expand Down Expand Up @@ -119,7 +118,7 @@ const BadgeMenuForm = ({
{showSelectedToggleLabel}
</ButtonTertiary>
)}
<ButtonPrimary type="submit" disabled={rest.isLoading} {...getDataAttributesFrom(rest)}>
<ButtonPrimary type="submit" disabled={rest.isLoading} {...getDataAttrFromProps(rest)}>
{t('APPLY', { defaultValue: 'Apply' })}
</ButtonPrimary>
</Form.Buttons>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { useMemo } from 'react';
import PropTypes from 'prop-types';

import { ButtonPrimary, Form } from '@talend/design-system';
import { getDataAttrFromProps } from '@talend/utils';

import { getApplyDataFeature, getDataAttributesFrom } from '../../../helpers/usage.helpers';
import { getApplyDataFeature } from '../../../helpers/usage.helpers';

const BadgeNumberForm = ({ id, onChange, onSubmit, value, feature, t, ...rest }) => {
const applyDataFeature = useMemo(() => getApplyDataFeature(feature), [feature]);
Expand All @@ -22,7 +23,7 @@ const BadgeNumberForm = ({ id, onChange, onSubmit, value, feature, t, ...rest })
<ButtonPrimary
type="submit"
data-feature={applyDataFeature}
{...getDataAttributesFrom(rest)}
{...getDataAttrFromProps(rest)}
>
{t('APPLY', { defaultValue: 'Apply' })}
</ButtonPrimary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
StackVertical,
} from '@talend/design-system';
import { InputDateTimeRangePicker } from '@talend/react-components/lib/DateTimePickers';

import { getDataAttributesFrom } from '../../../helpers/usage.helpers';
import { getDataAttrFromProps } from '@talend/utils';

function getPeriodOptions(t) {
return [
Expand Down Expand Up @@ -101,7 +100,7 @@ const BadgePeriodForm = ({ id, onChange, onSubmit, t, value, ...rest }) => {
onClick={event => onSelectOption(rowItem, event)}
data-testid={`badge-period-form-item-${rowItem.id}`}
data-test={`badge-period-form-item-${rowItem.id}`}
{...getDataAttributesFrom(rowItem)}
{...getDataAttrFromProps(rowItem)}
>
{rowItem.id === 'CUSTOM' ? (
<StackHorizontal gap="0" align="center" justify="spaceBetween">
Expand All @@ -123,7 +122,7 @@ const BadgePeriodForm = ({ id, onChange, onSubmit, t, value, ...rest }) => {
data-testid="badge-period-form-custom-button"
icon="chevron-left"
onClick={goBack}
{...getDataAttributesFrom(rest)}
{...getDataAttrFromProps(rest)}
>
{t('CUSTOM', { defaultValue: 'Custom' })}
</ButtonTertiary>
Expand All @@ -142,15 +141,15 @@ const BadgePeriodForm = ({ id, onChange, onSubmit, t, value, ...rest }) => {
id="reset-button"
disabled={rest.isLoading}
onClick={resetRange}
{...getDataAttributesFrom(rest)}
{...getDataAttrFromProps(rest)}
>
{t('RESET', { defaultValue: 'Reset' })}
</ButtonTertiary>
<ButtonPrimary
id="apply-button"
type="submit"
disabled={rest.isLoading || !!value?.errorMessage}
{...getDataAttributesFrom(rest)}
{...getDataAttrFromProps(rest)}
>
{t('APPLY', { defaultValue: 'Apply' })}
</ButtonPrimary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import PropTypes from 'prop-types';
import { ButtonPrimary, Form, InlineMessageDestructive } from '@talend/design-system';
import Icon from '@talend/react-components/lib/Icon';
import Slider from '@talend/react-components/lib/Slider';
import { getDataAttrFromProps } from '@talend/utils';

import { getApplyDataFeature, getDataAttributesFrom } from '../../../helpers/usage.helpers';
import { getApplyDataFeature } from '../../../helpers/usage.helpers';

import styles from './BadgeSlider.module.scss';

Expand Down Expand Up @@ -140,7 +141,7 @@ const BadgeSliderForm = ({
type="submit"
data-feature={applyDataFeature}
disabled={!!error}
{...getDataAttributesFrom(rest)}
{...getDataAttrFromProps(rest)}
>
{t('APPLY', { defaultValue: 'Apply' })}
</ButtonPrimary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { useMemo } from 'react';
import PropTypes from 'prop-types';

import { ButtonPrimary, Form } from '@talend/design-system';
import { getDataAttrFromProps } from '@talend/utils';

import { getApplyDataFeature, getDataAttributesFrom } from '../../../helpers/usage.helpers';
import { getApplyDataFeature } from '../../../helpers/usage.helpers';

const BadgeTextForm = ({
id,
Expand Down Expand Up @@ -39,7 +40,7 @@ const BadgeTextForm = ({
<ButtonPrimary
type="submit"
data-feature={applyDataFeature}
{...getDataAttributesFrom(rest)}
{...getDataAttrFromProps(rest)}
>
{t('APPLY', { defaultValue: 'Apply' })}
</ButtonPrimary>
Expand Down
6 changes: 0 additions & 6 deletions packages/faceted-search/src/helpers/usage.helpers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { pick } from 'lodash';
import { USAGE_TRACKING_TAGS } from '../constants';

export const getApplyDataFeature = feature => {
const formatedFeature = feature.toLowerCase().replace(' ', '_');
return USAGE_TRACKING_TAGS.BADGE_ADD.replace('#{badgeName}', formatedFeature);
};

export const getDataAttributesFrom = props => {
const dataAttributesKeys = Object.keys(props).filter(objectKey => objectKey.startsWith('data-'));
return pick(props, dataAttributesKeys);
};
16 changes: 1 addition & 15 deletions packages/faceted-search/src/helpers/usage.helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import { getApplyDataFeature, getDataAttributesFrom } from './usage.helpers';
import { getApplyDataFeature } from './usage.helpers';

describe('helpers/usage', () => {
it('should format an apply button data-feature based on formatedKey', () => {
expect(getApplyDataFeature('name')).toEqual('filter.name.add');
expect(getApplyDataFeature('Connection type')).toEqual('filter.connection_type.add');
expect(getApplyDataFeature('UPPERCASE LABEL')).toEqual('filter.uppercase_label.add');
});

it('should get only data attributes from props', () => {
const props = {
key: 'a',
id: 'b',
other: 'c',
'data-test': 'd',
'data-feature': 'e',
};
expect(getDataAttributesFrom(props)).toEqual({
'data-test': 'd',
'data-feature': 'e',
});
});
});
17 changes: 17 additions & 0 deletions packages/utils/src/getDataAttrFromProps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getDataAttrFromProps from './getDataAttrFromProps';

describe('getDataAttrFromProps tests', () => {
it('should get only data attributes from props', () => {
const props = {
key: 'a',
id: 'b',
other: 'c',
'data-test': 'd',
'data-feature': 'e',
};
expect(getDataAttrFromProps(props)).toEqual({
'data-test': 'd',
'data-feature': 'e',
});
});
});
13 changes: 13 additions & 0 deletions packages/utils/src/getDataAttrFromProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { pick } from 'lodash';

/**
* get data attributes from props
* @param {object} props
* @returns {object}
*/
export default function getDataAttrFromProps(props: object) {
const dataAttributesKeys: string[] = Object.keys(props).filter(objectKey =>
objectKey.startsWith('data-'),
);
return pick(props, dataAttributesKeys);
}
3 changes: 2 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as date from './date';
import * as filters from './filters';
import getDataAttrFromProps from './getDataAttrFromProps';
import { randomUUID } from './uuid';
import validation from './validation';

export { date, validation, randomUUID, filters };
export { date, validation, randomUUID, filters, getDataAttrFromProps };

0 comments on commit 52563ff

Please sign in to comment.