Search code examples
admin-on-restreact-admin

admin on rest: inject format change to filter input


I've got a problem I've been working on a few days, with my admin on rest implementation. We've got a request to make our filter input behave more like a "contains" than an "equal"

My API supports a lookup like that, on GET, by passing in % characters with your input (eg /app?foo=%25bar%25 returns apps where foo is like %bar%)

While in an ideal world, I could tell my users to provide the %s in the filter's TextInput fields, the great masses can't be troubled with inserting special characters.

So, my question is, where can I inject something to wrap the wildcard characters around the input being entered in the objects in my object?

I've tried re-implementing TextInput and updating the input.value on OnChange, render, etc but the restClient is still using the pre-altered input value.

Heres my crack at a custom inputComponent

class ContainsTextProps {
    public addField?: PropTypes.bool.isRequired = true;
    public elStyle?: PropTypes.object;
    public input?: PropTypes.object;
    public isRequired?: PropTypes.bool;
    public label?: PropTypes.string;
    public meta?: PropTypes.object;
    public name?: PropTypes.string;
    public options?: PropTypes.object = {};
    public resource?: PropTypes.string;
    public source?: PropTypes.string;
    public type?: PropTypes.string = 'text';
    public onBlur?: PropTypes.func = () => {};
    public onChange?: PropTypes.func = () => {};
    public onFocus?: PropTypes.func = () => {};
}
class ContainsTextInputInternal extends React.Component<ContainsTextProps, {}> {
    constructor(props) {
        super(props);
        this.handleBlur = this.handleBlur.bind(this);
        this.handleFocus = this.handleFocus.bind(this);
        this.handleChange = this.handleChange.bind(this);
    }

    public handleBlur = (eventOrValue) => {
        // this.props.onBlur(eventOrValue);
        this.props.input.onBlur(eventOrValue);
    }

    public handleFocus = (event) => {
        // this.props.onFocus(event);
        this.props.input.onFocus(event);
    }

    public handleChange = (eventOrValue, newvalue) => {
        // this.props.onChange(eventOrValue);
        this.props.input.onChange(eventOrValue, newvalue);
    }


    public render() {
        const {
            elStyle,
            input,
            label,
            meta: { touched, error },
            options,
            type,
        } = this.props;

        if (input && input.value && input.value.length > 0 && input.value.indexOf('%') < 0) {
            input.value = `%${input.value}%`;
        }

        return (
            <TextField
                {...input}
                onBlur={this.handleBlur}
                onFocus={this.handleFocus}
                onChange={this.handleChange}
                type={type}
                // floatingLabelText={<FieldTitle label={label} source={source} resource={resource} isRequired={isRequired} />}
                floatingLabelText={label}
                errorText={touched && error}
                style={elStyle}
                value={input.value}
                {...options}
            />
        );
    }
}

export const ContainsTextInput = pure(ContainsTextInputInternal);


Solution

  • What you need is a custom REST client that detects the query and injects the special characters into the input.

    https://marmelab.com/admin-on-rest/RestClients.html#writing-your-own-rest-client

    This should get you started. If you only have 1 special case where you only need to inject the special characters then you can also use a REST wrapper.

    https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload