|
| 1 | +import React from 'react'; |
| 2 | +import { ComponentStory, ComponentMeta } from '@storybook/react'; |
| 3 | +import { Alert } from 'react-native'; |
| 4 | +import SelectBottomSheet from './SelectBottomSheet'; |
| 5 | +import Text from '../Text/Text'; |
| 6 | +import Button from '../Button/Button'; |
| 7 | +import Box from '../Box/Box'; |
| 8 | + |
| 9 | +const typeList = ['checkbox', 'radio']; |
| 10 | + |
| 11 | +const SelectBottomSheetMeta: ComponentMeta<typeof SelectBottomSheet> = { |
| 12 | + title: 'SelectBottomSheet', |
| 13 | + component: SelectBottomSheet, |
| 14 | + argTypes: { |
| 15 | + type: typeList, |
| 16 | + options: { |
| 17 | + id: '1', |
| 18 | + label: 'Option', |
| 19 | + }, |
| 20 | + }, |
| 21 | + args: { |
| 22 | + visible: true, |
| 23 | + type: 'checkbox', |
| 24 | + selectButtonLabel: 'Select', |
| 25 | + closeButtonLabel: 'Close', |
| 26 | + title: 'Select Options', |
| 27 | + options: [ |
| 28 | + { id: '1', label: 'Option 1' }, |
| 29 | + { id: '2', label: 'Option 2' }, |
| 30 | + { id: '3', label: 'Option 3' }, |
| 31 | + ], |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +export default SelectBottomSheetMeta; |
| 36 | + |
| 37 | +type SelectBottomSheetStory = ComponentStory<typeof SelectBottomSheet>; |
| 38 | + |
| 39 | +export const Basic: SelectBottomSheetStory = args => ( |
| 40 | + <Box borderRadius="l" p="m"> |
| 41 | + <Text variant="subtitle1Bold" mb="l"> |
| 42 | + Select Bottom Sheet |
| 43 | + </Text> |
| 44 | + |
| 45 | + <SelectBottomSheet |
| 46 | + visible={args.visible} |
| 47 | + type={args.type} |
| 48 | + selectButtonLabel="Select" |
| 49 | + closeButtonLabel="Close" |
| 50 | + title="Select Options" |
| 51 | + options={args.options} |
| 52 | + onSelect={() => {}} |
| 53 | + /> |
| 54 | + </Box> |
| 55 | +); |
| 56 | + |
| 57 | +export const Checkbox: SelectBottomSheetStory = () => { |
| 58 | + const [modal, showModal] = React.useState(false); |
| 59 | + const [selectedOptions, setSelectedOptions] = React.useState<string[]>([]); |
| 60 | + |
| 61 | + const handleMultiSelect = (options: string | string[]) => { |
| 62 | + if (Array.isArray(options)) { |
| 63 | + setSelectedOptions(options); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + const options = [ |
| 68 | + { id: '1', label: 'Option 1' }, |
| 69 | + { id: '2', label: 'Option 2' }, |
| 70 | + { id: '3', label: 'Option 3' }, |
| 71 | + { id: '4', label: 'Option 4' }, |
| 72 | + { id: '5', label: 'Option 5' }, |
| 73 | + { id: '6', label: 'Option 6' }, |
| 74 | + { id: '7', label: 'Option 7' }, |
| 75 | + { id: '8', label: 'Option 8' }, |
| 76 | + { id: '9', label: 'Option 9' }, |
| 77 | + ]; |
| 78 | + |
| 79 | + const showAlert = (selected: string[]) => { |
| 80 | + Alert.alert('Selected Items', selected.join(', ')); |
| 81 | + }; |
| 82 | + |
| 83 | + return ( |
| 84 | + <> |
| 85 | + <Text p="2xs" variant="subtitle01Bold"> |
| 86 | + Select Bottom Sheet with Checkbox |
| 87 | + </Text> |
| 88 | + |
| 89 | + <Box px="m" py="2xs"> |
| 90 | + <Button |
| 91 | + mb="m" |
| 92 | + filled |
| 93 | + label="Checkbox" |
| 94 | + onPress={() => showModal(true)} |
| 95 | + /> |
| 96 | + <SelectBottomSheet |
| 97 | + visible={modal} |
| 98 | + type="checkbox" |
| 99 | + selectButtonLabel="Select" |
| 100 | + closeButtonLabel="Close" |
| 101 | + title="Select Options" |
| 102 | + closeButtonAction={() => showModal(false)} |
| 103 | + selectButtonAction={() => { |
| 104 | + showAlert(selectedOptions); |
| 105 | + showModal(false); |
| 106 | + }} |
| 107 | + options={options} |
| 108 | + selectedOption={selectedOptions} |
| 109 | + onSelect={handleMultiSelect} |
| 110 | + /> |
| 111 | + </Box> |
| 112 | + </> |
| 113 | + ); |
| 114 | +}; |
| 115 | + |
| 116 | +export const RadioButton: SelectBottomSheetStory = () => { |
| 117 | + const [modal, showModal] = React.useState(false); |
| 118 | + const [selectedRadioOption, setSelectedRadioOption] = |
| 119 | + React.useState<string>(''); |
| 120 | + |
| 121 | + const handleRadioSelect = (option: string | string[]) => { |
| 122 | + if (Array.isArray(option) && option.length > 0) { |
| 123 | + setSelectedRadioOption(option[0]); |
| 124 | + } else if (typeof option === 'string') { |
| 125 | + setSelectedRadioOption(option); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + const options = [ |
| 130 | + { id: '1', label: 'Option 1' }, |
| 131 | + { id: '2', label: 'Option 2' }, |
| 132 | + { id: '3', label: 'Option 3' }, |
| 133 | + { id: '4', label: 'Option 4' }, |
| 134 | + { id: '5', label: 'Option 5' }, |
| 135 | + { id: '6', label: 'Option 6' }, |
| 136 | + { id: '7', label: 'Option 7' }, |
| 137 | + { id: '8', label: 'Option 8' }, |
| 138 | + { id: '9', label: 'Option 9' }, |
| 139 | + ]; |
| 140 | + |
| 141 | + const showAlert = (selected: string[]) => { |
| 142 | + Alert.alert('Selected Items', selected.join(', ')); |
| 143 | + }; |
| 144 | + |
| 145 | + return ( |
| 146 | + <> |
| 147 | + <Text p="2xs" variant="subtitle01Bold"> |
| 148 | + Select Bottom Sheet with RadioButton |
| 149 | + </Text> |
| 150 | + |
| 151 | + <Box px="m" py="2xs"> |
| 152 | + <Button |
| 153 | + mb="m" |
| 154 | + filled |
| 155 | + label="RadioButton" |
| 156 | + onPress={() => showModal(true)} |
| 157 | + /> |
| 158 | + <SelectBottomSheet |
| 159 | + visible={modal} |
| 160 | + type="radio" |
| 161 | + selectButtonLabel="Select" |
| 162 | + closeButtonLabel="Close" |
| 163 | + title="Select a Single Option" |
| 164 | + closeButtonAction={() => showModal(false)} |
| 165 | + selectButtonAction={() => { |
| 166 | + showAlert([selectedRadioOption]); |
| 167 | + showModal(false); |
| 168 | + }} |
| 169 | + options={options} |
| 170 | + selectedOption={[selectedRadioOption]} |
| 171 | + onSelect={handleRadioSelect} |
| 172 | + /> |
| 173 | + </Box> |
| 174 | + </> |
| 175 | + ); |
| 176 | +}; |
0 commit comments