-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Revert "refactor: update toggle component and related fields to use UT_SWITCH type" #26520
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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
19 changes: 19 additions & 0 deletions
19
openmetadata-ui/src/main/resources/ui/src/components/form/MUISwitch/MUISwitch.interface.ts
This file contains hidden or 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,19 @@ | ||
| /* | ||
| * Copyright 2025 Collate. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { SwitchProps } from '@mui/material'; | ||
|
|
||
| export interface MUISwitchProps extends Omit<SwitchProps, 'onChange'> { | ||
| checked?: boolean; | ||
| onChange?: (checked: boolean) => void; | ||
| label?: string; | ||
| } |
134 changes: 134 additions & 0 deletions
134
openmetadata-ui/src/main/resources/ui/src/components/form/MUISwitch/MUISwitch.test.tsx
This file contains hidden or 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,134 @@ | ||
| /* | ||
| * Copyright 2025 Collate. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { fireEvent, render, screen } from '@testing-library/react'; | ||
| import MUISwitch from './MUISwitch'; | ||
|
|
||
| describe('MUISwitch Component', () => { | ||
| it('should render the switch component', () => { | ||
| render(<MUISwitch />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| expect(switchElement).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render with unchecked state by default', () => { | ||
| render(<MUISwitch />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| expect(switchElement).not.toBeChecked(); | ||
| }); | ||
|
|
||
| it('should render with checked state when checked prop is true', () => { | ||
| render(<MUISwitch checked />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| expect(switchElement).toBeChecked(); | ||
| }); | ||
|
|
||
| it('should render label when provided', () => { | ||
| const labelText = 'Enable Feature'; | ||
|
|
||
| render(<MUISwitch label={labelText} />); | ||
|
|
||
| expect(screen.getByText(labelText)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should not render label when not provided', () => { | ||
| render(<MUISwitch />); | ||
|
|
||
| expect(screen.queryByText('Enable Feature')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should call onChange with true when switch is toggled on', () => { | ||
| const mockOnChange = jest.fn(); | ||
|
|
||
| render(<MUISwitch checked={false} onChange={mockOnChange} />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| fireEvent.click(switchElement); | ||
|
|
||
| expect(mockOnChange).toHaveBeenCalledTimes(1); | ||
| expect(mockOnChange).toHaveBeenCalledWith(true); | ||
| }); | ||
|
|
||
| it('should call onChange with false when switch is toggled off', () => { | ||
| const mockOnChange = jest.fn(); | ||
|
|
||
| render(<MUISwitch checked onChange={mockOnChange} />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| fireEvent.click(switchElement); | ||
|
|
||
| expect(mockOnChange).toHaveBeenCalledTimes(1); | ||
| expect(mockOnChange).toHaveBeenCalledWith(false); | ||
| }); | ||
|
|
||
| it('should not throw error when onChange is not provided', () => { | ||
| render(<MUISwitch />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| expect(() => fireEvent.click(switchElement)).not.toThrow(); | ||
| }); | ||
|
|
||
| it('should apply disabled attribute when disabled prop is true', () => { | ||
| render(<MUISwitch disabled />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| expect(switchElement).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('should not call onChange when disabled', () => { | ||
| const mockOnChange = jest.fn(); | ||
|
|
||
| render(<MUISwitch disabled onChange={mockOnChange} />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| fireEvent.click(switchElement); | ||
|
|
||
| expect(mockOnChange).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should pass additional props to the underlying Switch component', () => { | ||
| render(<MUISwitch data-testid="custom-switch" size="small" />); | ||
|
|
||
| expect(screen.getByTestId('custom-switch')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render with both label and checked state', () => { | ||
| const labelText = 'Toggle Me'; | ||
|
|
||
| render(<MUISwitch checked label={labelText} />); | ||
|
|
||
| const switchElement = screen.getByRole('switch'); | ||
|
|
||
| expect(switchElement).toBeChecked(); | ||
|
|
||
| expect(screen.getByText(labelText)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should support memoization without breaking rendering', () => { | ||
| const { rerender } = render(<MUISwitch />); | ||
|
|
||
| rerender(<MUISwitch />); | ||
|
|
||
| expect(screen.getByRole('switch')).toBeInTheDocument(); | ||
| }); | ||
| }); |
52 changes: 52 additions & 0 deletions
52
openmetadata-ui/src/main/resources/ui/src/components/form/MUISwitch/MUISwitch.tsx
This file contains hidden or 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,52 @@ | ||
| /* | ||
| * Copyright 2025 Collate. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { Box, Switch as MuiSwitch, SxProps, Theme } from '@mui/material'; | ||
| import { FC, memo, useCallback } from 'react'; | ||
| import MUIFormItemLabel from '../../common/MUIFormItemLabel/MUIFormItemLabel'; | ||
| import { MUISwitchProps } from './MUISwitch.interface'; | ||
|
|
||
| const LABEL_STYLES: SxProps<Theme> = { | ||
| color: (theme) => theme.palette.grey[700], | ||
| fontWeight: (theme) => theme.typography.subtitle2.fontWeight, | ||
| }; | ||
|
|
||
| const MUISwitch: FC<MUISwitchProps> = ({ | ||
| checked = false, | ||
| onChange, | ||
| label, | ||
| disabled = false, | ||
| ...props | ||
| }) => { | ||
| const handleChange = useCallback( | ||
| (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| if (!disabled) { | ||
| onChange?.(event.target.checked); | ||
| } | ||
| }, | ||
| [onChange, disabled] | ||
| ); | ||
|
|
||
| return ( | ||
| <Box sx={{ display: 'flex', gap: 2, alignItems: 'center' }}> | ||
| <MuiSwitch | ||
| checked={checked} | ||
| disabled={disabled} | ||
| onChange={handleChange} | ||
| {...props} | ||
| /> | ||
| {label && <MUIFormItemLabel label={label} labelSx={LABEL_STYLES} />} | ||
| </Box> | ||
| ); | ||
| }; | ||
|
|
||
| export default memo(MUISwitch); |
14 changes: 14 additions & 0 deletions
14
openmetadata-ui/src/main/resources/ui/src/components/form/MUISwitch/index.ts
This file contains hidden or 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,14 @@ | ||
| /* | ||
| * Copyright 2025 Collate. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| export { default } from './MUISwitch'; | ||
| export type { MUISwitchProps } from './MUISwitch.interface'; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
In
TagsForm.tsxat line 215-218,disabledFieldis spread and onlylabelis translated (label: t(disabledField.label)), butmuiLabelremains as the raw i18n key string'label.disable-tag'. Since the field type isSWITCH_MUI,getFieldinformUtils.tsxusesmuiLabel(notlabel) to render the switch label. This means the disabled toggle will display the literal string'label.disable-tag'instead of the translated text.Similarly,
mutuallyExclusiveField(line 238-241) translatesmuiLabelbut leaveslabelas the raw key — less impactful sinceSWITCH_MUIusesmuiLabel, but still inconsistent.Suggested fix:
Was this helpful? React with 👍 / 👎 | Reply
gitar fixto apply this suggestion