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

Function UnionControl

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

StringFunction · 0.85
onChangeFunction · 0.70
filterMethod · 0.65

Tested by

no test coverage detected