@gravel-form/blueprintjs
Version:
A flexible middlewares driven json schema form with Blueprintjs
1 lines • 1.48 kB
Source Map (JSON)
{"version":3,"sources":["middlewares/CheckboxGroupMw.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAExD,eAAO,MAAM,eAAe,EAAE,KAAK,CAAC,aAAa,CAAC,4BAA4B,CAiC7E,CAAC;AAEF,eAAe,eAAe,CAAC","file":"CheckboxGroupMw.d.ts","sourcesContent":["import * as React from 'react';\nimport get from 'lodash/get';\nimport { Checkbox } from '@blueprintjs/core';\nimport { BlueprintFormMiddlewareProps } from '../share';\n\nexport const CheckboxGroupMw: React.ComponentType<BlueprintFormMiddlewareProps> = (props) => {\n const { schema, onChange, extraProps, next } = props;\n\n if (\n typeof schema === 'boolean' ||\n Array.isArray(schema.items) ||\n typeof schema.items === 'boolean' ||\n !schema.items ||\n !schema.items.enum ||\n !schema.uniqueItems\n ) {\n return next(props);\n }\n const checkboxValues = schema.items.enum || [];\n const labels = get(extraProps, 'labels', schema.items.enum);\n\n const data = Array.isArray(props.data) ? props.data : [];\n\n return (\n <>\n {checkboxValues.map((value, index) => (\n <Checkbox\n key={JSON.stringify(value)}\n checked={data.includes(value)}\n onChange={(e) => {\n onChange(e.currentTarget.checked ? [...data, value] : data.filter((v) => v !== value));\n }}\n >\n {get(labels, index, null)}\n </Checkbox>\n ))}\n </>\n );\n};\n\nexport default CheckboxGroupMw;\n"]}