import { useState, useMemo } from '@wordpress/element'; import { Flex, FlexBlock } from '@wordpress/components'; import { useControlSettingSet } from '../_hooks'; type Props = { controlSetting: any, // wp.customize.setting or wp.customize.settings[key] choices: { [key: string]: { label: string, title: string, image: string } }, value: string, title: string, description?: string, col?: number } export default function ApuraImageButtonGroup({ controlSetting, choices, value, title, description, col }: Props) { const buttons = useMemo( () => Object.entries(choices).map(([key, val]) => ({ label: val.label, value: key, image: val.image })), [choices] ); const [ selected, setSelected ] = useState(value); useControlSettingSet({ controlSetting, value: selected }); const handleOnClick = (value: string) => { setSelected(value); } // 1カラム以外の場合、col に応じたクラスを付与、デフォルトのgapは8px. const flexBasis = col && col !== 0 ? `calc(100% / ${col} - (8px * (${col} - 1) / ${col}))` : ''; return (
{title && {title}} {description &&

{description}

} {buttons.length > 0 && buttons.map(item => { return ( ) })}
) }