|
| 1 | +/* eslint-disable no-console, react/prop-types */ |
| 2 | +import React from 'react'; |
| 3 | +import Combobox from '~/components/combobox'; |
| 4 | +import comboboxFilterAndLimit from '~/components/combobox/filter'; |
| 5 | +import Icon from '~/components/icon'; |
| 6 | +import IconSettings from '~/components/icon-settings'; |
| 7 | +import Tooltip from '../../tooltip'; |
| 8 | + |
| 9 | +const accounts = [ |
| 10 | + { |
| 11 | + id: '1', |
| 12 | + label: 'Acme', |
| 13 | + subTitle: 'Account • San Francisco', |
| 14 | + type: 'account', |
| 15 | + }, |
| 16 | + { |
| 17 | + id: '2', |
| 18 | + label: 'Salesforce.com, Inc.', |
| 19 | + subTitle: 'Account • San Francisco', |
| 20 | + type: 'account', |
| 21 | + }, |
| 22 | + { |
| 23 | + id: '3', |
| 24 | + label: "Paddy's Pub", |
| 25 | + subTitle: 'Account • Boston, MA', |
| 26 | + type: 'account', |
| 27 | + }, |
| 28 | + { |
| 29 | + id: '4', |
| 30 | + label: 'Tyrell Corp', |
| 31 | + subTitle: 'Account • San Francisco, CA', |
| 32 | + type: 'account', |
| 33 | + }, |
| 34 | + { |
| 35 | + id: '5', |
| 36 | + label: 'Paper St. Soap Company', |
| 37 | + subTitle: 'Account • Beloit, WI', |
| 38 | + type: 'account', |
| 39 | + }, |
| 40 | + { |
| 41 | + id: '6', |
| 42 | + label: 'Nakatomi Investments', |
| 43 | + subTitle: 'Account • Chicago, IL', |
| 44 | + type: 'account', |
| 45 | + }, |
| 46 | + { id: '7', label: 'Acme Landscaping', subTitle: '\u00A0', type: 'account' }, |
| 47 | + { |
| 48 | + id: '8', |
| 49 | + label: 'Acme Construction', |
| 50 | + subTitle: 'Account • Grand Marais, MN', |
| 51 | + type: 'account', |
| 52 | + }, |
| 53 | +]; |
| 54 | + |
| 55 | +const accountsWithIcon = accounts.map((elem) => ({ |
| 56 | + ...elem, |
| 57 | + ...{ |
| 58 | + icon: ( |
| 59 | + <Icon |
| 60 | + assistiveText={{ label: 'Account' }} |
| 61 | + category="standard" |
| 62 | + name={elem.type} |
| 63 | + /> |
| 64 | + ), |
| 65 | + }, |
| 66 | +})); |
| 67 | + |
| 68 | +class Example extends React.Component { |
| 69 | + constructor(props) { |
| 70 | + super(props); |
| 71 | + |
| 72 | + this.state = { |
| 73 | + inputValue: '', |
| 74 | + selection: [accountsWithIcon[0], accountsWithIcon[1]], |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + render() { |
| 79 | + return ( |
| 80 | + <IconSettings iconPath="/assets/icons"> |
| 81 | + <Combobox |
| 82 | + id="combobox-unique-id" |
| 83 | + disabled={this.props.disabled} |
| 84 | + events={{ |
| 85 | + onChange: (event, { value }) => { |
| 86 | + if (this.props.action) { |
| 87 | + this.props.action('onChange')(event, value); |
| 88 | + } else if (console) { |
| 89 | + console.log('onChange', event, value); |
| 90 | + } |
| 91 | + this.setState({ inputValue: value }); |
| 92 | + }, |
| 93 | + onRequestRemoveSelectedOption: (event, data) => { |
| 94 | + this.setState({ |
| 95 | + inputValue: '', |
| 96 | + selection: data.selection, |
| 97 | + }); |
| 98 | + }, |
| 99 | + onSubmit: (event, { value }) => { |
| 100 | + if (this.props.action) { |
| 101 | + this.props.action('onChange')(event, value); |
| 102 | + } else if (console) { |
| 103 | + console.log('onChange', event, value); |
| 104 | + } |
| 105 | + this.setState({ |
| 106 | + inputValue: '', |
| 107 | + selection: [ |
| 108 | + ...this.state.selection, |
| 109 | + { |
| 110 | + label: value, |
| 111 | + icon: ( |
| 112 | + <Icon |
| 113 | + assistiveText="Account" |
| 114 | + category="standard" |
| 115 | + name="account" |
| 116 | + /> |
| 117 | + ), |
| 118 | + }, |
| 119 | + ], |
| 120 | + }); |
| 121 | + }, |
| 122 | + onSelect: (event, data) => { |
| 123 | + if (this.props.action) { |
| 124 | + this.props.action('onSelect')( |
| 125 | + event, |
| 126 | + ...Object.keys(data).map((key) => data[key]) |
| 127 | + ); |
| 128 | + } else if (console) { |
| 129 | + console.log('onSelect', event, data); |
| 130 | + } |
| 131 | + this.setState({ |
| 132 | + inputValue: '', |
| 133 | + selection: data.selection, |
| 134 | + }); |
| 135 | + }, |
| 136 | + }} |
| 137 | + fieldLevelHelpTooltip={ |
| 138 | + <Tooltip |
| 139 | + align="top left" |
| 140 | + content="Type to search Salesforce objects..." |
| 141 | + id="field-level-help-tooltip" |
| 142 | + /> |
| 143 | + } |
| 144 | + labels={{ |
| 145 | + label: 'Search', |
| 146 | + placeholder: 'Search Salesforce', |
| 147 | + }} |
| 148 | + multiple |
| 149 | + options={comboboxFilterAndLimit({ |
| 150 | + inputValue: this.state.inputValue, |
| 151 | + limit: 10, |
| 152 | + options: accountsWithIcon, |
| 153 | + selection: this.state.selection, |
| 154 | + })} |
| 155 | + selection={this.state.selection} |
| 156 | + value={this.state.inputValue} |
| 157 | + /> |
| 158 | + </IconSettings> |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +Example.displayName = 'ComboboxExample'; |
| 164 | +export default Example; // export is replaced with `ReactDOM.render(<Example />, mountNode);` at runtime |
0 commit comments