import Input from '../../common/Input/Input'; import { TextField, Button, Switch, Chip, Typography } from '@material-ui/core'; import { useStyles } from './ContextForm.styles'; import React, { useState } from 'react'; import { Add } from '@material-ui/icons'; import { trim } from '../../common/util'; interface IContextForm { contextName: string; contextDesc: string; legalValues: Array; stickiness: boolean; setContextName: React.Dispatch>; setContextDesc: React.Dispatch>; setStickiness: React.Dispatch>; setLegalValues: React.Dispatch>; handleSubmit: (e: any) => void; handleCancel: () => void; errors: { [key: string]: string }; mode: string; clearErrors: () => void; validateNameUniqueness: () => void; setErrors: React.Dispatch>; } const ENTER = 'Enter'; const ContextForm: React.FC = ({ children, handleSubmit, handleCancel, contextName, contextDesc, legalValues, stickiness, setContextName, setContextDesc, setLegalValues, setStickiness, errors, mode, validateNameUniqueness, setErrors, clearErrors, }) => { const styles = useStyles(); const [value, setValue] = useState(''); const [focused, setFocused] = useState(false); const submit = (event: React.SyntheticEvent) => { event.preventDefault(); if (focused) return; handleSubmit(event); }; const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === ENTER && focused) { addLegalValue(); return; } else if (event.key === ENTER) { handleSubmit(event); } }; const sortIgnoreCase = (a: string, b: string) => { a = a.toLowerCase(); b = b.toLowerCase(); if (a === b) return 0; if (a > b) return 1; return -1; }; const addLegalValue = () => { clearErrors(); if (!value) { return; } if (legalValues.indexOf(value) !== -1) { setErrors(prev => ({ ...prev, tag: 'Duplicate legal value', })); return; } setLegalValues(prev => [...prev, trim(value)].sort(sortIgnoreCase)); setValue(''); }; const removeLegalValue = (index: number) => { const filteredValues = legalValues.filter((_, i) => i !== index); setLegalValues([...filteredValues]); }; return (

Context information

What is your context name?

setContextName(trim(e.target.value))} error={Boolean(errors.name)} errorText={errors.name} onFocus={() => clearErrors()} onBlur={validateNameUniqueness} />

What is this context for?

setContextDesc(e.target.value)} />

Which values do you want to allow?

{legalValues.map((value, index) => { return ( removeLegalValue(index)} title="Remove value" /> ); })}
setValue(trim(e.target.value))} onKeyPress={e => handleKeyDown(e)} onBlur={e => setFocused(false)} onFocus={e => setFocused(true)} />

Custom stickiness (beta)

By enabling stickiness on this context field you can use it together with the flexible-rollout strategy. This will guarantee a consistent behavior for specific values of this context field. PS! Not all client SDK's support this feature yet!{' '} Read more

setStickiness(!stickiness)} /> {stickiness ? 'On' : 'Off'}
{children}
); }; export default ContextForm;