1 | import * as React from 'react';
|
2 | import { counter, getId, getPrefix } from './context';
|
3 |
|
4 | const prefixId = (id, prefix, name) => {
|
5 | const uid = prefix + id;
|
6 | return String(name ? name(uid) : uid);
|
7 | };
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export class UID extends React.Component {
|
24 | constructor() {
|
25 | super(...arguments);
|
26 | this.state = {
|
27 | quartz: this.props.idSource || counter,
|
28 | prefix: getPrefix(this.props.idSource),
|
29 | id: getId(this.props.idSource || counter),
|
30 | };
|
31 | this.uid = (item) => prefixId(this.state.id + '-' + this.state.quartz.uid(item), this.state.prefix, this.props.name);
|
32 | }
|
33 | render() {
|
34 | const { children, name } = this.props;
|
35 | const { id, prefix } = this.state;
|
36 | return children(prefixId(id, prefix, name), this.uid);
|
37 | }
|
38 | }
|