Skip to content

Commit 5e986e4

Browse files
committed
removed unneccessary wrappers and re-worked how
the "show secrets" button works
1 parent f94aaa3 commit 5e986e4

File tree

3 files changed

+35
-39
lines changed

3 files changed

+35
-39
lines changed

modules/st2-auto-form/fields/base.js

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import _ from 'lodash';
1616
import React from 'react';
1717
import { PropTypes } from 'prop-types';
1818

19-
import { TextFieldWrapper, Button } from '../wrappers';
19+
import { TextFieldWrapper } from '../wrappers';
2020
import TextareaAutosize from 'react-textarea-autosize';
2121

2222
export class Textarea extends TextareaAutosize {
@@ -45,6 +45,7 @@ export class BaseTextField extends React.Component {
4545
spec: PropTypes.object,
4646
value: PropTypes.any,
4747
disabled: PropTypes.bool,
48+
visible: PropTypes.bool,
4849
onChange: PropTypes.func,
4950
'data-test': PropTypes.string,
5051
}
@@ -54,6 +55,7 @@ export class BaseTextField extends React.Component {
5455

5556
this.state = {
5657
value: this.toStateValue(this.props.value),
58+
visible: false,
5759
};
5860
}
5961

@@ -75,10 +77,6 @@ export class BaseTextField extends React.Component {
7577
throw new Error('not implemented');
7678
}
7779

78-
toggleVisibility() {
79-
this.setState({ visible: !this.state.visible });
80-
}
81-
8280
validate(v, spec={}) {
8381
if ((v === '' || v === undefined) && spec.required) {
8482
return 'parameter is required';
@@ -109,33 +107,34 @@ export class BaseTextField extends React.Component {
109107
}
110108
}
111109

110+
visibilityToggle() {
111+
this.setState({visible: !this.state.visible})
112+
}
113+
112114
emitChange() {
113115
return this.props.onChange(this.fromStateValue(this.state.value));
114116
}
115117

116118
render() {
117119
const { icon } = this.constructor;
118-
const { invalid, visible } = this.state;
120+
const { invalid } = this.state;
119121
const { spec={} } = this.props;
120-
const wrapperProps = Object.assign({}, this.props);
122+
const wrapperProps = Object.assign({}, this.props, {
123+
visibilityToggle: () => this.visibilityToggle(),
124+
visible: this.state.visible,
125+
});
121126

122127
if (invalid) {
123128
wrapperProps.invalid = invalid;
124129
}
125130

126-
const buttonProps = {
127-
icon: visible ? 'view2' : 'view',
128-
title: visible ? 'hide value' : 'see value',
129-
onClick: () => this.toggleVisibility(),
130-
};
131-
132131
const inputProps = {
133-
className: spec.secret && !visible ? 'st2-auto-form__field--secret' : 'st2-auto-form__field',
132+
className: spec.secret && !this.state.visible ? 'st2-auto-form__field--secret' : 'st2-auto-form__field',
134133
type: 'text',
135134
placeholder:this.toStateValue(spec.default),
136135
disabled: this.props.disabled,
137136
value: this.state.value,
138-
spellCheck: spec.secret && !visible ? false : true,
137+
spellCheck: spec.secret && !this.state.visible ? false : true,
139138
onChange: (e) => this.handleChange(e, e.target.value),
140139
'data-test': this.props['data-test'],
141140
};
@@ -146,10 +145,7 @@ export class BaseTextField extends React.Component {
146145

147146
return (
148147
<TextFieldWrapper icon={icon} {...wrapperProps}>
149-
<div >
150-
{ !this.props.disabled && spec.secret && <Button {...buttonProps} /> }
151148
<input {...inputProps} />
152-
</div>
153149
</TextFieldWrapper>
154150
);
155151
}
@@ -158,31 +154,24 @@ export class BaseTextField extends React.Component {
158154
export class BaseTextareaField extends BaseTextField {
159155
render() {
160156
const { icon } = this.constructor;
161-
const { invalid, visible } = this.state;
157+
const { invalid } = this.state;
162158
const { spec = {} } = this.props;
163159

164-
const wrapperProps = Object.assign({}, this.props);
160+
const wrapperProps = Object.assign({}, this.props, {
161+
visibilityToggle: () => this.visibilityToggle(),
162+
visible: this.state.visible,
163+
});
165164

166165
if (invalid) {
167166
wrapperProps.invalid = invalid;
168167
}
169168

170-
const buttonProps = {
171-
icon: visible ? 'view2' : 'view',
172-
title: visible ? 'hide value' : 'see value',
173-
onClick: () => this.toggleVisibility(),
174-
};
175-
176-
const wrapperBlockProps = {
177-
className: 'st2-auto-form__wrapper-block',
178-
};
179-
180169
const inputProps = {
181-
className: spec.secret && !visible ? 'st2-auto-form__field--secret' : 'st2-auto-form__field',
170+
className: spec.secret && !this.state.visible ? 'st2-auto-form__field--secret' : 'st2-auto-form__field',
182171
placeholder: this.toStateValue(spec.default),
183172
disabled: this.props.disabled,
184173
value: this.state.value,
185-
spellCheck: spec.secret && !visible ? false : true,
174+
spellCheck: spec.secret && !this.state.visible ? false : true,
186175
onChange: (e) => this.handleChange(e, e.target.value),
187176
minRows: 1,
188177
maxRows: 10,
@@ -195,10 +184,7 @@ export class BaseTextareaField extends BaseTextField {
195184

196185
return (
197186
<TextFieldWrapper icon={icon} {...wrapperProps}>
198-
<div {...wrapperBlockProps}>
199-
{ !this.props.disabled && spec.secret && <Button {...buttonProps} /> }
200187
<Textarea {...inputProps} />
201-
</div>
202188
</TextFieldWrapper>
203189
);
204190
}

modules/st2-auto-form/style.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,6 @@
180180
color: black;
181181
}
182182

183-
&__wrapper-block &__button {
184-
right: 16px;
185-
}
186-
187183
&__text-field &__field,
188184
&__text-field &__field--secret {
189185
font-size: 13px;

modules/st2-auto-form/wrappers.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,28 @@ export class TextFieldWrapper extends React.Component {
171171
children: PropTypes.element.isRequired,
172172
icon: PropTypes.string,
173173
labelClass: PropTypes.string,
174+
visible: PropTypes.bool,
175+
visibilityToggle: PropTypes.func,
174176
}
175177

178+
handleVisibilityToggle() {
179+
this.props.visibilityToggle && this.props.visibilityToggle()
180+
}
181+
182+
176183
render() {
184+
const buttonProps = {
185+
icon: this.props.visible ? 'view2' : 'view',
186+
title: this.props.visible ? 'hide value' : 'see value',
187+
onClick: () => this.handleVisibilityToggle(),
188+
};
189+
177190
const line = (
178191
<div className='st2-auto-form__line'>
179192
<Label className={this.props.labelClass || 'st2-auto-form__text-field'}>
180193
<Icon name={this.props.icon} />
181194
<Title {...this.props} />
195+
{ this.props.spec && this.props.spec.secret && <Button {...buttonProps} />}
182196
{ this.props.children }
183197
<ErrorMessage>{ this.props.invalid }</ErrorMessage>
184198
</Label>

0 commit comments

Comments
 (0)