generated from cloudoperators/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ui): migrate InputGroup to Typescript (#569)
* chore(ui): convert inputgroup to ts * chore(ui): remove native select from tests and stories, add deprecated code for .js files * chore(ui): revert Button * chore(ui): fix deprecated tests * chore(ui): remove deprecated component and make suggested changes * chore(ui): make imports more explicit * chore(ui): suggested class name changes
- Loading branch information
1 parent
3ee03d3
commit 3e225b9
Showing
9 changed files
with
236 additions
and
212 deletions.
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,5 @@ | ||
--- | ||
"@cloudoperators/juno-ui-components": minor | ||
--- | ||
|
||
Migrate the InputGroup component to TypeScript |
43 changes: 0 additions & 43 deletions
43
packages/ui-components/src/components/InputGroup/InputGroup.component.js
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
packages/ui-components/src/components/InputGroup/InputGroup.component.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,85 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Juno contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { ReactNode, isValidElement } from "react" | ||
import { Stack } from "../Stack/Stack.component" | ||
import "./input-group.scss" | ||
|
||
type VariantTypes = "default" | "primary" | "primary-danger" | "subdued" | ||
|
||
export interface InputGroupProps { | ||
/** | ||
* The children to render within the InputGroup. | ||
* This can be any React node or a collection of React nodes such as Buttons, TextInput, and Select elements. | ||
*/ | ||
children?: React.ReactNode | ||
/** | ||
* Additional CSS class name(s) to apply to the InputGroup for custom styling. | ||
*/ | ||
className?: string | ||
/** | ||
* The variant style to apply to the group and its children. | ||
*/ | ||
variant?: VariantTypes | ||
/** | ||
* If true, all elements within the InputGroup will be disabled. | ||
* Individual elements can override this setting if needed. | ||
*/ | ||
disabled?: boolean | ||
} | ||
|
||
type InheritedProps = { variant?: VariantTypes; disabled?: boolean } | ||
|
||
/** | ||
* Clones a child element with inherited variant and disabled props. | ||
* Individual child props will override inherited ones if specified. | ||
* | ||
* @returns A cloned child element with the variant and disabled props applied. | ||
*/ | ||
const cloneElementWithInheritedProps = ( | ||
child: ReactNode, | ||
parentVariant: VariantTypes, | ||
parentDisabled: boolean | ||
): ReactNode => { | ||
if (!isValidElement<InheritedProps>(child)) return child | ||
|
||
const combinedProps = { | ||
variant: child.props.variant || parentVariant, | ||
disabled: child.props.disabled || parentDisabled, | ||
} | ||
return React.cloneElement(child, combinedProps) | ||
} | ||
|
||
const getClassNames = (baseClassName: string, variant: VariantTypes, disabled: boolean): string => { | ||
return ` | ||
${baseClassName}-${variant} | ||
${disabled ? `${baseClassName}-disabled` : ""} | ||
` | ||
} | ||
|
||
/** | ||
* InputGroup is a component used to visually group related elements such as | ||
* Buttons, TextInput, and Select elements, providing a cohesive styling approach. | ||
*/ | ||
export const InputGroup: React.FC<InputGroupProps> = ({ | ||
children = null, | ||
className = "", | ||
variant = "default", | ||
disabled = false, | ||
...props | ||
}) => { | ||
// Clone each child element with inherited variant and disabled props | ||
const modifiedChildren = React.Children.map(children, (child) => | ||
cloneElementWithInheritedProps(child, variant, disabled) | ||
) | ||
|
||
const inputGroupClassName = getClassNames("juno-input-group", variant, disabled) | ||
|
||
return ( | ||
<Stack className={`juno-input-group ${inputGroupClassName} ${className}`} {...props}> | ||
{modifiedChildren} | ||
</Stack> | ||
) | ||
} |
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
102 changes: 0 additions & 102 deletions
102
packages/ui-components/src/components/InputGroup/InputGroup.test.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.