//url=?node-id=18787-39450 //source=./index.tsx //component=RadioGroup import figma from 'figma' const instance = figma.selectedInstance const state = instance.getEnum('State', { Normal: undefined, Error: 'error', Warning: 'warning', }) const withLabel = instance.getBoolean('Label') let label let message if (withLabel) { const labelInstance = instance.findInstance('Form label') if (labelInstance.type !== 'ERROR') { const labelText = labelInstance.findText('Label') if (labelText.type === 'TEXT') { label = labelText.textContent } } label ??= 'Label' } if (state === 'error' || state === 'warning') { const messageInstance = instance.findInstance('Form inline message') if (messageInstance.type !== 'ERROR') { const messageText = messageInstance.findText('Label') if (messageText.type === 'TEXT') { message = messageText.textContent } } message ??= 'Message' } const orientation = instance.getEnum('Orientation', { Horizontal: 'horizontal', Vertical: undefined, })! const childInstance = instance.findInstance('Radio group', { traverseInstances: true, }) const children = childInstance.type !== 'ERROR' ? childInstance.findConnectedInstances((node) => !!node, { path: ['Radio group'], }) : [] const propLines = [ label ? `label=${JSON.stringify(label)}` : undefined, state === 'error' && message ? `error=${JSON.stringify(message)}` : undefined, state === 'warning' && message ? `warning=${JSON.stringify(message)}` : undefined, orientation === 'horizontal' ? `orientation=${JSON.stringify(orientation)}` : undefined, `value="selected-item-id"`, ].filter((line): line is string => Boolean(line)) const propsBlock = `\n ${propLines.join('\n ')}\n` const mappedChildren = children .map((c) => { if (c.type !== 'ERROR') { const radioText = c.findText('Label') let radioLabel = c.name || 'Label' if (radioText.type === 'TEXT') { radioLabel = radioText.textContent } return `` } }) .filter((child): child is string => Boolean(child)) .join('\n ') export default { id: 'RadioGroup', imports: ['import { RadioGroup, Radio } from "@planview/pv-form"'], example: figma.tsx`${mappedChildren}`, metadata: { nestable: true, }, }