|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { IStyle, Label, mergeStyles, Stack } from '@fluentui/react'; |
| 5 | +import { useWarnings } from '@fluentui/react-hooks'; |
| 6 | +import React from 'react'; |
| 7 | +import { chunk } from '../utils'; |
| 8 | +import { _VideoEffectsItem, _VideoEffectsItemProps } from './VideoEffectsItem'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Props for {@link _VideoBackgroundEffectsPicker} |
| 12 | + * @internal |
| 13 | + */ |
| 14 | +export interface _VideoBackgroundEffectsPickerProps { |
| 15 | + /** |
| 16 | + * The options to display in the picker. |
| 17 | + */ |
| 18 | + options: _VideoBackgroundEffectChoiceOption[]; |
| 19 | + |
| 20 | + /** |
| 21 | + * The key of the current selected Video Background Effect. |
| 22 | + * If you provide this, you must maintain selection state by observing onChange events and passing a new value in when changed. |
| 23 | + */ |
| 24 | + selectedEffectKey?: string; |
| 25 | + |
| 26 | + /** |
| 27 | + * Callback to invoke when a Video Background Effect is selected. |
| 28 | + * @param effectKey - The key of the Video Background Effect that was selected. |
| 29 | + */ |
| 30 | + onChange?: (effectKey: string) => void; |
| 31 | + |
| 32 | + /** |
| 33 | + * The key of the Video Background Effect that is initially selected. |
| 34 | + * Only provide this if the picker is an uncontrolled component; |
| 35 | + * otherwise, use the `selectedEffectKey` property. |
| 36 | + */ |
| 37 | + defaultSelectedEffectKey?: string; |
| 38 | + |
| 39 | + /** |
| 40 | + * The label to display for the picker. |
| 41 | + */ |
| 42 | + label?: string; |
| 43 | + |
| 44 | + /** |
| 45 | + * The number of items to display per row. |
| 46 | + * @default 3 |
| 47 | + */ |
| 48 | + itemsPerRow?: 'wrap' | number; |
| 49 | + |
| 50 | + /** |
| 51 | + * Styles for the picker. |
| 52 | + */ |
| 53 | + styles?: _VideoBackgroundEffectsPickerStyles; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Option for the {@link _VideoBackgroundEffectsPicker}. |
| 58 | + * @internal |
| 59 | + */ |
| 60 | +export type _VideoBackgroundEffectChoiceOption = _VideoEffectsItemProps; |
| 61 | + |
| 62 | +/** |
| 63 | + * Styles for the {@link _VideoBackgroundEffectsPicker}. |
| 64 | + * @internal |
| 65 | + */ |
| 66 | +export interface _VideoBackgroundEffectsPickerStyles { |
| 67 | + /** |
| 68 | + * Styles for the root element. |
| 69 | + */ |
| 70 | + root?: IStyle; |
| 71 | + |
| 72 | + /** |
| 73 | + * Styles for the label. |
| 74 | + */ |
| 75 | + label?: IStyle; |
| 76 | + |
| 77 | + /** |
| 78 | + * Styles for the root of each row element. |
| 79 | + */ |
| 80 | + rowRoot?: IStyle; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Picker for choosing a Video Background Effect. |
| 85 | + * |
| 86 | + * @remarks |
| 87 | + * This functions similar to a radio group of buttons, where the user can select one of the options. |
| 88 | + * |
| 89 | + * @internal |
| 90 | + */ |
| 91 | +export const _VideoBackgroundEffectsPicker = (props: _VideoBackgroundEffectsPickerProps): JSX.Element => { |
| 92 | + const [componentControlledSelectedEffectKey, setComponentControlledSelectedEffectKey] = React.useState< |
| 93 | + string | undefined |
| 94 | + >(props.defaultSelectedEffectKey); |
| 95 | + |
| 96 | + // Warn the developer if they use the component in an incorrect controlled way. |
| 97 | + useWarnings({ |
| 98 | + name: 'VideoBackgroundEffectsPicker', |
| 99 | + props, |
| 100 | + controlledUsage: { |
| 101 | + onChangeProp: 'onChange', |
| 102 | + valueProp: 'selectedEffectKey', |
| 103 | + defaultValueProp: 'defaultSelectedEffectKey' |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + const selectedEffect = props.selectedEffectKey ?? componentControlledSelectedEffectKey; |
| 108 | + const setSelectedEffect = (selectedEffectKey: string): void => { |
| 109 | + setComponentControlledSelectedEffectKey(selectedEffectKey); |
| 110 | + props.onChange?.(selectedEffectKey); |
| 111 | + }; |
| 112 | + |
| 113 | + const convertedOptions: _VideoEffectsItemProps[] = props.options.map((option) => ({ |
| 114 | + isSelected: option.key === selectedEffect, |
| 115 | + onSelect: () => setSelectedEffect(option.key), |
| 116 | + ...option |
| 117 | + })); |
| 118 | + |
| 119 | + const optionsByRow = |
| 120 | + props.itemsPerRow === 'wrap' ? [convertedOptions] : chunk(convertedOptions, props.itemsPerRow ?? 3); |
| 121 | + |
| 122 | + return ( |
| 123 | + <Stack tokens={{ childrenGap: '0.5rem' }}> |
| 124 | + <Label className={mergeStyles(props.styles?.label)}>{props.label}</Label> |
| 125 | + {optionsByRow.map((options, rowIndex) => ( |
| 126 | + <Stack |
| 127 | + className={mergeStyles(props.styles?.rowRoot)} |
| 128 | + wrap={props.itemsPerRow === 'wrap'} |
| 129 | + horizontal |
| 130 | + key={rowIndex} |
| 131 | + tokens={{ childrenGap: '0.5rem' }} |
| 132 | + > |
| 133 | + {options.map((option) => ( |
| 134 | + <_VideoEffectsItem {...option} key={option.key} /> |
| 135 | + ))} |
| 136 | + </Stack> |
| 137 | + ))} |
| 138 | + </Stack> |
| 139 | + ); |
| 140 | +}; |
0 commit comments