diff --git a/src/components/Radio.tsx b/src/components/Radio.tsx index 98d309487..df0fec23d 100644 --- a/src/components/Radio.tsx +++ b/src/components/Radio.tsx @@ -54,7 +54,10 @@ const LabelWithTooltipWrapper = styled.div` type RadioProps = PropsWithChildren< Omit, 'type'>>; -export const Radio = ({ children, disabled, ...props }: RadioProps & {tooltipText?: string}) => { +export const Radio = ({ children, disabled, labelAs, ...props }: RadioProps & { + tooltipText?: string; + labelAs?: string; +}) => { const state = useTooltipTriggerState({delay: 0}); const ref = React.useRef(null); @@ -64,7 +67,7 @@ export const Radio = ({ children, disabled, ...props }: RadioProps & {tooltipTex return props.tooltipText ?
- + state.open()} isDisabled={disabled} aria-disabled={disabled} {...props} /> {children} {state.isOpen && ( @@ -73,7 +76,7 @@ export const Radio = ({ children, disabled, ...props }: RadioProps & {tooltipTex
- : + : {children} ; diff --git a/src/components/forms/controlled/inputTypes.tsx b/src/components/forms/controlled/inputTypes.tsx index 83a617dcb..8358f61e4 100644 --- a/src/components/forms/controlled/inputTypes.tsx +++ b/src/components/forms/controlled/inputTypes.tsx @@ -7,6 +7,11 @@ type MakeControlled> = name: string; emptyDisabledValue?: boolean; }; +type MakeControlledCheckbox> = + Omit, 'checked'> & { + name: string; + emptyDisabledValue?: boolean; + }; const useEmptyDisabledValue = ( props: {disabled?: boolean; emptyDisabledValue?: boolean}, @@ -43,7 +48,7 @@ export const TextInput = (props: MakeControlled) return ; }; @@ -67,10 +72,10 @@ export const TextArea = (props: MakeControlled) => />; }; -export const Radio = (props: MakeControlled) => { +export const Radio = (props: MakeControlledCheckbox) => { const {data, namespace, setInput} = useFormHelpers(); - const onChangeValue = (value: boolean | undefined) => { + const onChangeValue = (value: string) => { props.onChangeValue?.(value); setInput.field(props.name)(value); }; @@ -85,7 +90,7 @@ export const Radio = (props: MakeControlled) => { />; }; -export const Checkbox = (props: MakeControlled) => { +export const Checkbox = (props: MakeControlledCheckbox) => { const {data, namespace, setInput} = useFormHelpers(); const onChangeValue = (value: boolean | undefined) => { @@ -171,3 +176,25 @@ export const File = (props: MakeControlled) => { onChangeValue={onChangeValue} />; }; + +export const RangeInput = (props: MakeControlled) => { + const {data, namespace, setInput} = useFormHelpers(); + + const onChangeValue = (value: number | undefined) => { + props.onChangeValue?.(value); + setInput.field(props.name)(value); + }; + + const value = data[props.name]; + const numberValue = parseFloat((value ?? '').toString()); + const formValue = isNaN(numberValue) ? '' : numberValue; + + useEmptyDisabledValue(props, formValue, onChangeValue); + + return ; +} diff --git a/src/components/forms/uncontrolled/inputType.stories.tsx b/src/components/forms/uncontrolled/inputType.stories.tsx index 9218ac3cc..f87b196dd 100644 --- a/src/components/forms/uncontrolled/inputType.stories.tsx +++ b/src/components/forms/uncontrolled/inputType.stories.tsx @@ -1,5 +1,5 @@ import styled from "styled-components"; -import { Checkbox } from "./inputTypes"; +import { Checkbox, RangeInput } from "./inputTypes"; const CheckboxGroup = styled.div` @@ -39,4 +39,11 @@ export const disabledCheckbox = () => <> {renderCheckboxes({error: [], disabled: true, label: 'Checkbox Label', variant: 'disabled', size: 1.6})} {renderCheckboxes({error: [], disabled: true, label: 'Checkbox Label', variant: 'disabled', size: 1.8})} {renderCheckboxes({error: [], disabled: true, label: 'Checkbox Label', variant: 'disabled', size: 2.0})} -; \ No newline at end of file +; + +export const slider = () => <> + + +; diff --git a/src/components/forms/uncontrolled/inputTypes.tsx b/src/components/forms/uncontrolled/inputTypes.tsx index 3e7ff3e4f..50fa0374c 100644 --- a/src/components/forms/uncontrolled/inputTypes.tsx +++ b/src/components/forms/uncontrolled/inputTypes.tsx @@ -161,7 +161,7 @@ export const Select = ({ * radio element */ type RadioProps = React.ComponentPropsWithoutRef<'input'> & InputProps & { - onChangeValue?: (value: boolean | undefined) => void; + onChangeValue?: (value: string) => void; wrapperProps?: React.ComponentPropsWithoutRef<'label'>; tooltipText?: string; }; @@ -182,8 +182,10 @@ export const Radio = ({ }: RadioProps) => { return - { - onChangeValue?.(!!e.target.checked); + { + if (e.target.checked) { + onChangeValue?.(e.target.value); + } props.onChange?.(e); }}> {label} @@ -271,3 +273,48 @@ export const File = ({label, help, wrapperProps, onChangeValue, uploader, value, ; }; + +const RangeInputWrapper = styled(FormInputWrapper)` + datalist { + display: flex; + justify-content: space-between; + writing-mode:unset; + flex-direction: row; + padding: 0 1em; + + option { + width: 0; + text-align: center; + display: flex; + justify-content: center; + } + } +`; +type RangeProps = React.ComponentPropsWithoutRef<'input'> & InputProps & { + wrapperProps?: React.ComponentPropsWithoutRef<'label'>; + onChangeValue?: (value: number | undefined) => void; + labels?: {value: number; label: string}[]; +}; +export const RangeInput = ({label, help, wrapperProps, onChangeValue, labels, ...props}: RangeProps) => { + const datalistId = React.useMemo(() => `datalist-${Math.random().toString(36).substring(2, 15)}`, []); + + return + {label}: + 0 ? datalistId : undefined} + onChange={e => { + const newValue = parseFloat(e.target.value); + onChangeValue?.(isNaN(newValue) ? undefined : newValue); + props.onChange?.(e); + }} + /> + {labels && labels.length > 0 && ( + + {labels.map(label => ( + + )} + + ; +}