| 24 | |
| 25 | class DynamicList extends Component { |
| 26 | constructor(props) { |
| 27 | super(props); |
| 28 | this.state = { |
| 29 | before: props.initialBefore || [], |
| 30 | after: props.initialAfter || [] |
| 31 | }; |
| 32 | |
| 33 | setState = newState => this.setState(newState); |
| 34 | |
| 35 | prepend = () => { |
| 36 | const before = this.state.before; |
| 37 | const newValue = before[0] ? before[0] - 1 : 1; |
| 38 | this.setState({ |
| 39 | before: [newValue, ...before] |
| 40 | }); |
| 41 | }; |
| 42 | |
| 43 | append = () => { |
| 44 | const after = this.state.after; |
| 45 | const lastValue = after[after.length - 1]; |
| 46 | const newValue = lastValue ? lastValue + 1 : 2; |
| 47 | this.setState({ |
| 48 | after: [...after, newValue] |
| 49 | }); |
| 50 | }; |
| 51 | |
| 52 | shift = () => { |
| 53 | this.setState({ |
| 54 | before: this.state.before.slice(1) |
| 55 | }); |
| 56 | }; |
| 57 | |
| 58 | pop = () => { |
| 59 | this.setState({ |
| 60 | after: this.state.after.slice(0, -1) |
| 61 | }); |
| 62 | }; |
| 63 | |
| 64 | getDynamicListHtml = () => { |
| 65 | const liHtml = this.props.as == Input ? inputStr : span; |
| 66 | return div([ |
| 67 | ...this.state.before.map(liHtml), |
| 68 | '<input id="input-0" type="text">', |
| 69 | ...this.state.after.map(liHtml) |
| 70 | ]); |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | render(props, state) { |
| 75 | const ListComponent = props.as || ListItem; |