All files / src/fieldcontrols/TagsInput TagsInput.tsx

48.65% Statements 18/37
0% Branches 0/10
7.69% Functions 1/13
50% Lines 16/32
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171        1x         1x 1x     1x 1x 1x   1x 1x                     1x                                             1x                 1x             1x                   1x                 1x         1x                                                                                                                                                           1x
/**
 * @module FieldControls
 * 
 */ /** */
import * as React from 'react'
import { FieldSettings } from 'sn-client-js'
import { IClientFieldSetting, IReactClientFieldSetting } from '../IClientFieldSetting'
import { IReferenceFieldSetting } from '../IReferenceFieldSetting'
 
import ChipInput from 'material-ui-chip-input'
import { styles } from './TagsInputStyles'
import * as mui from 'material-ui';
import { Col } from 'react-materialize'
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import { MuiThemeProvider, lightBaseTheme } from 'material-ui/styles';
const lightMuiTheme = getMuiTheme(lightBaseTheme);
 
import * as injectTapEventPlugin from 'react-tap-event-plugin';
injectTapEventPlugin();
 
 
/**
 * Interface for TagsInput properties
 */
export interface TagsInputProps extends IReactClientFieldSetting, IClientFieldSetting, IReferenceFieldSetting { }
 
/**
 * Field control that represents a Reference field. Available values will be populated from the FieldSettings.
 */
export class TagsInput extends React.Component<TagsInputProps, { chips, dataSource }> {
    /**
     * constructor
     * @param {object} props
     */
    constructor(props) {
        super(props);
        /**
         * @type {object}
         * @property {any[]} chips array of chips input value
         * @property {any[]} dataSource array of data for autocomplete
         */
        this.state = {
            chips: [],
            //dataSource: this.getDataSource(this.props['data-selectionRoot'])
            dataSource: ['foo', 'bar']
        };
    }
 
    /**
     * returns datasource arra by paths
     * @param {string} paths
     */
    getDataSource(paths) {
        let data = [];
        return data;
    }
 
    /**
     * handle change event on an input
     * @param {SytheticEvent} event
     */
    handleChange(event) {
 
    }
    /**
     * handle add event on an input
     * @param {any[]} chips
     */
    handleRequestAdd(...chips) {
        this.setState({
            chips: [...this.state.chips, ...chips]
        })
 
    }
    /**
     * handle delete event on an input
     * @param {any} deletedChip
     */
    handleRequestDelete(deletedChip) {
        this.setState({
            chips: this.state.chips.filter((c) => c !== deletedChip)
        })
    }
    /**
     * chip renderer
     * @param {SytheticEvent} event
     */
    chipRenderer() { }
    /**
     * render
     * @return {ReactElement} markup
     */
    render() {
        switch (this.props['data-actionName']) {
            case 'edit':
                return (
                    <div style={styles.container} className='col input-field s12'>
                        <MuiThemeProvider muiTheme={lightMuiTheme}>
                            <ChipInput
                                floatingLabelText={
                                    this.props.required
                                        ? this.props['data-labelText'] + ' *'
                                        : this.props['data-labelText']
                                }
                                defaultValue={['foo', 'bar']}
                                onRequestAdd={(chip) => this.handleRequestAdd(chip)}
                                onRequestDelete={(deletedChip) => this.handleRequestDelete(deletedChip)}
                                fullWidth
                                allowDuplicates={false}
                                dataSource={this.props.dataSource}
                                disabled={this.props.readOnly}
                                errorText={this.props['data-errorText']}
                                hintText={this.props['data-labelText']}
                                id={this.props.name}
                                style={{ ...this.props.style, ...styles.chipContainerStyle }}
                                value={this.state.chips}
                                chipRenderer={
                                    this.chipRenderer
 
                                }
                            />
                        </MuiThemeProvider>
                    </div>
                )
            case 'new':
                return (
                    <div style={styles.container} className='col input-field s12'>
                        <MuiThemeProvider muiTheme={lightMuiTheme}>
                            <ChipInput
                                floatingLabelText={
                                    this.props.required
                                        ? this.props['data-labelText'] + ' *'
                                        : this.props['data-labelText']
                                }
                                defaultValue={['foo', 'bar']}
                                onRequestAdd={(chip) => this.handleRequestAdd(chip)}
                                onRequestDelete={(deletedChip) => this.handleRequestDelete(deletedChip)}
                                fullWidth
                                allowDuplicates={false}
                                dataSource={this.props.dataSource}
                                disabled={this.props.readOnly}
                                errorText={this.props['data-errorText']}
                                hintText={this.props['data-labelText']}
                                id={this.props.name}
                                style={{ ...this.props.style, ...styles.chipContainerStyle }}
                                value={this.state.chips}
                                chipRenderer={
                                    this.chipRenderer
 
                                }
                            />
                        </MuiThemeProvider>
                    </div>
                )
            case 'browse':
                return (
                    <div>
                        <label>
                            {this.props['data-labelText']}
                        </label>
                        <p>
                            {this.props['data-fieldValue']}
                        </p>
                    </div>
                )
 
            default:
                break;
        }
    }
}