-
Notifications
You must be signed in to change notification settings - Fork 924
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
[Workspace]Add name and description characters limitation #7656
Merged
ruanyl
merged 7 commits into
opensearch-project:main
from
wanglam:add-characters-limit-for-workspace-name-and-description
Aug 15, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5294c24
Add name and description characters limitation
wanglam bf9b64f
Changeset file for PR #7656 created/updated
opensearch-changeset-bot[bot] 0cf2fd8
Add workspace name and description limitation in server side
wanglam b68bb41
Add unit tests for workspace name and description field
wanglam 6c64b17
Update test case name
wanglam 696ba2f
Allow input text when exceed max length
wanglam d53954d
Merge branch 'main' into add-characters-limit-for-workspace-name-and-…
wanglam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
feat: | ||
- [Workspace]Add name and description characters limitation ([#7656](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7656)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ns/workspace/public/components/workspace_form/fields/workspace_description_field.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
|
||
import { MAX_WORKSPACE_DESCRIPTION_LENGTH } from '../../../../common/constants'; | ||
import { WorkspaceDescriptionField } from './workspace_description_field'; | ||
|
||
describe('<WorkspaceDescriptionField />', () => { | ||
it('should call onChange when the new value is within MAX_WORKSPACE_DESCRIPTION_LENGTH', () => { | ||
const onChangeMock = jest.fn(); | ||
const value = 'test'; | ||
|
||
render(<WorkspaceDescriptionField value={value} onChange={onChangeMock} />); | ||
|
||
const textarea = screen.getByPlaceholderText('Describe the workspace'); | ||
fireEvent.change(textarea, { target: { value: 'new value' } }); | ||
|
||
expect(onChangeMock).toHaveBeenCalledWith('new value'); | ||
}); | ||
|
||
it('should not call onChange when the new value exceeds MAX_WORKSPACE_DESCRIPTION_LENGTH', () => { | ||
const onChangeMock = jest.fn(); | ||
const value = 'a'.repeat(MAX_WORKSPACE_DESCRIPTION_LENGTH); | ||
|
||
render(<WorkspaceDescriptionField value={value} onChange={onChangeMock} />); | ||
|
||
const textarea = screen.getByPlaceholderText('Describe the workspace'); | ||
fireEvent.change(textarea, { | ||
target: { value: 'a'.repeat(MAX_WORKSPACE_DESCRIPTION_LENGTH + 1) }, | ||
}); | ||
|
||
expect(onChangeMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render the correct number of characters left when value is empty', () => { | ||
render(<WorkspaceDescriptionField value={undefined} onChange={jest.fn()} />); | ||
|
||
const helpText = screen.getByText( | ||
new RegExp(`${MAX_WORKSPACE_DESCRIPTION_LENGTH}.+characters left\.`) | ||
); | ||
expect(helpText).toBeInTheDocument(); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
...plugins/workspace/public/components/workspace_form/fields/workspace_description_field.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { EuiCompressedFormRow, EuiCompressedTextArea } from '@elastic/eui'; | ||
import { i18n } from '@osd/i18n'; | ||
import React, { useCallback } from 'react'; | ||
|
||
import { MAX_WORKSPACE_DESCRIPTION_LENGTH } from '../../../../common/constants'; | ||
|
||
export interface WorkspaceDescriptionFieldProps { | ||
value?: string; | ||
onChange: (newValue: string) => void; | ||
error?: string; | ||
readOnly?: boolean; | ||
} | ||
|
||
export const WorkspaceDescriptionField = ({ | ||
value, | ||
error, | ||
readOnly, | ||
onChange, | ||
}: WorkspaceDescriptionFieldProps) => { | ||
const handleChange = useCallback( | ||
(e) => { | ||
const newValue = e.currentTarget.value; | ||
if (newValue.length <= MAX_WORKSPACE_DESCRIPTION_LENGTH) { | ||
onChange(newValue); | ||
} | ||
}, | ||
[onChange] | ||
); | ||
|
||
return ( | ||
<EuiCompressedFormRow | ||
label={ | ||
<> | ||
Description - <i>optional</i> | ||
</> | ||
} | ||
isInvalid={!!error} | ||
error={error} | ||
helpText={<>{MAX_WORKSPACE_DESCRIPTION_LENGTH - (value?.length ?? 0)} characters left.</>} | ||
> | ||
<EuiCompressedTextArea | ||
value={value} | ||
onChange={handleChange} | ||
data-test-subj="workspaceForm-workspaceDetails-descriptionInputText" | ||
rows={4} | ||
placeholder={i18n.translate('workspace.form.workspaceDetails.description.placeholder', { | ||
defaultMessage: 'Describe the workspace', | ||
})} | ||
readOnly={readOnly} | ||
maxLength={MAX_WORKSPACE_DESCRIPTION_LENGTH} | ||
/> | ||
</EuiCompressedFormRow> | ||
); | ||
}; |
45 changes: 45 additions & 0 deletions
45
src/plugins/workspace/public/components/workspace_form/fields/workspace_name_field.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
|
||
import { MAX_WORKSPACE_NAME_LENGTH } from '../../../../common/constants'; | ||
import { WorkspaceNameField } from './workspace_name_field'; | ||
|
||
describe('<WorkspaceNameField />', () => { | ||
it('should call onChange when the new value is within MAX_WORKSPACE_NAME_LENGTH', () => { | ||
const onChangeMock = jest.fn(); | ||
const value = 'test'; | ||
|
||
render(<WorkspaceNameField value={value} onChange={onChangeMock} />); | ||
|
||
const input = screen.getByPlaceholderText('Enter a name'); | ||
fireEvent.change(input, { target: { value: 'new value' } }); | ||
|
||
expect(onChangeMock).toHaveBeenCalledWith('new value'); | ||
}); | ||
|
||
it('should not call onChange when the new value exceeds MAX_WORKSPACE_NAME_LENGTH', () => { | ||
const onChangeMock = jest.fn(); | ||
const value = 'a'.repeat(MAX_WORKSPACE_NAME_LENGTH); | ||
|
||
render(<WorkspaceNameField value={value} onChange={onChangeMock} />); | ||
|
||
const input = screen.getByPlaceholderText('Enter a name'); | ||
fireEvent.change(input, { target: { value: 'a'.repeat(MAX_WORKSPACE_NAME_LENGTH + 1) } }); | ||
|
||
expect(onChangeMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render the correct number of characters left when value is empty', () => { | ||
render(<WorkspaceNameField value={undefined} onChange={jest.fn()} />); | ||
|
||
const helpText = screen.getByText( | ||
new RegExp(`${MAX_WORKSPACE_NAME_LENGTH}.+characters left\.`) | ||
); | ||
expect(helpText).toBeInTheDocument(); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
src/plugins/workspace/public/components/workspace_form/fields/workspace_name_field.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { EuiCompressedFieldText, EuiCompressedFormRow } from '@elastic/eui'; | ||
import { i18n } from '@osd/i18n'; | ||
import React, { useCallback } from 'react'; | ||
|
||
import { MAX_WORKSPACE_NAME_LENGTH } from '../../../../common/constants'; | ||
|
||
export interface WorkspaceNameFieldProps { | ||
value?: string; | ||
onChange: (newValue: string) => void; | ||
error?: string; | ||
readOnly?: boolean; | ||
} | ||
|
||
export const WorkspaceNameField = ({ | ||
value, | ||
error, | ||
readOnly, | ||
onChange, | ||
}: WorkspaceNameFieldProps) => { | ||
const handleChange = useCallback( | ||
(e) => { | ||
const newValue = e.currentTarget.value; | ||
if (newValue.length <= MAX_WORKSPACE_NAME_LENGTH) { | ||
onChange(newValue); | ||
} | ||
}, | ||
[onChange] | ||
); | ||
|
||
return ( | ||
<EuiCompressedFormRow | ||
label={i18n.translate('workspace.form.workspaceDetails.name.label', { | ||
defaultMessage: 'Name', | ||
})} | ||
helpText={ | ||
<> | ||
{MAX_WORKSPACE_NAME_LENGTH - (value?.length ?? 0)} characters left. <br /> | ||
{i18n.translate('workspace.form.workspaceDetails.name.helpText', { | ||
defaultMessage: | ||
'Use a unique name for the workspace. Valid characters are a-z, A-Z, 0-9, (), [], _ (underscore), - (hyphen) and (space).', | ||
})} | ||
</> | ||
} | ||
isInvalid={!!error} | ||
error={error} | ||
> | ||
<EuiCompressedFieldText | ||
value={value} | ||
onChange={handleChange} | ||
readOnly={readOnly} | ||
data-test-subj="workspaceForm-workspaceDetails-nameInputText" | ||
placeholder={i18n.translate('workspace.form.workspaceDetails.name.placeholder', { | ||
defaultMessage: 'Enter a name', | ||
})} | ||
maxLength={MAX_WORKSPACE_NAME_LENGTH} | ||
/> | ||
</EuiCompressedFormRow> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we allow user to keep editing but display error message when exceed limit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to this. Can be discuss with designer about that behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated this part according the latest feedback. Could you help me take a look?