Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 1x 1x 1x 1x 1x 1x 1x 1x 3x | // @flow strict
import React, { useState, useCallback } from 'react';
import Example from '../Example';
import Chip from '../material-toolbox/chip';
import ChipSet from '../material-toolbox/chip/Set';
import ChipIcon from '../material-toolbox/chip/Icon';
import TextField from '../material-toolbox/textfield';
import Button from '../material-toolbox/button';
const source = `import React, { useState, useCallback } from 'react';
import Chip from 'material-toolbox/chip';
import ChipSet from 'material-toolbox/chip/Set';
import ChipIcon from 'material-toolbox/chip/Icon';
import TextField from 'material-toolbox/textfield';
import Button from 'material-toolbox/button';
const Input = () => {
const [chips, setChips] = useState(['tag-1', 'tag-2', 'tag-3']);
const [text, setText] = useState('');
const handleTextChange = useCallback(evt => {
const { value } = evt.currentTarget;
setText(value);
}, []);
const handleClick = useCallback(() => {
setChips(chips_ => [...chips_, text]);
setText('');
}, [text]);
const handleRemoval = useCallback(({ element }) => {
const { data } = element.dataset;
setChips(chips_ => chips_.filter(chip => chip !== data));
}, []);
return (
<>
<TextField
value={text}
onChange={handleTextChange}
outlined
label="Add new tag"
/>
<Button onClick={handleClick}>Add</Button>
<ChipSet input>
{chips.map(chip => (
<Chip
label={chip}
key={chip}
data-data={chip}
onRemoval={handleRemoval}
trailingIcon={
<ChipIcon trailing className="material-icons">
close
</ChipIcon>
}
/>
))}
</ChipSet>
</>
);
};
export default Input;`;
const Input = () => {
const [chips, setChips] = useState(['tag-1', 'tag-2', 'tag-3']);
const [text, setText] = useState('');
const handleTextChange = useCallback(
(evt: SyntheticEvent<HTMLInputElement>) => {
const { value } = evt.currentTarget;
setText(value);
},
[],
);
const handleClick = useCallback(() => {
setChips(chips_ => [...chips_, text]);
setText('');
}, [text]);
const handleRemoval = useCallback(({ element }) => {
const { data } = element.dataset;
setChips(chips_ => chips_.filter(chip => chip !== data));
}, []);
return (
<Example title="Input Chips" source={source}>
<TextField
value={text}
onChange={handleTextChange}
outlined
label="Add new tag"
/>
<Button onClick={handleClick}>Add</Button>
<ChipSet input>
{chips.map(chip => (
<Chip
label={chip}
key={chip}
data-data={chip}
onRemoval={handleRemoval}
trailingIcon={
<ChipIcon trailing className="material-icons">
close
</ChipIcon>
}
/>
))}
</ChipSet>
</Example>
);
};
export default Input;
|