react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Feature Request: defaultValue prop in SimpleForm should support dynamic value

Open binhfoto opened this issue 9 years ago • 1 comments

My first try with Global Default Value

const generateCode = {
    number: 1,
    call: function() {
        let code =  'CODE' + this.number;
        this.number++;
        return code;
    }
};

const productDefaultValue = { created_at: new Date(), code:  generateCode.call()};

export const ProductCreate = (props) => (
    <Create {...props}>
        <SimpleForm defaultValue={productDefaultValue}>
            <TextInput source="code" />
            <DateInput source="created_at" />
        </SimpleForm>
    </Create>
);

The product 'code' is expected as 'CODE1', 'CODE2' and so on everytime we press CREATE button. But it does not, it's always 'CODE1'. 'created_at' prop has the same story.

Then, I try Per Field Default Value

export const ProductCreate = (props) => (
    <Create {...props}>
        <SimpleForm>
            <TextInput source="code" defaultValue={generateCode.call()}/>
            <DateInput source="created_at" defaultValue={new Date()}/>
        </SimpleForm>
    </Create>
);

The result is good for 'created_at' prop, it always receives new date value. But the 'code' prop is not pretty good. It is CODE1 at 1st time, CODE4 at 2nd time, CODE8 at 3rd time.

Seem that 'generateCode.call()' gets called everytime React renders as below _react2.default.createElement(_input.TextInput, _extends({}, MA_DH, { defaultValue: generateCode.call()})), ...);

I think we should somehow support dynamic default value.

If there is better way, please light me up. Thank you very much.

binhfoto avatar Feb 02 '17 16:02 binhfoto

Fixed by #277

fzaninotto avatar Feb 06 '17 20:02 fzaninotto