Skip to content

Commit

Permalink
Add a more complex option
Browse files Browse the repository at this point in the history
  • Loading branch information
tristen committed Jan 23, 2024
1 parent 23ac032 commit a5bfa22
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 103 deletions.
38 changes: 14 additions & 24 deletions src/components/select/examples/select-example-basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,31 @@ import React, { useState } from 'react';
import Select from '../select';

export default function Example() {
const [value, setValue] = useState('');
const [value, setValue] = useState('humpback-whale');

const options = [
{
label: 'Humpback whale',
value: 'humpback-whale'
},
{
label: 'Rufous Hummingbird',
value: 'rufous-hummingbird'
},
{
label: 'Sea Otter',
value: 'sea-otter'
},
{
label: 'Snowshoe Hare',
value: 'snowshoe-hare'
}
].map(option => {
const animals = {
'humpback-whale': 'Humpback whale',
'rufous-hummingbird': 'Rufous Hummingbird',
'sea-otter': 'Sea Otter',
'snowshoe-hare': 'Snowshoe Hare'
};

const options = Object.keys(animals).map(option => {
return {
...option,
active: value === option.value
label: animals[option],
value: option,
active: value === option
}
});

return (
<Select
value={value}
onChange={setValue}
options={options}
>
<span className="link">
{`Click to change the value to: `}
<span className="txt-bold">{value}</span>
{`Today's animal is: `}
<span className="txt-bold">{animals[value]}</span>
</span>
</Select>
);
Expand Down
67 changes: 67 additions & 0 deletions src/components/select/examples/select-example-options.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
With options
*/
import React, { useState } from 'react';
import Select from '../select';

export default function Example() {
const [value, setValue] = useState('humpback-whale');
const [filterBy, setFilterBy] = useState({
input: '',
value: ''
});

const animals = {
'humpback-whale': 'Humpback whale',
'rufous-hummingbird': 'Rufous Hummingbird',
'sea-otter': 'Sea Otter',
'snowshoe-hare': 'Snowshoe Hare'
};

const options = Object.keys(animals)
.filter(option => filterBy ? option.includes(filterBy.value) : true)
.map(option => ({
label: animals[option],
value: option,
active: value === option
}));

const renderHeader = (
<div className="px6 pt6">
<label className="txt-bold txt-s mb6 pt12">This is a header</label>
<input
className="input"
type="text"
placeholder="Filter"
autoFocus={true}
value={filterBy.input}
onChange={e => {
setFilterBy({
input: e.target.value,
value: e.target.value.toLowerCase().trim().split(/\s/g).join('-')
})
}}
/>
</div>
);

const renderFooter = (
<div className="bg-gray-lighter px12 py6 txt-s round-b color-gray">This is a footer</div>
);

return (
<Select
onChange={setValue}
options={options}
header={renderHeader}
footer={renderFooter}
themeSelectItemWrapper="px6 py12"
padding="none"
>
<span className="link">
{`Today's animal is: `}
<span className="txt-bold">{animals[value]}</span>
</span>
</Select>
);
}
Loading

0 comments on commit a5bfa22

Please sign in to comment.