Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@leav/ui):Open and close the record's value details depending on where the user clicks #712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/data-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"antd": "5.15.3",
"apollo-cache-inmemory": "1.6.6",
"apollo-upload-client": "14.1.3",
"aristid-ds": "11.0.0-6ec698b",
"aristid-ds": "12.0.0-d8287e7",
"dayjs": "1.11.10",
"graphql": "15.0.0",
"graphql-tag": "2.12.6",
Expand Down
2 changes: 1 addition & 1 deletion apps/login/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@ant-design/icons": "5.2.6",
"@leav/ui": "workspace:libs/ui",
"antd": "5.15.3",
"aristid-ds": "11.0.0-6ec698b",
"aristid-ds": "12.0.0-d8287e7",
"i18next": "22.5.0",
"i18next-browser-languagedetector": "7.0.2",
"i18next-http-backend": "2.1.1",
Expand Down
2 changes: 1 addition & 1 deletion apps/portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@leav/ui": "workspace:libs/ui",
"@leav/utils": "workspace:libs/utils",
"antd": "5.15.3",
"aristid-ds": "11.0.0-6ec698b",
"aristid-ds": "12.0.0-d8287e7",
"cross-fetch": "3.1.5",
"graphql-ws": "5.12.0",
"i18next": "22.5.0",
Expand Down
2 changes: 1 addition & 1 deletion libs/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@ant-design/icons": ">=5.2",
"@apollo/client": ">=3.8.1",
"antd": "5.15.3",
"aristid-ds": "11.0.0-6ec698b",
"aristid-ds": "12.0.0-d8287e7",
"dayjs": "^1.11.10",
"i18next": "22.5",
"react": "18.2.0",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import {
} from '../../_types';
import StandardField from './StandardField';
import {AntForm} from 'aristid-ds';
import * as useEditRecordReducer from '_ui/components/RecordEdition/editRecordReducer/useEditRecordReducer';
import {
EditRecordReducerActionsTypes,
initialState
} from '_ui/components/RecordEdition/editRecordReducer/editRecordReducer';
import {EDIT_RECORD_SIDEBAR_ID} from '_ui/constants';
import {RecordFormElementsValueStandardValue} from '_ui/hooks/useGetRecordForm';

describe('StandardField', () => {
Expand Down Expand Up @@ -65,7 +71,102 @@ describe('StandardField', () => {
onDeleteMultipleValues: mockHandleMultipleValues
};

beforeEach(() => jest.clearAllMocks());
const mockEditRecordDispatch = jest.fn();
const mockEditRecordInitialState = jest.fn();

jest.spyOn(useEditRecordReducer, 'useEditRecordReducer').mockImplementation(() => ({
state: mockEditRecordInitialState(),
dispatch: mockEditRecordDispatch
}));
philippechevieux marked this conversation as resolved.
Show resolved Hide resolved

beforeEach(() => {
jest.clearAllMocks();
});

describe('Click outside behavior', () => {
test('Should call useEditRecord dispatch on click on input', async () => {
mockEditRecordInitialState.mockReturnValue(initialState);

render(
<AntForm>
<StandardField element={mockFormElementInput} {...baseProps} />
</AntForm>
);

const textInput = screen.getByRole('textbox');
await userEvent.click(textInput);

expect(mockEditRecordDispatch).toHaveBeenCalledWith({
type: EditRecordReducerActionsTypes.SET_ACTIVE_VALUE,
value: expect.objectContaining({
attribute: mockFormElementInput.attribute,
editingValue: null,
value: null
})
});
});

test('Should call useEditRecord dispatch with null value on click outside focused input', async () => {
mockEditRecordInitialState.mockReturnValue({
...initialState,
activeValue: {
attribute: mockFormElementInput.attribute,
editingValue: null,
value: null
}
});

render(
<AntForm>
<StandardField element={mockFormElementInput} {...baseProps} />
<button>Outside</button>
</AntForm>
);

await userEvent.click(document.body);

expect(mockEditRecordDispatch).toHaveBeenCalledWith({
type: EditRecordReducerActionsTypes.SET_ACTIVE_VALUE,
value: null
});
});

describe('On click on allowed element', () => {
test.each`
property | value
${'id'} | ${EDIT_RECORD_SIDEBAR_ID}
${'id'} | ${'standardfield-' + mockFormElementInput.attribute.id}
${'className'} | ${'ant-popover ant-color-picker'}
${'className'} | ${'ant-picker-dropdown'}
${'className'} | ${'kit-modal-wrapper link-modal'}
philippechevieux marked this conversation as resolved.
Show resolved Hide resolved
`(
'Should not call useEditRecord dispatch on click outside focused input',
async ({property, value}: {property: string; value: string}) => {
mockEditRecordInitialState.mockReturnValue({
...initialState,
activeValue: {
attribute: mockFormElementInput.attribute,
editingValue: null,
value: null
}
});

const allowedElementProps = property === 'id' ? {id: value} : {className: value};

render(
<AntForm>
<StandardField element={mockFormElementInput} {...baseProps} />
<div {...allowedElementProps}>Allowed click</div>
</AntForm>
);

await userEvent.click(screen.getByText('Allowed click'));

expect(mockEditRecordDispatch).not.toHaveBeenCalled();
}
);
});
});

describe('Mono', () => {
const initialValues = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import {useSharedTranslation} from '_ui/hooks/useSharedTranslation';
import {ComputeIndicator} from './ComputeIndicator';
import {getAntdDisplayedValue, getEmptyInitialValue} from '../../antdUtils';
import {GetRecordColumnsValuesRecord, IRecordColumnValueStandard} from '_ui/_queries/records/getRecordColumnsValues';
import {useEditRecordReducer} from '_ui/components/RecordEdition/editRecordReducer/useEditRecordReducer';
import {EditRecordReducerActionsTypes} from '_ui/components/RecordEdition/editRecordReducer/editRecordReducer';
import {EDIT_RECORD_SIDEBAR_ID, STANDARDFIELD_ID_PREFIX} from '_ui/constants';

const Wrapper = styled.div<{$metadataEdit: boolean}>`
margin-bottom: ${props => (props.$metadataEdit ? 0 : '1.5em')};
Expand Down Expand Up @@ -100,6 +103,41 @@ const StandardField: FunctionComponent<
return <ErrorDisplay message={t('record_edition.missing_attribute')} />;
}

const {state, dispatch} = useEditRecordReducer();

useEffect(() => {
const _handleClickOutside = (event: MouseEvent | FocusEvent) => {
if (state.activeValue?.attribute?.id !== attribute.id) {
philippechevieux marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const target = event.target as HTMLElement;

const sideBarSelector = '#' + EDIT_RECORD_SIDEBAR_ID;
const inputWrapperSelector = '#' + STANDARDFIELD_ID_PREFIX + attribute.id;
const colorDropdownSelector = '.ant-popover.ant-color-picker';
const pickerDropdownSelector = '.ant-picker-dropdown';
const richTextModalSelector = '.kit-modal-wrapper.link-modal';

const allowedElementsToKeepValueDetailsDisplayed = target.closest(
`${sideBarSelector}, ${inputWrapperSelector}, ${colorDropdownSelector}, ${pickerDropdownSelector}, ${richTextModalSelector}`
);

if (!allowedElementsToKeepValueDetailsDisplayed) {
dispatch({
type: EditRecordReducerActionsTypes.SET_ACTIVE_VALUE,
value: null
});
}
};

document.addEventListener('mousedown', _handleClickOutside);

return () => {
document.removeEventListener('mousedown', _handleClickOutside);
};
}, [state.activeValue, attribute.id, dispatch]);

const [backendValues, setBackendValues] = useState<RecordFormElementsValueStandardValue[]>(element.values);

const calculatedFlags = computeCalculatedFlags(backendValues);
Expand Down Expand Up @@ -223,6 +261,17 @@ const StandardField: FunctionComponent<
}
};

const setActiveValue = () => {
dispatch({
type: EditRecordReducerActionsTypes.SET_ACTIVE_VALUE,
value: {
value: null, // Value is needed for Tree or Metadata (for now it seems that we don't need it)
editingValue: null, // Set to null because it is only use to compute value.length but it will no longer be used (TO REMOVE IN XSTREAM-1094)
attribute
}
});
};

const isMultipleValues = element.attribute.multiple_values;
const hasValue = isMultipleValues && backendValues.length > 0;
const canAddAnotherValue =
Expand All @@ -249,6 +298,7 @@ const StandardField: FunctionComponent<
return (
<Wrapper $metadataEdit={metadataEdit}>
<KitInputWrapperStyled
id={STANDARDFIELD_ID_PREFIX + attribute.id}
label={label}
required={attribute.required}
disabled={isReadOnly}
Expand All @@ -274,6 +324,7 @@ const StandardField: FunctionComponent<
label={label}
calculatedFlags={calculatedFlags}
inheritedFlags={inheritedFlags}
setActiveValue={setActiveValue}
/>
)}
{attribute.multiple_values && (
Expand Down Expand Up @@ -307,6 +358,7 @@ const StandardField: FunctionComponent<
index === fields.length - 1 && index !== 0
}
removeLastValueOfMultivalues={() => remove(index)}
setActiveValue={setActiveValue}
/>
</StandardFieldValueWrapper>
{fields.length > 1 && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ describe('DSBooleanWrapper', () => {
let user!: ReturnType<typeof userEvent.setup>;
const mockHandleSubmit = jest.fn();
const mockOnChange = jest.fn();
const mockSetActiveValue = jest.fn();

beforeEach(() => {
user = userEvent.setup({});
mockHandleSubmit.mockReset();
mockOnChange.mockReset();
mockSetActiveValue.mockReset();
});

test('Should display boolean value as yes', async () => {
Expand All @@ -68,6 +70,7 @@ describe('DSBooleanWrapper', () => {
inheritedFlags={inheritedFlagsWithoutInheritedValue}
handleSubmit={mockHandleSubmit}
onChange={mockOnChange}
setActiveValue={mockSetActiveValue}
/>
</AntForm.Item>
</AntForm>
Expand All @@ -89,6 +92,7 @@ describe('DSBooleanWrapper', () => {
inheritedFlags={inheritedFlagsWithoutInheritedValue}
handleSubmit={mockHandleSubmit}
onChange={mockOnChange}
setActiveValue={mockSetActiveValue}
/>
</AntForm.Item>
</AntForm>
Expand All @@ -110,6 +114,7 @@ describe('DSBooleanWrapper', () => {
inheritedFlags={inheritedFlagsWithoutInheritedValue}
handleSubmit={mockHandleSubmit}
onChange={mockOnChange}
setActiveValue={mockSetActiveValue}
/>
</AntForm.Item>
</AntForm>
Expand All @@ -132,6 +137,7 @@ describe('DSBooleanWrapper', () => {
inheritedFlags={inheritedFagsWithInheritedValue}
handleSubmit={mockHandleSubmit}
onChange={mockOnChange}
setActiveValue={mockSetActiveValue}
/>
</AntForm.Item>
</AntForm>
Expand All @@ -156,6 +162,7 @@ describe('DSBooleanWrapper', () => {
inheritedFlags={inheritedFlagsWithoutInheritedValue}
handleSubmit={mockHandleSubmit}
onChange={mockOnChange}
setActiveValue={mockSetActiveValue}
/>
</AntForm.Item>
</AntForm>
Expand All @@ -180,6 +187,7 @@ describe('DSBooleanWrapper', () => {
inheritedFlags={inheritedFagsWithInheritedValue}
handleSubmit={mockHandleSubmit}
onChange={mockOnChange}
setActiveValue={mockSetActiveValue}
/>
</AntForm.Item>
</AntForm>
Expand All @@ -204,6 +212,7 @@ describe('DSBooleanWrapper', () => {
inheritedFlags={inheritedFlagsWithoutInheritedValue}
handleSubmit={mockHandleSubmit}
onChange={mockOnChange}
setActiveValue={mockSetActiveValue}
/>
</AntForm.Item>
</AntForm>
Expand All @@ -214,4 +223,29 @@ describe('DSBooleanWrapper', () => {

expect(mockHandleSubmit).toHaveBeenCalledWith(null, mockFormAttribute.id);
});

test('Should call setActiveValue when the switch is clicked', async () => {
render(
<AntForm>
<AntForm.Item>
<DSBooleanWrapper
value={false}
attribute={mockFormAttribute}
readonly={notReadonly}
required={notRequired}
calculatedFlags={calculatedFlagsWithoutCalculatedValue}
inheritedFlags={inheritedFlagsWithoutInheritedValue}
handleSubmit={mockHandleSubmit}
onChange={mockOnChange}
setActiveValue={mockSetActiveValue}
/>
</AntForm.Item>
</AntForm>
);

const switchInput = screen.getByRole('switch');
await user.click(switchInput);

expect(mockSetActiveValue).toHaveBeenCalled();
});
});
Loading
Loading