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

fix(InlineEdit): handle required property in InlineEdit #5478

Merged
merged 1 commit into from
Dec 19, 2024
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
5 changes: 5 additions & 0 deletions .changeset/spotty-ads-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@talend/design-system': patch
---

InlineEdit should take into account the required property to not submit the input
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,35 @@ describe('InlineEditing', () => {

expect(screen.getByTestId('my-prefix.inlineediting.button.edit')).toBeInTheDocument();
});

it('should not allow to submit required input', async () => {
const onEdit = jest.fn();
render(
<main>
<InlineEditing
label="Edit the value"
placeholder="What is your Lorem Ipsum?"
defaultValue=""
onEdit={onEdit}
required
/>
</main>,
);

fireEvent.click(screen.getByTestId('inlineediting.button.edit'));

expect(
screen.getByRole('button', {
name: /submit/i,
}),
).toBeDisabled();

fireEvent.keyDown(
screen.getByRole('textbox', {
name: /edit the value\*/i,
}),
'Enter',
);
expect(onEdit).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ const InlineEditingPrimitive = forwardRef(

// eslint-disable-next-line react-hooks/exhaustive-deps
const getValue = () => (onChangeValue ? value : internalValue);
const inputIsValid = () => {
return !required || !!getValue();
};

const toggleEditionMode = (isEditionMode: boolean) => {
editionModeControl.onChange(isEditionMode);
Expand All @@ -156,6 +159,10 @@ const InlineEditingPrimitive = forwardRef(
const handleSubmit = (event: OnEditEvent) => {
event.stopPropagation();

if (!inputIsValid()) {
return;
}

if (onEdit) {
onEdit(event, getValue() || '');
}
Expand Down Expand Up @@ -273,6 +280,7 @@ const InlineEditingPrimitive = forwardRef(
</ButtonIcon>
<ButtonIcon
onClick={handleSubmit}
disabled={!inputIsValid()}
icon="check-filled"
data-test={`${dataTest ? `${dataTest}.` : ''}inlineediting.button.submit`}
data-testid={`${dataTestId ? `${dataTestId}.` : ''}inlineediting.button.submit`}
Expand Down
12 changes: 12 additions & 0 deletions packages/design-system/src/stories/form/InlineEditing.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ export const Default = {
/>
),
};
export const IsRequired = {
render: (props: Story) => (
<InlineEditing.Text
placeholder="Input a crawler name"
label="Crawler name"
required
defaultValue=""
onEdit={action('onEdit')}
{...props}
/>
),
};
export const WithEllipsis = {
render: (props: Story) => (
<InlineEditing.Text
Expand Down
Loading