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(RadioButton): allow an extended value other than a string #1177

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

GermanVor
Copy link
Contributor

No description provided.

@gravity-ui-bot
Copy link
Contributor

Playwright Test Component is ready.

@gravity-ui-bot
Copy link
Contributor

Preview is ready.

@GermanVor
Copy link
Contributor Author

GermanVor commented Dec 1, 2023

Motivation: to support enums
common example to describe pain:

enum ActiveObject {
    A = 'A',
    B = 'B'
}
...
const [currentObj, setCurrentObj] = useState(ActiveObject.A) // type currentObj === ActiveObject, type setCurrentObj === (v: ActiveObject) => any

...

<RadioButton value={currentObj} onUpdate={setCurrentObj as RadioButtonProps['onUpdate']} /> // you have to use as cause (v: string) => void !== (v: ActiveObject) => any (by default enum not reduced to string)
<ObjectComponent object={currentObj} /> // type ObjectComponent.object === ActiveObject

@GermanVor
Copy link
Contributor Author

may be even extends string is extra

Comment on lines +28 to +30
type RadioButtonComponentType = (<T extends string>(
props: RadioButtonProps<T> & {ref?: React.ForwardedRef<HTMLDivElement>},
) => React.JSX.Element) & {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know how to write like this

<T extends string>React.ForwardRefExoticComponent<
        RadioButtonProps<T> & React.RefAttributes<HTMLDivElement>
>

Ts does not allow

@@ -67,7 +69,7 @@ export function useRadioGroup(props: UseRadioGroupProps): UseRadioGroupResult {

const optionsProps = options.map((option) => ({
name: name || controlId,
value: String(option.value),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not understand why - type option.value === 'string' already

@korvin89 korvin89 changed the title RadioButton Generic feat(RadioButton): allow an extended value other than a string Dec 4, 2023
@GermanVor GermanVor merged commit 77eb5a0 into main Dec 4, 2023
4 checks passed
@GermanVor GermanVor deleted the radio-button-generic branch December 4, 2023 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants