RadioGroup.tsx 1.4 KB
Newer Older
1
import React, {FunctionComponent, PropsWithChildren, createContext, useCallback, useState} from 'react';
2

3
import type {WithStyled} from '~/utils/style';
4
import styled from 'styled-components';
5 6 7 8 9 10 11 12 13 14 15

const Wrapper = styled.div`
    display: inline-flex;

    > * {
        flex-shrink: 0;
        align-items: flex-start;
    }
`;

// eslint-disable-next-line @typescript-eslint/no-empty-function
16 17
export const EventContext = createContext<(<V extends unknown>(value: V) => unknown) | undefined>(() => {});
export const ValueContext = createContext<unknown>(null);
18

19 20 21
type RadioGroupProps<T> = {
    value?: T;
    onChange?: (value: T) => unknown;
22 23
};

24 25 26 27 28 29
const RadioGroup = <T extends unknown>({
    value,
    onChange,
    children,
    className
}: PropsWithChildren<RadioGroupProps<T>> & WithStyled): ReturnType<FunctionComponent> => {
30 31
    const [selected, setSelected] = useState(value);
    const onSelectedChange = useCallback(
32
        (value: T) => {
33 34 35
            setSelected(value);
            onChange?.(value);
        },
36
        [onChange] // eslint-disable-line react-hooks/exhaustive-deps
37 38 39
    );

    return (
40
        <EventContext.Provider value={v => onSelectedChange(v as T)}>
41 42 43 44 45 46 47 48
            <ValueContext.Provider value={selected}>
                <Wrapper className={className}>{children}</Wrapper>
            </ValueContext.Provider>
        </EventContext.Provider>
    );
};

export default RadioGroup;