MCPcopy Create free account
hub / github.com/adobe/react-spectrum / UnionControl

Function UnionControl

packages/dev/s2-docs/src/VisualExampleClient.tsx:793–848  ·  view source on GitHub ↗
({control, value, onChange, isPicker = false})

Source from the content-addressed store, hash-verified

791const controlWidth = {default: 'full', sm: 130} as const;
792
793function UnionControl({control, value, onChange, isPicker = false}) {
794 let length = control.value.elements.reduce((p, v) => p + v.value, '').length;
795 if (isPicker || control.options?.control === 'picker' || length > 18) {
796 return (
797 <Picker
798 label={control.name}
799 contextualHelp={<PropContextualHelp control={control} />}
800 value={value == null && control.optional && !control.default ? '__none' : value}
801 onChange={v => onChange(v === '__none' ? null : v)}
802 styles={style({width: controlWidth})}>
803 {control.optional && !control.default ? <PickerItem id="__none">Default</PickerItem> : null}
804 {control.value.elements
805 .filter(e => e.value)
806 .map(element => (
807 <PickerItem key={element.value} id={element.value}>
808 {String(element.value)}
809 </PickerItem>
810 ))}
811 </Picker>
812 );
813 }
814
815 return (
816 <Wrapper
817 control={control}
818 styles={style({
819 gridColumnStart: {
820 isLong: 1
821 },
822 gridColumnEnd: {
823 isLong: -1
824 }
825 })({isLong: length > 12 || control.value.elements.length > 3})}>
826 <ToggleButtonGroup
827 aria-label={control.name}
828 disallowEmptySelection={!control.optional || !!control.default}
829 selectedKeys={[value]}
830 onSelectionChange={keys => onChange([...keys][0])}
831 density="compact">
832 {control.value.elements.map(element => (
833 <ToggleButton
834 key={element.value}
835 id={element.value}
836 styles={style({
837 flexGrow: {
838 default: 1,
839 lg: 0
840 }
841 })}>
842 {element.value}
843 </ToggleButton>
844 ))}
845 </ToggleButtonGroup>
846 </Wrapper>
847 );
848}
849
850function Wrapper({

Callers

nothing calls this directly

Calls 3

StringFunction · 0.85
onChangeFunction · 0.70
filterMethod · 0.65

Tested by

no test coverage detected