import * as React from 'react'; import { Checkbox, Dropdown, IDropdownOption, Stack, Text, TextField } from '@fluentui/react'; import { PlaygroundProps } from './Playground.types'; const tableStyle: React.CSSProperties = { border: '1px solid black', }; const cellStyle: React.CSSProperties = { border: '1px solid black', padding: '5px', }; export const Playground = function (props: PlaygroundProps): JSX.Element { const { children, sections } = props; const [componentProps, setComponentProps] = React.useState<{ [key in string]: boolean | string } | null>(null); const newProps: { [key in string]: boolean | string } = {}; const playgroundSections: JSX.Element[] = []; let booleanValueChanged = false; for (const section of sections) { const sectionList: JSX.Element[] = []; for (const prop of section.propList) { const propName = prop.propName as string; const propType = prop.propType; let isPropEnabled = true; if (componentProps && prop.dependsOnProps) { for (const dependentProp of prop.dependsOnProps as string[]) { isPropEnabled = isPropEnabled && (dependentProp[0] === '~' ? !componentProps[dependentProp.substr(1)] : !!componentProps[dependentProp]); } } if (propType === 'boolean') { newProps[propName + 'Default'] = prop.defaultValue || false; const propDefaultValueChanged = componentProps && prop.defaultValue !== undefined && prop.defaultValue !== componentProps[propName + 'Default']; const propEnabledValueChanged = componentProps && componentProps[propName] !== (componentProps[propName] && isPropEnabled); newProps[propName] = componentProps && componentProps[propName] !== 'undefined' && !propDefaultValueChanged ? componentProps[propName] && isPropEnabled : newProps[propName + 'Default']; if (propDefaultValueChanged || propEnabledValueChanged) { prop.setDefaultValue?.(newProps[propName] as boolean); booleanValueChanged = true; } const onBooleanPropChange = (ev?: React.FormEvent, checked?: boolean) => { const newComponentProps: { [key in string]: boolean | string } = { ...componentProps }; newComponentProps[propName] = checked || false; setComponentProps(newComponentProps); prop.setDefaultValue?.(checked || false); }; sectionList.push( {propName}: , ); } else if (propType === 'string') { newProps[propName] = (componentProps && componentProps[propName]) || prop.defaultValue || ''; const onStringPropChange = ( ev?: React.FormEvent, newValue?: string, ) => { const newComponentProps: { [key in string]: boolean | string } = { ...componentProps }; newComponentProps[propName] = newValue || ''; setComponentProps(newComponentProps); }; sectionList.push( {propName}: , ); } else { const defaultSelectedKey = prop.defaultValue || propType[0]; newProps[propName] = (componentProps && componentProps[propName]) || prop.defaultValue || propType[0]; const onOptionsPropChange = ( ev?: React.FormEvent, option?: IDropdownOption, index?: number, ) => { const newComponentProps: { [key in string]: boolean | string } = { ...componentProps }; if (option) { newComponentProps[propName] = (option.key as string) || ''; setComponentProps(newComponentProps); } }; sectionList.push( {propName}: ({ key: value, text: value }))} // eslint-disable-next-line react/jsx-no-bind onChange={onOptionsPropChange} /> , ); } } playgroundSections.push( {section.sectionName} {sectionList} , ); } if (componentProps === null || booleanValueChanged) { setComponentProps(newProps); } const elementProps = { ...componentProps, children: componentProps && !componentProps.iconOnly && !componentProps.children && componentProps.content, icon: componentProps && componentProps.icon ? 'x' : undefined, }; return ( <> {React.cloneElement(children, elementProps || {})} {playgroundSections}
); };