-
Notifications
You must be signed in to change notification settings - Fork 82
/
ExternalClear.js
55 lines (46 loc) · 1.57 KB
/
ExternalClear.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React, { Component } from 'react';
import styled from '@emotion/styled';
import { Heading } from './components/Heading';
import Select from '../../../src';
class ExternalClear extends Component {
state = { values: [this.props.options[1]] };
onChange = (values) =>
this.setState({
values
});
onSet = (values) => {
const newValue = values.map((val) => ({ value: val.email, label: val.name }));
return this.setState({
values: [...this.state.values, ...newValue]
});
};
render() {
const { options, title } = this.props;
return (
<React.Fragment>
<Heading
title={title}
source="https://github.com/sanusart/react-dropdown-select/tree/master/docs/src/examples/ExternalClear.js"
/>
<p>I can be cleared from outside by setting values to <code>[]</code> <button href={() => null} onClick={() => this.onChange([])}>× clear</button></p>
<p>Values can be added from outside <button href={() => null} onClick={() => this.onSet([options[Math.floor(Math.random() * (10 - 1) + 1)]])}>» set</button></p>
<Select
multi
options={options}
values={[...this.state.values]}
onChange={(value) => {
this.onChange(value);
console.log(`%c > onChange ${title} `, 'background: #555; color: tomato', value);
}}
/>
</React.Fragment>
);
}
}
ExternalClear.propTypes = {};
const Title = styled.div`
display: flex;
justify-content: space-between;
align-items: baseline;
`;
export default ExternalClear;