Improve FieldBase (preface/postface and custom className)

pull/285/head
Kelvin Schoofs 3 years ago
parent 2f08f871db
commit 0c752ae00d

@ -8,6 +8,8 @@ interface Props<T> {
value: T; value: T;
optional?: boolean; optional?: boolean;
group?: FieldGroup; group?: FieldGroup;
preface?: React.ReactNode;
postface?: React.ReactNode;
validator?(value: T): string | null; validator?(value: T): string | null;
onChange(newValue?: T): void; onChange(newValue?: T): void;
} }
@ -40,7 +42,7 @@ export abstract class FieldBase<T, P = {}, S = {}> extends React.Component<Props
if (oldProps.value === value) return; if (oldProps.value === value) return;
this.setState({ oldValue: value, newValue: value }); this.setState({ oldValue: value, newValue: value });
} }
public abstract renderInput(): React.ReactNode; protected abstract renderInput(): React.ReactNode;
public getError(): string | null { public getError(): string | null {
const { newValue } = this.state; const { newValue } = this.state;
const { validator, optional } = this.props; const { validator, optional } = this.props;
@ -60,17 +62,22 @@ export abstract class FieldBase<T, P = {}, S = {}> extends React.Component<Props
public getLabel() { public getLabel() {
return this.props.label; return this.props.label;
} }
protected getClassName(): string {
return 'Field';
}
public render() { public render() {
const error = this.getError(); const error = this.getError();
const { description, label, optional } = this.props; const { description, label, optional, preface, postface } = this.props;
return <div className="Field"> return <div className={this.getClassName()}>
<FieldGroup.Consumer>{group => (group?.register(this), [])}</FieldGroup.Consumer> <FieldGroup.Consumer>{group => (group?.register(this), [])}</FieldGroup.Consumer>
<div className="label">{label}</div>{optional && <div className="optional">Optional</div>} <div className="label">{label}</div>{optional && <div className="optional">Optional</div>}
{description && <div className="description">{description}</div>} {description && <div className="description">{description}</div>}
{preface}
{error && <div className="error">{error}</div>} {error && <div className="error">{error}</div>}
<div className="value"> <div className="value">
{this.renderInput()} {this.renderInput()}
</div> </div>
{postface}
</div>; </div>;
} }
} }

Loading…
Cancel
Save