Sha256: 9acc34119852fc8f293ae6522e6538279207f88bb3f749dbbb8f82d759e65f99

Contents?: true

Size: 1.97 KB

Versions: 255

Compression:

Stored size: 1.97 KB

Contents

import React, { useState } from 'react'
import classnames from 'classnames'

import { buildAriaProps, buildCss, buildDataProps } from '../utilities/props'
import { globalProps } from '../utilities/globalProps'
import { SelectableListItemProps } from './_item.js'

import List from  '../pb_list/_list'
import SelectableListItem from './_item'

type SelectableListProps = {
  aria?: {[key: string]: string },
  children?: React.ReactElement[],
  className?: string,
  data?: object,
  id?: string,
  variant?: 'checkbox' | 'radio',
}

const SelectableList = (props: SelectableListProps) => {
  const {
    aria = {},
    children,
    className,
    data = {},
    id,
  } = props

  const ariaProps = buildAriaProps(aria)
  const classes = classnames(buildCss('pb_selectable_list_kit'), globalProps(props), className)
  const dataProps = buildDataProps(data)
  const isRadio = props.variant === "radio"
  const defaultCheckedRadioValue = children.filter((item: {props:SelectableListItemProps} ) => item.props.defaultChecked)[0]?.props?.value

  const [selectedRadioValue, setSelectedRadioValue] = useState(defaultCheckedRadioValue)
  
  const onChangeRadioValue = ({ target }: React.ChangeEvent<HTMLInputElement>) => {
    setSelectedRadioValue(target.value)
  }

  let selectableListItems = children

  if (isRadio) {
    selectableListItems = children.map(( {props}: {props:SelectableListItemProps} ) => {
      return (
        <SelectableListItem
            {...props}
            className={classnames(selectedRadioValue === props.value ? "checked_item" : "", props.className)}
            key={props.value}
            onChange={evt => { onChangeRadioValue(evt); props?.onChange?.(evt); }}
        />
      )
    })
  }

  
  return (
    <div
        {...ariaProps}
        {...dataProps}
        className={classes}
        id={id}
    >
      <List variant={props.variant}>
        {selectableListItems}
      </List>
    </div>
  )
}
 
SelectableList.Item = SelectableListItem

export default SelectableList

Version data entries

255 entries across 255 versions & 1 rubygems

Version Path
playbook_ui-12.37.0.pre.alpha.PLAYaddingdatapropselectkit1071 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.38.0 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.37.0.pre.alpha.svgiconmethods1064 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.37.0.pre.alpha.PLAY951collapsiblenav31062 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.37.0 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.36.0.pre.alpha.svgiconmethods1059 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.36.0.pre.alpha.PLAY936momentjs1058 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.36.0.pre.alpha.PLAY936momentjs1050 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.36.0.pre.alpha.PLAY936momentjs1047 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.36.0.pre.alpha.svgiconmethods1043 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.36.0.pre.alpha.PLAY942collapsiblenav21035 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.36.0 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.35.0.pre.alpha.iconpocwebfonts1023 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.35.0 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.34.0.pre.alpha.fixdialogcloseevents1004 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.34.0.pre.alpha.fixdialogcloseevents1003 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.34.0.pre.alpha.play716popoverkitcloseonclickissue998 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.34.0 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.33.1.pre.alpha.PLAY933navkitcollapsible994 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx
playbook_ui-12.33.1 app/pb_kits/playbook/pb_selectable_list/_selectable_list.tsx